blob: f45c514a0947c09cc88eadd60225c9b7f49c616f [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
Willy Tarreau5e205522011-12-17 16:34:27 +010022#include <netinet/tcp.h>
23
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <sys/socket.h>
25#include <sys/stat.h>
26#include <sys/types.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 Tarreauf1348312010-10-07 15:54:11 +0200847const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */
848const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive),
849 Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten,
850 Set-cookie Updated, unknown, unknown */
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200851struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200852struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100853
Emeric Brun3a058f32009-06-30 18:26:00 +0200854void http_sess_clflog(struct session *s)
855{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100856 char pn[INET6_ADDRSTRLEN];
Emeric Brun3a058f32009-06-30 18:26:00 +0200857 struct proxy *fe = s->fe;
858 struct proxy *be = s->be;
859 struct proxy *prx_log;
860 struct http_txn *txn = &s->txn;
861 int tolog, level, err;
862 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +0100863 const char *svid;
Emeric Brun3a058f32009-06-30 18:26:00 +0200864 struct tm tm;
865 static char tmpline[MAX_SYSLOG_LEN];
866 int hdr;
867 size_t w;
868 int t_request;
869
870 prx_log = fe;
871 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +0200872 (s->req->cons->conn_retries != be->conn_retries) ||
Emeric Brun3a058f32009-06-30 18:26:00 +0200873 txn->status >= 500;
874
Willy Tarreau6471afb2011-09-23 10:54:59 +0200875 if (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn)) == AF_UNIX)
Emeric Brun5bd86a82010-10-22 17:23:04 +0200876 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Emeric Brun3a058f32009-06-30 18:26:00 +0200877
878 get_gmtime(s->logs.accept_date.tv_sec, &tm);
879
880 /* FIXME: let's limit ourselves to frontend logging for now. */
881 tolog = fe->to_log;
882
883 h = tmpline;
884
885 w = snprintf(h, sizeof(tmpline),
886 "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]",
887 pn,
888 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
889 tm.tm_hour, tm.tm_min, tm.tm_sec);
890 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
891 goto trunc;
892 h += w;
893
894 if (h >= tmpline + sizeof(tmpline) - 4)
895 goto trunc;
896
897 *(h++) = ' ';
898 *(h++) = '\"';
899 uri = txn->uri ? txn->uri : "<BADREQ>";
900 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
901 '#', url_encode_map, uri);
902 *(h++) = '\"';
903
904 w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out);
905 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
906 goto trunc;
907 h += w;
908
909 if (h >= tmpline + sizeof(tmpline) - 9)
910 goto trunc;
911 memcpy(h, " \"-\" \"-\"", 8);
912 h += 8;
913
914 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
915 " %d %03d",
Willy Tarreau6471afb2011-09-23 10:54:59 +0200916 s->req->prod->addr.from.ss_family == AF_UNIX ? s->listener->luid :
917 get_host_port(&s->req->prod->addr.from),
Emeric Brun3a058f32009-06-30 18:26:00 +0200918 (int)s->logs.accept_date.tv_usec/1000);
919 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
920 goto trunc;
921 h += w;
922
923 w = strlen(fe->id);
924 if (h >= tmpline + sizeof(tmpline) - 4 - w)
925 goto trunc;
926 *(h++) = ' ';
927 *(h++) = '\"';
928 memcpy(h, fe->id, w);
929 h += w;
930 *(h++) = '\"';
931
932 w = strlen(be->id);
933 if (h >= tmpline + sizeof(tmpline) - 4 - w)
934 goto trunc;
935 *(h++) = ' ';
936 *(h++) = '\"';
937 memcpy(h, be->id, w);
938 h += w;
939 *(h++) = '\"';
940
Willy Tarreau71904a42011-02-13 14:30:26 +0100941 if (!(tolog & LW_SVID))
942 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200943 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100944 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200945 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100946 break;
947 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200948 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100949 break;
950 default:
951 svid = "<NOSRV>";
952 break;
953 }
Emeric Brun3a058f32009-06-30 18:26:00 +0200954
955 w = strlen(svid);
956 if (h >= tmpline + sizeof(tmpline) - 4 - w)
957 goto trunc;
958 *(h++) = ' ';
959 *(h++) = '\"';
960 memcpy(h, svid, w);
961 h += w;
962 *(h++) = '\"';
963
964 t_request = -1;
965 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
966 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
967 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
968 " %d %ld %ld %ld %ld",
969 t_request,
970 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
971 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
972 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
973 s->logs.t_close);
974 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
975 goto trunc;
976 h += w;
977
978 if (h >= tmpline + sizeof(tmpline) - 8)
979 goto trunc;
980 *(h++) = ' ';
981 *(h++) = '\"';
982 *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT];
983 *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT];
984 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
985 *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-';
986 *(h++) = '\"';
987
988 w = snprintf(h, sizeof(tmpline) - (h - tmpline),
989 " %d %d %d %d %d %ld %ld",
Willy Tarreau827aee92011-03-10 16:55:02 +0100990 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Willy Tarreauee28de02010-06-01 09:51:00 +0200991 (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries,
Emeric Brun3a058f32009-06-30 18:26:00 +0200992 s->logs.srv_queue_size, s->logs.prx_queue_size);
993
994 if (w < 0 || w >= sizeof(tmpline) - (h - tmpline))
995 goto trunc;
996 h += w;
997
998 if (txn->cli_cookie) {
999 w = strlen(txn->cli_cookie);
1000 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1001 goto trunc;
1002 *(h++) = ' ';
1003 *(h++) = '\"';
1004 memcpy(h, txn->cli_cookie, w);
1005 h += w;
1006 *(h++) = '\"';
1007 } else {
1008 if (h >= tmpline + sizeof(tmpline) - 5)
1009 goto trunc;
1010 memcpy(h, " \"-\"", 4);
1011 h += 4;
1012 }
1013
1014 if (txn->srv_cookie) {
1015 w = strlen(txn->srv_cookie);
1016 if (h >= tmpline + sizeof(tmpline) - 4 - w)
1017 goto trunc;
1018 *(h++) = ' ';
1019 *(h++) = '\"';
1020 memcpy(h, txn->srv_cookie, w);
1021 h += w;
1022 *(h++) = '\"';
1023 } else {
1024 if (h >= tmpline + sizeof(tmpline) - 5)
1025 goto trunc;
1026 memcpy(h, " \"-\"", 4);
1027 h += 4;
1028 }
1029
1030 if ((fe->to_log & LW_REQHDR) && txn->req.cap) {
1031 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1032 if (h >= sizeof (tmpline) + tmpline - 4)
1033 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001034 if (txn->req.cap[hdr] != NULL) {
1035 *(h++) = ' ';
1036 *(h++) = '\"';
1037 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1038 '#', hdr_encode_map, txn->req.cap[hdr]);
1039 *(h++) = '\"';
1040 } else {
1041 memcpy(h, " \"-\"", 4);
1042 h += 4;
1043 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001044 }
1045 }
1046
1047 if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) {
1048 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1049 if (h >= sizeof (tmpline) + tmpline - 4)
1050 goto trunc;
Cyril Bonté7f2c5392010-03-13 15:15:07 +01001051 if (txn->rsp.cap[hdr] != NULL) {
1052 *(h++) = ' ';
1053 *(h++) = '\"';
1054 h = encode_string(h, tmpline + sizeof(tmpline) - 2,
1055 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1056 *(h++) = '\"';
1057 } else {
1058 memcpy(h, " \"-\"", 4);
1059 h += 4;
1060 }
Emeric Brun3a058f32009-06-30 18:26:00 +02001061 }
1062 }
1063
1064trunc:
1065 *h = '\0';
1066
1067 level = LOG_INFO;
1068 if (err && (fe->options2 & PR_O2_LOGERRORS))
1069 level = LOG_ERR;
1070
1071 send_log(prx_log, level, "%s\n", tmpline);
1072
1073 s->logs.logwait = 0;
1074}
1075
Willy Tarreau42250582007-04-01 01:30:43 +02001076/*
1077 * send a log for the session when we have enough info about it.
1078 * Will not log if the frontend has no log defined.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001080void http_sess_log(struct session *s)
Willy Tarreau42250582007-04-01 01:30:43 +02001081{
Cyril Bontéacd7d632010-11-01 19:26:02 +01001082 char pn[INET6_ADDRSTRLEN];
Willy Tarreau42250582007-04-01 01:30:43 +02001083 struct proxy *fe = s->fe;
1084 struct proxy *be = s->be;
1085 struct proxy *prx_log;
1086 struct http_txn *txn = &s->txn;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001087 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +02001088 char *uri, *h;
Willy Tarreau71904a42011-02-13 14:30:26 +01001089 const char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +02001090 struct tm tm;
Willy Tarreau42250582007-04-01 01:30:43 +02001091 static char tmpline[MAX_SYSLOG_LEN];
Willy Tarreau70089872008-06-13 21:12:51 +02001092 int t_request;
Willy Tarreau42250582007-04-01 01:30:43 +02001093 int hdr;
1094
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001095 /* if we don't want to log normal traffic, return now */
1096 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) ||
Willy Tarreauee28de02010-06-01 09:51:00 +02001097 (s->req->cons->conn_retries != be->conn_retries) ||
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001098 txn->status >= 500;
1099 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
1100 return;
1101
William Lallemand0f99e342011-10-12 17:50:54 +02001102 if (LIST_ISEMPTY(&fe->logsrvs))
Willy Tarreau42250582007-04-01 01:30:43 +02001103 return;
1104 prx_log = fe;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105
Emeric Brun3a058f32009-06-30 18:26:00 +02001106 if (prx_log->options2 & PR_O2_CLFLOG)
1107 return http_sess_clflog(s);
1108
Willy Tarreau6471afb2011-09-23 10:54:59 +02001109 if (addr_to_str(&s->req->prod->addr.from, pn, sizeof(pn)) == AF_UNIX)
Willy Tarreau631f01c2011-09-05 00:36:48 +02001110 snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid);
Willy Tarreau42250582007-04-01 01:30:43 +02001111
Willy Tarreaub7f694f2008-06-22 17:18:02 +02001112 get_localtime(s->logs.accept_date.tv_sec, &tm);
Willy Tarreau42250582007-04-01 01:30:43 +02001113
1114 /* FIXME: let's limit ourselves to frontend logging for now. */
1115 tolog = fe->to_log;
1116
1117 h = tmpline;
1118 if (fe->to_log & LW_REQHDR &&
1119 txn->req.cap &&
1120 (h < tmpline + sizeof(tmpline) - 10)) {
1121 *(h++) = ' ';
1122 *(h++) = '{';
1123 for (hdr = 0; hdr < fe->nb_req_cap; hdr++) {
1124 if (hdr)
1125 *(h++) = '|';
1126 if (txn->req.cap[hdr] != NULL)
1127 h = encode_string(h, tmpline + sizeof(tmpline) - 7,
1128 '#', hdr_encode_map, txn->req.cap[hdr]);
1129 }
1130 *(h++) = '}';
1131 }
1132
1133 if (fe->to_log & LW_RSPHDR &&
1134 txn->rsp.cap &&
1135 (h < tmpline + sizeof(tmpline) - 7)) {
1136 *(h++) = ' ';
1137 *(h++) = '{';
1138 for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) {
1139 if (hdr)
1140 *(h++) = '|';
1141 if (txn->rsp.cap[hdr] != NULL)
1142 h = encode_string(h, tmpline + sizeof(tmpline) - 4,
1143 '#', hdr_encode_map, txn->rsp.cap[hdr]);
1144 }
1145 *(h++) = '}';
1146 }
1147
1148 if (h < tmpline + sizeof(tmpline) - 4) {
1149 *(h++) = ' ';
1150 *(h++) = '"';
1151 uri = txn->uri ? txn->uri : "<BADREQ>";
1152 h = encode_string(h, tmpline + sizeof(tmpline) - 1,
1153 '#', url_encode_map, uri);
1154 *(h++) = '"';
1155 }
1156 *h = '\0';
1157
Willy Tarreau71904a42011-02-13 14:30:26 +01001158 if (!(tolog & LW_SVID))
1159 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001160 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +01001161 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001162 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +01001163 break;
1164 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02001165 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +01001166 break;
1167 default:
1168 svid = "<NOSRV>";
1169 break;
1170 }
Willy Tarreau42250582007-04-01 01:30:43 +02001171
Willy Tarreau70089872008-06-13 21:12:51 +02001172 t_request = -1;
1173 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
1174 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
1175
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +02001176 level = LOG_INFO;
1177 if (err && (fe->options2 & PR_O2_LOGERRORS))
1178 level = LOG_ERR;
1179
1180 send_log(prx_log, level,
Willy Tarreau42250582007-04-01 01:30:43 +02001181 "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +02001182 " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld"
1183 " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +02001184 (s->req->prod->addr.from.ss_family == AF_UNIX) ? "unix" : pn,
1185 (s->req->prod->addr.from.ss_family == AF_UNIX) ? s->listener->luid :
1186 get_host_port(&s->req->prod->addr.from),
Willy Tarreaufe944602007-10-25 10:34:16 +02001187 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +02001188 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +02001189 fe->id, be->id, svid,
Willy Tarreau70089872008-06-13 21:12:51 +02001190 t_request,
1191 (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1,
Willy Tarreau42250582007-04-01 01:30:43 +02001192 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
1193 (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1,
1194 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
1195 txn->status,
Willy Tarreau8b3977f2008-01-18 11:16:32 +01001196 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +02001197 txn->cli_cookie ? txn->cli_cookie : "-",
1198 txn->srv_cookie ? txn->srv_cookie : "-",
1199 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
1200 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
1201 (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-',
1202 (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-',
Willy Tarreau827aee92011-03-10 16:55:02 +01001203 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +01001204 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +02001205 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +02001206 s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline);
1207
1208 s->logs.logwait = 0;
1209}
1210
Willy Tarreau117f59e2007-03-04 18:17:17 +01001211
1212/*
1213 * Capture headers from message starting at <som> according to header list
1214 * <cap_hdr>, and fill the <idx> structure appropriately.
1215 */
1216void capture_headers(char *som, struct hdr_idx *idx,
1217 char **cap, struct cap_hdr *cap_hdr)
1218{
1219 char *eol, *sol, *col, *sov;
1220 int cur_idx;
1221 struct cap_hdr *h;
1222 int len;
1223
1224 sol = som + hdr_idx_first_pos(idx);
1225 cur_idx = hdr_idx_first_idx(idx);
1226
1227 while (cur_idx) {
1228 eol = sol + idx->v[cur_idx].len;
1229
1230 col = sol;
1231 while (col < eol && *col != ':')
1232 col++;
1233
1234 sov = col + 1;
1235 while (sov < eol && http_is_lws[(unsigned char)*sov])
1236 sov++;
1237
1238 for (h = cap_hdr; h; h = h->next) {
1239 if ((h->namelen == col - sol) &&
1240 (strncasecmp(sol, h->name, h->namelen) == 0)) {
1241 if (cap[h->index] == NULL)
1242 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +02001243 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +01001244
1245 if (cap[h->index] == NULL) {
1246 Alert("HTTP capture : out of memory.\n");
1247 continue;
1248 }
1249
1250 len = eol - sov;
1251 if (len > h->len)
1252 len = h->len;
1253
1254 memcpy(cap[h->index], sov, len);
1255 cap[h->index][len]=0;
1256 }
1257 }
1258 sol = eol + idx->v[cur_idx].cr + 1;
1259 cur_idx = idx->v[cur_idx].next;
1260 }
1261}
1262
1263
Willy Tarreau42250582007-04-01 01:30:43 +02001264/* either we find an LF at <ptr> or we jump to <bad>.
1265 */
1266#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
1267
1268/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
1269 * otherwise to <http_msg_ood> with <state> set to <st>.
1270 */
1271#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
1272 ptr++; \
1273 if (likely(ptr < end)) \
1274 goto good; \
1275 else { \
1276 state = (st); \
1277 goto http_msg_ood; \
1278 } \
1279 } while (0)
1280
1281
Willy Tarreaubaaee002006-06-26 02:48:02 +02001282/*
Willy Tarreaua15645d2007-03-18 16:22:39 +01001283 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
1285 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
1286 * will give undefined results.
1287 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1288 * and that msg->sol points to the beginning of the response.
1289 * If a complete line is found (which implies that at least one CR or LF is
1290 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1291 * returned indicating an incomplete line (which does not mean that parts have
1292 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1293 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1294 * upon next call.
1295 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001296 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1298 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001299 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001301const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
1302 unsigned int state, const char *ptr, const char *end,
1303 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +01001304{
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001307 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001308 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +01001309 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
1310
1311 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001312 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001313 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1314 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001315 state = HTTP_MSG_ERROR;
1316 break;
1317
Willy Tarreau8973c702007-01-21 23:58:29 +01001318 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001319 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001321 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 goto http_msg_rpcode;
1323 }
1324 if (likely(HTTP_IS_SPHT(*ptr)))
1325 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
1326 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +01001327 state = HTTP_MSG_ERROR;
1328 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001329
Willy Tarreau8973c702007-01-21 23:58:29 +01001330 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001331 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 if (likely(!HTTP_IS_LWS(*ptr)))
1333 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1334
1335 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001336 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001337 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1338 }
1339
1340 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001341 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001342 http_msg_rsp_reason:
1343 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +01001344 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001345 msg->sl.st.r_l = 0;
1346 goto http_msg_rpline_eol;
1347
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001349 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001350 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001351 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +01001352 goto http_msg_rpreason;
1353 }
1354 if (likely(HTTP_IS_SPHT(*ptr)))
1355 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1356 /* so it's a CR/LF, so there is no reason phrase */
1357 goto http_msg_rsp_reason;
1358
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001360 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 if (likely(!HTTP_IS_CRLF(*ptr)))
1362 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001363 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001364 http_msg_rpline_eol:
1365 /* We have seen the end of line. Note that we do not
1366 * necessarily have the \n yet, but at least we know that we
1367 * have EITHER \r OR \n, otherwise the response would not be
1368 * complete. We can then record the response length and return
1369 * to the caller which will be able to register it.
1370 */
1371 msg->sl.st.l = ptr - msg->sol;
1372 return ptr;
1373
1374#ifdef DEBUG_FULL
1375 default:
1376 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1377 exit(1);
1378#endif
1379 }
1380
1381 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001382 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001383 if (ret_state)
1384 *ret_state = state;
1385 if (ret_ptr)
1386 *ret_ptr = (char *)ptr;
1387 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001388}
1389
Willy Tarreau8973c702007-01-21 23:58:29 +01001390/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 * This function parses a request line between <ptr> and <end>, starting with
1392 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1393 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1394 * will give undefined results.
1395 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1396 * and that msg->sol points to the beginning of the request.
1397 * If a complete line is found (which implies that at least one CR or LF is
1398 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1399 * returned indicating an incomplete line (which does not mean that parts have
1400 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1401 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1402 * upon next call.
1403 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001404 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1406 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001407 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001408 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001409const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1410 unsigned int state, const char *ptr, const char *end,
1411 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001412{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001415 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 if (likely(HTTP_IS_TOKEN(*ptr)))
1417 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001418
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001420 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1422 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 if (likely(HTTP_IS_CRLF(*ptr))) {
1425 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001426 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001428 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001430 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001432 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 msg->sl.rq.v_l = 0;
1434 goto http_msg_rqline_eol;
1435 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001436 state = HTTP_MSG_ERROR;
1437 break;
1438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001440 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001442 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 goto http_msg_rquri;
1444 }
1445 if (likely(HTTP_IS_SPHT(*ptr)))
1446 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1447 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1448 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001451 http_msg_rquri:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 if (likely(!HTTP_IS_LWS(*ptr)))
1453 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001456 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1458 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001459
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001460 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1461 goto http_msg_req09_uri_e;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001462
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001463 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001464 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001465 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001466 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001467 goto http_msg_rqver;
1468 }
1469 if (likely(HTTP_IS_SPHT(*ptr)))
1470 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1471 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1472 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001475 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001476 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001478
1479 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001480 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001481 http_msg_rqline_eol:
1482 /* We have seen the end of line. Note that we do not
1483 * necessarily have the \n yet, but at least we know that we
1484 * have EITHER \r OR \n, otherwise the request would not be
1485 * complete. We can then record the request length and return
1486 * to the caller which will be able to register it.
1487 */
1488 msg->sl.rq.l = ptr - msg->sol;
1489 return ptr;
1490 }
1491
1492 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001493 state = HTTP_MSG_ERROR;
1494 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496#ifdef DEBUG_FULL
1497 default:
1498 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1499 exit(1);
1500#endif
1501 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001504 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001505 if (ret_state)
1506 *ret_state = state;
1507 if (ret_ptr)
1508 *ret_ptr = (char *)ptr;
1509 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001510}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001511
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001512/*
1513 * Returns the data from Authorization header. Function may be called more
1514 * than once so data is stored in txn->auth_data. When no header is found
1515 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1516 * searching again for something we are unable to find anyway.
1517 */
1518
1519char get_http_auth_buff[BUFSIZE];
1520
1521int
1522get_http_auth(struct session *s)
1523{
1524
1525 struct http_txn *txn = &s->txn;
1526 struct chunk auth_method;
1527 struct hdr_ctx ctx;
1528 char *h, *p;
1529 int len;
1530
1531#ifdef DEBUG_AUTH
1532 printf("Auth for session %p: %d\n", s, txn->auth.method);
1533#endif
1534
1535 if (txn->auth.method == HTTP_AUTH_WRONG)
1536 return 0;
1537
1538 if (txn->auth.method)
1539 return 1;
1540
1541 txn->auth.method = HTTP_AUTH_WRONG;
1542
1543 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001544
1545 if (txn->flags & TX_USE_PX_CONN) {
1546 h = "Proxy-Authorization";
1547 len = strlen(h);
1548 } else {
1549 h = "Authorization";
1550 len = strlen(h);
1551 }
1552
1553 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001554 return 0;
1555
1556 h = ctx.line + ctx.val;
1557
1558 p = memchr(h, ' ', ctx.vlen);
1559 if (!p || p == h)
1560 return 0;
1561
1562 chunk_initlen(&auth_method, h, 0, p-h);
1563 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1564
1565 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1566
1567 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1568 get_http_auth_buff, BUFSIZE - 1);
1569
1570 if (len < 0)
1571 return 0;
1572
1573
1574 get_http_auth_buff[len] = '\0';
1575
1576 p = strchr(get_http_auth_buff, ':');
1577
1578 if (!p)
1579 return 0;
1580
1581 txn->auth.user = get_http_auth_buff;
1582 *p = '\0';
1583 txn->auth.pass = p+1;
1584
1585 txn->auth.method = HTTP_AUTH_BASIC;
1586 return 1;
1587 }
1588
1589 return 0;
1590}
1591
Willy Tarreau58f10d72006-12-04 02:26:12 +01001592
Willy Tarreau8973c702007-01-21 23:58:29 +01001593/*
1594 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001595 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001596 * when data are missing and recalled at the exact same location with no
1597 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001598 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001599 * fields. Note that msg->som and msg->sol will be initialized after completing
1600 * the first state, so that none of the msg pointers has to be initialized
1601 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001602 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001603void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1604{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001605 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001606 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001607
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001608 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001609 ptr = buf->lr;
1610 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001611
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001612 if (unlikely(ptr >= end))
1613 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001614
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001615 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001616 /*
1617 * First, states that are specific to the response only.
1618 * We check them first so that request and headers are
1619 * closer to each other (accessed more often).
1620 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001621 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001622 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001623 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001624 /* we have a start of message, but we have to check
1625 * first if we need to remove some CRLF. We can only
1626 * do this when send_max=0.
1627 */
1628 char *beg = buf->w + buf->send_max;
1629 if (beg >= buf->data + buf->size)
1630 beg -= buf->size;
1631 if (unlikely(ptr != beg)) {
1632 if (buf->send_max)
1633 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001634 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001635 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001636 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001637 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001638 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001639 hdr_idx_init(idx);
1640 state = HTTP_MSG_RPVER;
1641 goto http_msg_rpver;
1642 }
1643
1644 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1645 goto http_msg_invalid;
1646
1647 if (unlikely(*ptr == '\n'))
1648 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1649 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1650 /* stop here */
1651
Willy Tarreau8973c702007-01-21 23:58:29 +01001652 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001653 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001654 EXPECT_LF_HERE(ptr, http_msg_invalid);
1655 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1656 /* stop here */
1657
Willy Tarreau8973c702007-01-21 23:58:29 +01001658 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001659 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001660 case HTTP_MSG_RPVER_SP:
1661 case HTTP_MSG_RPCODE:
1662 case HTTP_MSG_RPCODE_SP:
1663 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001664 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001665 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001666 if (unlikely(!ptr))
1667 return;
1668
1669 /* we have a full response and we know that we have either a CR
1670 * or an LF at <ptr>.
1671 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001672 //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 +01001673 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1674
1675 msg->sol = ptr;
1676 if (likely(*ptr == '\r'))
1677 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1678 goto http_msg_rpline_end;
1679
Willy Tarreau8973c702007-01-21 23:58:29 +01001680 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001681 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001682 /* msg->sol must point to the first of CR or LF. */
1683 EXPECT_LF_HERE(ptr, http_msg_invalid);
1684 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1685 /* stop here */
1686
1687 /*
1688 * Second, states that are specific to the request only
1689 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001690 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001691 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001692 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001693 /* we have a start of message, but we have to check
1694 * first if we need to remove some CRLF. We can only
1695 * do this when send_max=0.
1696 */
1697 char *beg = buf->w + buf->send_max;
1698 if (beg >= buf->data + buf->size)
1699 beg -= buf->size;
1700 if (likely(ptr != beg)) {
1701 if (buf->send_max)
1702 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001703 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001704 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001705 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001706 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001707 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001708 /* we will need this when keep-alive will be supported
1709 hdr_idx_init(idx);
1710 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001711 state = HTTP_MSG_RQMETH;
1712 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001713 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001714
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001715 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1716 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001717
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001718 if (unlikely(*ptr == '\n'))
1719 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1720 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001721 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001723 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001724 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001725 EXPECT_LF_HERE(ptr, http_msg_invalid);
1726 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001727 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001728
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001729 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001730 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001731 case HTTP_MSG_RQMETH_SP:
1732 case HTTP_MSG_RQURI:
1733 case HTTP_MSG_RQURI_SP:
1734 case HTTP_MSG_RQVER:
1735 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001736 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001737 if (unlikely(!ptr))
1738 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001739
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001740 /* we have a full request and we know that we have either a CR
1741 * or an LF at <ptr>.
1742 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001743 //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 +01001744 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001745
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001746 msg->sol = ptr;
1747 if (likely(*ptr == '\r'))
1748 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001749 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001750
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001751 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001752 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001753 /* check for HTTP/0.9 request : no version information available.
1754 * msg->sol must point to the first of CR or LF.
1755 */
1756 if (unlikely(msg->sl.rq.v_l == 0))
1757 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001758
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001759 EXPECT_LF_HERE(ptr, http_msg_invalid);
1760 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001761 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001762
Willy Tarreau8973c702007-01-21 23:58:29 +01001763 /*
1764 * Common states below
1765 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001766 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001767 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001768 msg->sol = ptr;
1769 if (likely(!HTTP_IS_CRLF(*ptr))) {
1770 goto http_msg_hdr_name;
1771 }
1772
1773 if (likely(*ptr == '\r'))
1774 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1775 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001776
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001777 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001778 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001779 /* assumes msg->sol points to the first char */
1780 if (likely(HTTP_IS_TOKEN(*ptr)))
1781 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001782
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001783 if (likely(*ptr == ':')) {
1784 msg->col = ptr - buf->data;
1785 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1786 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001787
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001788 if (likely(msg->err_pos < -1) || *ptr == '\n')
1789 goto http_msg_invalid;
1790
1791 if (msg->err_pos == -1) /* capture error pointer */
1792 msg->err_pos = ptr - buf->data; /* >= 0 now */
1793
1794 /* and we still accept this non-token character */
1795 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001796
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001797 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001798 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001799 /* assumes msg->sol points to the first char and msg->col to the colon */
1800 if (likely(HTTP_IS_SPHT(*ptr)))
1801 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001802
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001803 /* header value can be basically anything except CR/LF */
1804 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001805
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001806 if (likely(!HTTP_IS_CRLF(*ptr))) {
1807 goto http_msg_hdr_val;
1808 }
1809
1810 if (likely(*ptr == '\r'))
1811 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1812 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001813
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001814 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001815 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001816 EXPECT_LF_HERE(ptr, http_msg_invalid);
1817 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001819 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001820 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001821 if (likely(HTTP_IS_SPHT(*ptr))) {
1822 /* replace HT,CR,LF with spaces */
1823 for (; buf->data+msg->sov < ptr; msg->sov++)
1824 buf->data[msg->sov] = ' ';
1825 goto http_msg_hdr_l1_sp;
1826 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001827 /* we had a header consisting only in spaces ! */
1828 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001829 goto http_msg_complete_header;
1830
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001831 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001832 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001833 /* assumes msg->sol points to the first char, msg->col to the
1834 * colon, and msg->sov points to the first character of the
1835 * value.
1836 */
1837 if (likely(!HTTP_IS_CRLF(*ptr)))
1838 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001839
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001840 msg->eol = ptr;
1841 /* Note: we could also copy eol into ->eoh so that we have the
1842 * real header end in case it ends with lots of LWS, but is this
1843 * really needed ?
1844 */
1845 if (likely(*ptr == '\r'))
1846 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1847 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001848
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001849 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001850 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001851 EXPECT_LF_HERE(ptr, http_msg_invalid);
1852 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001853
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001854 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001855 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001856 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1857 /* LWS: replace HT,CR,LF with spaces */
1858 for (; msg->eol < ptr; msg->eol++)
1859 *msg->eol = ' ';
1860 goto http_msg_hdr_val;
1861 }
1862 http_msg_complete_header:
1863 /*
1864 * It was a new header, so the last one is finished.
1865 * Assumes msg->sol points to the first char, msg->col to the
1866 * colon, msg->sov points to the first character of the value
1867 * and msg->eol to the first CR or LF so we know how the line
1868 * ends. We insert last header into the index.
1869 */
1870 /*
1871 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1872 write(2, msg->sol, msg->eol-msg->sol);
1873 fprintf(stderr,"\n");
1874 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001875
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001876 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1877 idx, idx->tail) < 0))
1878 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001879
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001880 msg->sol = ptr;
1881 if (likely(!HTTP_IS_CRLF(*ptr))) {
1882 goto http_msg_hdr_name;
1883 }
1884
1885 if (likely(*ptr == '\r'))
1886 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1887 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001888
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001889 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001890 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001891 /* Assumes msg->sol points to the first of either CR or LF */
1892 EXPECT_LF_HERE(ptr, http_msg_invalid);
1893 ptr++;
1894 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001895 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001896 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001897 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001898 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001899 return;
1900#ifdef DEBUG_FULL
1901 default:
1902 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1903 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001904#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001905 }
1906 http_msg_ood:
1907 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001908 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001909 buf->lr = ptr;
1910 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001911
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001912 http_msg_invalid:
1913 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001914 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001915 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001916 return;
1917}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001918
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001919/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1920 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1921 * nothing is done and 1 is returned.
1922 */
1923static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1924{
1925 int delta;
1926 char *cur_end;
1927
1928 if (msg->sl.rq.v_l != 0)
1929 return 1;
1930
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001931 cur_end = msg->sol + msg->sl.rq.l;
1932 delta = 0;
1933
1934 if (msg->sl.rq.u_l == 0) {
1935 /* if no URI was set, add "/" */
1936 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1937 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001938 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001939 }
1940 /* add HTTP version */
1941 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001942 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001943 cur_end += delta;
1944 cur_end = (char *)http_parse_reqline(msg, req->data,
1945 HTTP_MSG_RQMETH,
1946 msg->sol, cur_end + 1,
1947 NULL, NULL);
1948 if (unlikely(!cur_end))
1949 return 0;
1950
1951 /* we have a full HTTP/1.0 request now and we know that
1952 * we have either a CR or an LF at <ptr>.
1953 */
1954 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1955 return 1;
1956}
1957
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001958/* Parse the Connection: header of an HTTP request, looking for both "close"
1959 * and "keep-alive" values. If a buffer is provided and we already know that
1960 * some headers may safely be removed, we remove them now. The <to_del> flags
1961 * are used for that :
1962 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1963 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1964 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1965 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1966 * harmless combinations may be removed. Do not call that after changes have
1967 * been processed. If unused, the buffer can be NULL, and no data will be
1968 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001969 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001970void 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 +01001971{
Willy Tarreau5b154472009-12-21 20:11:07 +01001972 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001973 const char *hdr_val = "Connection";
1974 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001975
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001976 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001977 return;
1978
Willy Tarreau88d349d2010-01-25 12:15:43 +01001979 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1980 hdr_val = "Proxy-Connection";
1981 hdr_len = 16;
1982 }
1983
Willy Tarreau5b154472009-12-21 20:11:07 +01001984 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001985 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001986 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001987 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1988 txn->flags |= TX_HDR_CONN_KAL;
1989 if ((to_del & 2) && buf)
1990 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1991 else
1992 txn->flags |= TX_CON_KAL_SET;
1993 }
1994 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1995 txn->flags |= TX_HDR_CONN_CLO;
1996 if ((to_del & 1) && buf)
1997 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1998 else
1999 txn->flags |= TX_CON_CLO_SET;
2000 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002001 }
2002
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002003 txn->flags |= TX_HDR_CONN_PRS;
2004 return;
2005}
Willy Tarreau5b154472009-12-21 20:11:07 +01002006
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002007/* Apply desired changes on the Connection: header. Values may be removed and/or
2008 * added depending on the <wanted> flags, which are exclusively composed of
2009 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
2010 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
2011 */
2012void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
2013{
2014 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002015 const char *hdr_val = "Connection";
2016 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002017
2018 ctx.idx = 0;
2019
Willy Tarreau88d349d2010-01-25 12:15:43 +01002020
2021 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2022 hdr_val = "Proxy-Connection";
2023 hdr_len = 16;
2024 }
2025
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002026 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01002027 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002028 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
2029 if (wanted & TX_CON_KAL_SET)
2030 txn->flags |= TX_CON_KAL_SET;
2031 else
2032 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01002033 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002034 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
2035 if (wanted & TX_CON_CLO_SET)
2036 txn->flags |= TX_CON_CLO_SET;
2037 else
2038 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002039 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002040 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002041
2042 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
2043 return;
2044
2045 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
2046 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002047 hdr_val = "Connection: close";
2048 hdr_len = 17;
2049 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2050 hdr_val = "Proxy-Connection: close";
2051 hdr_len = 23;
2052 }
2053 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002054 }
2055
2056 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
2057 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01002058 hdr_val = "Connection: keep-alive";
2059 hdr_len = 22;
2060 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
2061 hdr_val = "Proxy-Connection: keep-alive";
2062 hdr_len = 28;
2063 }
2064 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002065 }
2066 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01002067}
2068
Willy Tarreaud98cf932009-12-27 22:54:55 +01002069/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
2070 * first byte of body, and increments msg->sov by the number of bytes parsed,
2071 * so that we know we can forward between ->som and ->sov. Note that due to
2072 * possible wrapping at the end of the buffer, it is possible that msg->sov is
2073 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01002074 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002075 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01002076 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002077int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01002078{
Willy Tarreaud98cf932009-12-27 22:54:55 +01002079 char *ptr = buf->lr;
2080 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01002081 unsigned int chunk = 0;
2082
2083 /* The chunk size is in the following form, though we are only
2084 * interested in the size and CRLF :
2085 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
2086 */
2087 while (1) {
2088 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002089 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002090 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002091 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01002092 if (c < 0) /* not a hex digit anymore */
2093 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002094 if (++ptr >= end)
2095 ptr = buf->data;
Willy Tarreau115acb92009-12-26 13:56:06 +01002096 if (chunk & 0xF000000) /* overflow will occur */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002097 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002098 chunk = (chunk << 4) + c;
2099 }
2100
Willy Tarreaud98cf932009-12-27 22:54:55 +01002101 /* empty size not allowed */
2102 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002103 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002104
2105 while (http_is_spht[(unsigned char)*ptr]) {
2106 if (++ptr >= end)
2107 ptr = buf->data;
2108 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002109 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01002110 }
2111
Willy Tarreaud98cf932009-12-27 22:54:55 +01002112 /* Up to there, we know that at least one byte is present at *ptr. Check
2113 * for the end of chunk size.
2114 */
2115 while (1) {
2116 if (likely(HTTP_IS_CRLF(*ptr))) {
2117 /* we now have a CR or an LF at ptr */
2118 if (likely(*ptr == '\r')) {
2119 if (++ptr >= end)
2120 ptr = buf->data;
2121 if (ptr == buf->r)
2122 return 0;
2123 }
Willy Tarreau115acb92009-12-26 13:56:06 +01002124
Willy Tarreaud98cf932009-12-27 22:54:55 +01002125 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002126 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002127 if (++ptr >= end)
2128 ptr = buf->data;
2129 /* done */
2130 break;
2131 }
2132 else if (*ptr == ';') {
2133 /* chunk extension, ends at next CRLF */
2134 if (++ptr >= end)
2135 ptr = buf->data;
2136 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01002137 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002138
2139 while (!HTTP_IS_CRLF(*ptr)) {
2140 if (++ptr >= end)
2141 ptr = buf->data;
2142 if (ptr == buf->r)
2143 return 0;
2144 }
2145 /* we have a CRLF now, loop above */
2146 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01002147 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002148 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002149 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01002150 }
2151
Willy Tarreaud98cf932009-12-27 22:54:55 +01002152 /* OK we found our CRLF and now <ptr> points to the next byte,
2153 * which may or may not be present. We save that into ->lr and
2154 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01002155 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01002156 msg->sov += ptr - buf->lr;
2157 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01002158 msg->chunk_len = chunk;
2159 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002160 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01002161 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002162 error:
2163 msg->err_pos = ptr - buf->data;
2164 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01002165}
2166
Willy Tarreaud98cf932009-12-27 22:54:55 +01002167/* This function skips trailers in the buffer <buf> associated with HTTP
2168 * message <msg>. The first visited position is buf->lr. If the end of
2169 * the trailers is found, it is automatically scheduled to be forwarded,
2170 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
2171 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01002172 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
2173 * zero. If a parse error is encountered, the function returns < 0 and does not
2174 * change anything except maybe buf->lr and msg->sov. Note that the message
2175 * must already be in HTTP_MSG_TRAILERS state before calling this function,
2176 * which implies that all non-trailers data have already been scheduled for
2177 * forwarding, and that the difference between msg->som and msg->sov exactly
2178 * matches the length of trailers already parsed and not forwarded. It is also
2179 * important to note that this function is designed to be able to parse wrapped
2180 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01002181 */
2182int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
2183{
2184 /* we have buf->lr which points to next line. Look for CRLF. */
2185 while (1) {
2186 char *p1 = NULL, *p2 = NULL;
2187 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01002188 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002189
2190 /* scan current line and stop at LF or CRLF */
2191 while (1) {
2192 if (ptr == buf->r)
2193 return 0;
2194
2195 if (*ptr == '\n') {
2196 if (!p1)
2197 p1 = ptr;
2198 p2 = ptr;
2199 break;
2200 }
2201
2202 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002203 if (p1) {
2204 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002205 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002206 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002207 p1 = ptr;
2208 }
2209
2210 ptr++;
2211 if (ptr >= buf->data + buf->size)
2212 ptr = buf->data;
2213 }
2214
2215 /* after LF; point to beginning of next line */
2216 p2++;
2217 if (p2 >= buf->data + buf->size)
2218 p2 = buf->data;
2219
Willy Tarreau638cd022010-01-03 07:42:04 +01002220 bytes = p2 - buf->lr;
2221 if (bytes < 0)
2222 bytes += buf->size;
2223
2224 /* schedule this line for forwarding */
2225 msg->sov += bytes;
2226 if (msg->sov >= buf->size)
2227 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002228
Willy Tarreau638cd022010-01-03 07:42:04 +01002229 if (p1 == buf->lr) {
2230 /* LF/CRLF at beginning of line => end of trailers at p2.
2231 * Everything was scheduled for forwarding, there's nothing
2232 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01002233 */
Willy Tarreau638cd022010-01-03 07:42:04 +01002234 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002235 msg->msg_state = HTTP_MSG_DONE;
2236 return 1;
2237 }
2238 /* OK, next line then */
2239 buf->lr = p2;
2240 }
2241}
2242
2243/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
2244 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
2245 * ->som, buf->lr in order to include this part into the next forwarding phase.
2246 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
2247 * not enough data are available, the function does not change anything and
2248 * returns zero. If a parse error is encountered, the function returns < 0 and
2249 * does not change anything. Note: this function is designed to parse wrapped
2250 * CRLF at the end of the buffer.
2251 */
2252int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
2253{
2254 char *ptr;
2255 int bytes;
2256
2257 /* NB: we'll check data availabilty at the end. It's not a
2258 * problem because whatever we match first will be checked
2259 * against the correct length.
2260 */
2261 bytes = 1;
2262 ptr = buf->lr;
2263 if (*ptr == '\r') {
2264 bytes++;
2265 ptr++;
2266 if (ptr >= buf->data + buf->size)
2267 ptr = buf->data;
2268 }
2269
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01002270 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01002271 return 0;
2272
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002273 if (*ptr != '\n') {
2274 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01002275 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01002276 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002277
2278 ptr++;
2279 if (ptr >= buf->data + buf->size)
2280 ptr = buf->data;
2281 buf->lr = ptr;
2282 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
2283 msg->sov = ptr - buf->data;
2284 msg->som = msg->sov - bytes;
2285 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
2286 return 1;
2287}
Willy Tarreau5b154472009-12-21 20:11:07 +01002288
Willy Tarreau83e3af02009-12-28 17:39:57 +01002289void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
2290{
2291 char *end = buf->data + buf->size;
2292 int off = buf->data + buf->size - buf->w;
2293
2294 /* two possible cases :
2295 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01002296 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01002297 */
2298 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01002299 int block1 = buf->l;
2300 int block2 = 0;
2301 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01002302 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01002303 block1 = buf->data + buf->size - buf->w;
2304 block2 = buf->r - buf->data;
2305 }
2306 if (block2)
2307 memcpy(swap_buffer, buf->data, block2);
2308 memmove(buf->data, buf->w, block1);
2309 if (block2)
2310 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002311 }
2312
2313 /* adjust all known pointers */
2314 buf->w = buf->data;
2315 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
2316 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
2317 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
2318 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
2319
2320 /* adjust relative pointers */
2321 msg->som = 0;
2322 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
2323 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
2324 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
2325
Willy Tarreau83e3af02009-12-28 17:39:57 +01002326 if (msg->err_pos >= 0) {
2327 msg->err_pos += off;
2328 if (msg->err_pos >= buf->size)
2329 msg->err_pos -= buf->size;
2330 }
2331
2332 buf->flags &= ~BF_FULL;
2333 if (buf->l >= buffer_max_len(buf))
2334 buf->flags |= BF_FULL;
2335}
2336
Willy Tarreaud787e662009-07-07 10:14:51 +02002337/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2338 * processing can continue on next analysers, or zero if it either needs more
2339 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2340 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2341 * when it has nothing left to do, and may remove any analyser when it wants to
2342 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002343 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002344int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002345{
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 /*
2347 * We will parse the partial (or complete) lines.
2348 * We will check the request syntax, and also join multi-line
2349 * headers. An index of all the lines will be elaborated while
2350 * parsing.
2351 *
2352 * For the parsing, we use a 28 states FSM.
2353 *
2354 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002355 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002356 * req->data + msg->eoh = end of processed headers / start of current one
2357 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002358 * req->lr = first non-visited byte
2359 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002360 *
2361 * At end of parsing, we may perform a capture of the error (if any), and
2362 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2363 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2364 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002365 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002366
Willy Tarreau59234e92008-11-30 23:51:27 +01002367 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002368 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 struct http_txn *txn = &s->txn;
2370 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002371 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002372
Willy Tarreau6bf17362009-02-24 10:48:35 +01002373 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2374 now_ms, __FUNCTION__,
2375 s,
2376 req,
2377 req->rex, req->wex,
2378 req->flags,
2379 req->l,
2380 req->analysers);
2381
Willy Tarreau52a0c602009-08-16 22:45:38 +02002382 /* we're speaking HTTP here, so let's speak HTTP to the client */
2383 s->srv_error = http_return_srv_error;
2384
Willy Tarreau83e3af02009-12-28 17:39:57 +01002385 /* There's a protected area at the end of the buffer for rewriting
2386 * purposes. We don't want to start to parse the request if the
2387 * protected area is affected, because we may have to move processed
2388 * data later, which is much more complicated.
2389 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002390 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002391 if ((txn->flags & TX_NOT_FIRST) &&
2392 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002393 req->r < req->lr ||
2394 req->r > req->data + req->size - global.tune.maxrewrite)) {
2395 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002396 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2397 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002398 /* some data has still not left the buffer, wake us once that's done */
2399 buffer_dont_connect(req);
2400 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2401 return 0;
2402 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002403 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002404 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002405 }
2406
Willy Tarreau065e8332010-01-08 00:30:20 +01002407 /* Note that we have the same problem with the response ; we
2408 * may want to send a redirect, error or anything which requires
2409 * some spare space. So we'll ensure that we have at least
2410 * maxrewrite bytes available in the response buffer before
2411 * processing that one. This will only affect pipelined
2412 * keep-alive requests.
2413 */
2414 if ((txn->flags & TX_NOT_FIRST) &&
2415 unlikely((s->rep->flags & BF_FULL) ||
2416 s->rep->r < s->rep->lr ||
2417 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2418 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002419 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2420 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002421 /* don't let a connection request be initiated */
2422 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002423 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002424 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002425 return 0;
2426 }
2427 }
2428
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002429 if (likely(req->lr < req->r))
2430 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002431 }
2432
Willy Tarreau59234e92008-11-30 23:51:27 +01002433 /* 1: we might have to print this header in debug mode */
2434 if (unlikely((global.mode & MODE_DEBUG) &&
2435 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002436 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002437 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002438 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002439
Willy Tarreau663308b2010-06-07 14:06:08 +02002440 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002441 eol = sol + msg->sl.rq.l;
2442 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002443
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 sol += hdr_idx_first_pos(&txn->hdr_idx);
2445 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002446
Willy Tarreau59234e92008-11-30 23:51:27 +01002447 while (cur_idx) {
2448 eol = sol + txn->hdr_idx.v[cur_idx].len;
2449 debug_hdr("clihdr", s, sol, eol);
2450 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2451 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002452 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002453 }
2454
Willy Tarreau58f10d72006-12-04 02:26:12 +01002455
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 /*
2457 * Now we quickly check if we have found a full valid request.
2458 * If not so, we check the FD and buffer states before leaving.
2459 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002460 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002461 * requests are checked first. When waiting for a second request
2462 * on a keep-alive session, if we encounter and error, close, t/o,
2463 * we note the error in the session flags but don't set any state.
2464 * Since the error will be noted there, it will not be counted by
2465 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002466 * Last, we may increase some tracked counters' http request errors on
2467 * the cases that are deliberately the client's fault. For instance,
2468 * a timeout or connection reset is not counted as an error. However
2469 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002471
Willy Tarreau655dce92009-11-08 13:10:58 +01002472 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002473 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002474 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002475 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002476 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002477 session_inc_http_req_ctr(s);
2478 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002479 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002481 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002482
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 /* 1: Since we are in header mode, if there's no space
2484 * left for headers, we won't be able to free more
2485 * later, so the session will never terminate. We
2486 * must terminate it now.
2487 */
2488 if (unlikely(req->flags & BF_FULL)) {
2489 /* FIXME: check if URI is set and return Status
2490 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002491 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002492 session_inc_http_req_ctr(s);
2493 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002494 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002495 if (msg->err_pos < 0)
2496 msg->err_pos = req->l;
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 goto return_bad_req;
2498 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002499
Willy Tarreau59234e92008-11-30 23:51:27 +01002500 /* 2: have we encountered a read error ? */
2501 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002502 if (!(s->flags & SN_ERR_MASK))
2503 s->flags |= SN_ERR_CLICL;
2504
Willy Tarreaufcffa692010-01-10 14:21:19 +01002505 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002506 goto failed_keep_alive;
2507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002509 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002510 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002511 session_inc_http_err_ctr(s);
2512 }
2513
Willy Tarreau59234e92008-11-30 23:51:27 +01002514 msg->msg_state = HTTP_MSG_ERROR;
2515 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002516
Willy Tarreauda7ff642010-06-23 11:44:09 +02002517 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002518 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002519 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002520 if (s->listener->counters)
2521 s->listener->counters->failed_req++;
2522
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 if (!(s->flags & SN_FINST_MASK))
2524 s->flags |= SN_FINST_R;
2525 return 0;
2526 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002527
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 /* 3: has the read timeout expired ? */
2529 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002530 if (!(s->flags & SN_ERR_MASK))
2531 s->flags |= SN_ERR_CLITO;
2532
Willy Tarreaufcffa692010-01-10 14:21:19 +01002533 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002534 goto failed_keep_alive;
2535
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002537 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002538 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002539 session_inc_http_err_ctr(s);
2540 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002541 txn->status = 408;
2542 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2543 msg->msg_state = HTTP_MSG_ERROR;
2544 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002545
Willy Tarreauda7ff642010-06-23 11:44:09 +02002546 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002547 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002548 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002549 if (s->listener->counters)
2550 s->listener->counters->failed_req++;
2551
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 if (!(s->flags & SN_FINST_MASK))
2553 s->flags |= SN_FINST_R;
2554 return 0;
2555 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002556
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 /* 4: have we encountered a close ? */
2558 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002559 if (!(s->flags & SN_ERR_MASK))
2560 s->flags |= SN_ERR_CLICL;
2561
Willy Tarreaufcffa692010-01-10 14:21:19 +01002562 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002563 goto failed_keep_alive;
2564
Willy Tarreau4076a152009-04-02 15:18:36 +02002565 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002566 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 txn->status = 400;
2568 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2569 msg->msg_state = HTTP_MSG_ERROR;
2570 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002571
Willy Tarreauda7ff642010-06-23 11:44:09 +02002572 session_inc_http_err_ctr(s);
2573 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002574 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002575 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002576 if (s->listener->counters)
2577 s->listener->counters->failed_req++;
2578
Willy Tarreau59234e92008-11-30 23:51:27 +01002579 if (!(s->flags & SN_FINST_MASK))
2580 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002581 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002582 }
2583
Willy Tarreau520d95e2009-09-19 21:04:57 +02002584 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002585 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002586 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002587#ifdef TCP_QUICKACK
2588 if (s->listener->options & LI_O_NOQUICKACK) {
2589 /* We need more data, we have to re-enable quick-ack in case we
2590 * previously disabled it, otherwise we might cause the client
2591 * to delay next data.
2592 */
2593 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2594 }
2595#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002596
Willy Tarreaufcffa692010-01-10 14:21:19 +01002597 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2598 /* If the client starts to talk, let's fall back to
2599 * request timeout processing.
2600 */
2601 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002602 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002603 }
2604
Willy Tarreau59234e92008-11-30 23:51:27 +01002605 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002606 if (!tick_isset(req->analyse_exp)) {
2607 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2608 (txn->flags & TX_WAIT_NEXT_RQ) &&
2609 tick_isset(s->be->timeout.httpka))
2610 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2611 else
2612 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2613 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002614
Willy Tarreau59234e92008-11-30 23:51:27 +01002615 /* we're not ready yet */
2616 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002617
2618 failed_keep_alive:
2619 /* Here we process low-level errors for keep-alive requests. In
2620 * short, if the request is not the first one and it experiences
2621 * a timeout, read error or shutdown, we just silently close so
2622 * that the client can try again.
2623 */
2624 txn->status = 0;
2625 msg->msg_state = HTTP_MSG_RQBEFORE;
2626 req->analysers = 0;
2627 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002628 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002629 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002630 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002631 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002632
Willy Tarreaud787e662009-07-07 10:14:51 +02002633 /* OK now we have a complete HTTP request with indexed headers. Let's
2634 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002635 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2636 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2637 * points to the CRLF of the request line. req->lr points to the first
2638 * byte after the last LF. msg->col and msg->sov point to the first
2639 * byte of data. msg->eol cannot be trusted because it may have been
2640 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002641 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002642
Willy Tarreauda7ff642010-06-23 11:44:09 +02002643 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002644 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2645
Willy Tarreaub16a5742010-01-10 14:46:16 +01002646 if (txn->flags & TX_WAIT_NEXT_RQ) {
2647 /* kill the pending keep-alive timeout */
2648 txn->flags &= ~TX_WAIT_NEXT_RQ;
2649 req->analyse_exp = TICK_ETERNITY;
2650 }
2651
2652
Willy Tarreaud787e662009-07-07 10:14:51 +02002653 /* Maybe we found in invalid header name while we were configured not
2654 * to block on that, so we have to capture it now.
2655 */
2656 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002657 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002658
Willy Tarreau59234e92008-11-30 23:51:27 +01002659 /*
2660 * 1: identify the method
2661 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002662 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002663
2664 /* we can make use of server redirect on GET and HEAD */
2665 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2666 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002667
Willy Tarreau59234e92008-11-30 23:51:27 +01002668 /*
2669 * 2: check if the URI matches the monitor_uri.
2670 * We have to do this for every request which gets in, because
2671 * the monitor-uri is defined by the frontend.
2672 */
2673 if (unlikely((s->fe->monitor_uri_len != 0) &&
2674 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002675 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002676 s->fe->monitor_uri,
2677 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002678 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002679 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002680 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002681 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002682
Willy Tarreau59234e92008-11-30 23:51:27 +01002683 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002684 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002685
Willy Tarreau59234e92008-11-30 23:51:27 +01002686 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002687 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2688 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002689
Willy Tarreau59234e92008-11-30 23:51:27 +01002690 ret = acl_pass(ret);
2691 if (cond->pol == ACL_COND_UNLESS)
2692 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002693
Willy Tarreau59234e92008-11-30 23:51:27 +01002694 if (ret) {
2695 /* we fail this request, let's return 503 service unavail */
2696 txn->status = 503;
2697 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2698 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002699 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002700 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002701
Willy Tarreau59234e92008-11-30 23:51:27 +01002702 /* nothing to fail, let's reply normaly */
2703 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002704 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002705 goto return_prx_cond;
2706 }
2707
2708 /*
2709 * 3: Maybe we have to copy the original REQURI for the logs ?
2710 * Note: we cannot log anymore if the request has been
2711 * classified as invalid.
2712 */
2713 if (unlikely(s->logs.logwait & LW_REQ)) {
2714 /* we have a complete HTTP request that we must log */
2715 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2716 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002717
Willy Tarreau59234e92008-11-30 23:51:27 +01002718 if (urilen >= REQURI_LEN)
2719 urilen = REQURI_LEN - 1;
2720 memcpy(txn->uri, &req->data[msg->som], urilen);
2721 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002722
Willy Tarreau59234e92008-11-30 23:51:27 +01002723 if (!(s->logs.logwait &= ~LW_REQ))
2724 s->do_log(s);
2725 } else {
2726 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002727 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002728 }
Willy Tarreau06619262006-12-17 08:37:22 +01002729
Willy Tarreau59234e92008-11-30 23:51:27 +01002730 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002731 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2732 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002733
Willy Tarreau5b154472009-12-21 20:11:07 +01002734 /* ... and check if the request is HTTP/1.1 or above */
2735 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002736 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2737 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2738 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002739 txn->flags |= TX_REQ_VER_11;
2740
2741 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002742 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002743
Willy Tarreau88d349d2010-01-25 12:15:43 +01002744 /* if the frontend has "option http-use-proxy-header", we'll check if
2745 * we have what looks like a proxied connection instead of a connection,
2746 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2747 * Note that this is *not* RFC-compliant, however browsers and proxies
2748 * happen to do that despite being non-standard :-(
2749 * We consider that a request not beginning with either '/' or '*' is
2750 * a proxied connection, which covers both "scheme://location" and
2751 * CONNECT ip:port.
2752 */
2753 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2754 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2755 txn->flags |= TX_USE_PX_CONN;
2756
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002757 /* transfer length unknown*/
2758 txn->flags &= ~TX_REQ_XFER_LEN;
2759
Willy Tarreau59234e92008-11-30 23:51:27 +01002760 /* 5: we may need to capture headers */
2761 if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002762 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002763 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002764
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002765 /* 6: determine the transfer-length.
2766 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2767 * the presence of a message-body in a REQUEST and its transfer length
2768 * must be determined that way (in order of precedence) :
2769 * 1. The presence of a message-body in a request is signaled by the
2770 * inclusion of a Content-Length or Transfer-Encoding header field
2771 * in the request's header fields. When a request message contains
2772 * both a message-body of non-zero length and a method that does
2773 * not define any semantics for that request message-body, then an
2774 * origin server SHOULD either ignore the message-body or respond
2775 * with an appropriate error message (e.g., 413). A proxy or
2776 * gateway, when presented the same request, SHOULD either forward
2777 * the request inbound with the message- body or ignore the
2778 * message-body when determining a response.
2779 *
2780 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2781 * and the "chunked" transfer-coding (Section 6.2) is used, the
2782 * transfer-length is defined by the use of this transfer-coding.
2783 * If a Transfer-Encoding header field is present and the "chunked"
2784 * transfer-coding is not present, the transfer-length is defined
2785 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002786 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002787 * 3. If a Content-Length header field is present, its decimal value in
2788 * OCTETs represents both the entity-length and the transfer-length.
2789 * If a message is received with both a Transfer-Encoding header
2790 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002791 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002792 * 4. By the server closing the connection. (Closing the connection
2793 * cannot be used to indicate the end of a request body, since that
2794 * would leave no possibility for the server to send back a response.)
2795 *
2796 * Whenever a transfer-coding is applied to a message-body, the set of
2797 * transfer-codings MUST include "chunked", unless the message indicates
2798 * it is terminated by closing the connection. When the "chunked"
2799 * transfer-coding is used, it MUST be the last transfer-coding applied
2800 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002801 */
2802
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002803 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002804 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002805 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002806 while ((txn->flags & TX_REQ_VER_11) &&
2807 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002808 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2809 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2810 else if (txn->flags & TX_REQ_TE_CHNK) {
2811 /* bad transfer-encoding (chunked followed by something else) */
2812 use_close_only = 1;
2813 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2814 break;
2815 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002816 }
2817
Willy Tarreau32b47f42009-10-18 20:55:02 +02002818 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002819 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002820 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2821 signed long long cl;
2822
Willy Tarreauad14f752011-09-02 20:33:27 +02002823 if (!ctx.vlen) {
2824 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002825 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002826 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002827
Willy Tarreauad14f752011-09-02 20:33:27 +02002828 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2829 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002830 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002831 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002832
Willy Tarreauad14f752011-09-02 20:33:27 +02002833 if (cl < 0) {
2834 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002835 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002836 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002837
Willy Tarreauad14f752011-09-02 20:33:27 +02002838 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) {
2839 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002840 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002841 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002842
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002843 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002844 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002845 }
2846
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002847 /* bodyless requests have a known length */
2848 if (!use_close_only)
2849 txn->flags |= TX_REQ_XFER_LEN;
2850
Willy Tarreaud787e662009-07-07 10:14:51 +02002851 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002852 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002853 req->analyse_exp = TICK_ETERNITY;
2854 return 1;
2855
2856 return_bad_req:
2857 /* We centralize bad requests processing here */
2858 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2859 /* we detected a parsing error. We want to archive this request
2860 * in the dedicated proxy area for later troubleshooting.
2861 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002862 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002863 }
2864
2865 txn->req.msg_state = HTTP_MSG_ERROR;
2866 txn->status = 400;
2867 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002868
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002869 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002870 if (s->listener->counters)
2871 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002872
2873 return_prx_cond:
2874 if (!(s->flags & SN_ERR_MASK))
2875 s->flags |= SN_ERR_PRXCOND;
2876 if (!(s->flags & SN_FINST_MASK))
2877 s->flags |= SN_FINST_R;
2878
2879 req->analysers = 0;
2880 req->analyse_exp = TICK_ETERNITY;
2881 return 0;
2882}
2883
Cyril Bonté70be45d2010-10-12 00:14:35 +02002884/* We reached the stats page through a POST request.
2885 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002886 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002887 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002888int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002889{
Cyril Bonté70be45d2010-10-12 00:14:35 +02002890 struct proxy *px;
2891 struct server *sv;
2892
2893 char *backend = NULL;
2894 int action = 0;
2895
2896 char *first_param, *cur_param, *next_param, *end_params;
2897
2898 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002899 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002900
2901 cur_param = next_param = end_params;
2902
Cyril Bonté23b39d92011-02-10 22:54:44 +01002903 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002904 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002905 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002906 return 1;
2907 }
2908 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002909 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002910 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002911 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002912 }
2913
2914 *end_params = '\0';
2915
Willy Tarreau295a8372011-03-10 11:25:07 +01002916 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002917
2918 /*
2919 * Parse the parameters in reverse order to only store the last value.
2920 * From the html form, the backend and the action are at the end.
2921 */
2922 while (cur_param > first_param) {
2923 char *key, *value;
2924
2925 cur_param--;
2926 if ((*cur_param == '&') || (cur_param == first_param)) {
2927 /* Parse the key */
2928 key = cur_param;
2929 if (cur_param != first_param) {
2930 /* delimit the string for the next loop */
2931 *key++ = '\0';
2932 }
2933
2934 /* Parse the value */
2935 value = key;
2936 while (*value != '\0' && *value != '=') {
2937 value++;
2938 }
2939 if (*value == '=') {
2940 /* Ok, a value is found, we can mark the end of the key */
2941 *value++ = '\0';
2942 }
2943
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002944 if (!url_decode(key) || !url_decode(value))
2945 break;
2946
Cyril Bonté70be45d2010-10-12 00:14:35 +02002947 /* Now we can check the key to see what to do */
2948 if (!backend && strcmp(key, "b") == 0) {
2949 backend = value;
2950 }
2951 else if (!action && strcmp(key, "action") == 0) {
2952 if (strcmp(value, "disable") == 0) {
2953 action = 1;
2954 }
2955 else if (strcmp(value, "enable") == 0) {
2956 action = 2;
2957 } else {
2958 /* unknown action, no need to continue */
2959 break;
2960 }
2961 }
2962 else if (strcmp(key, "s") == 0) {
2963 if (backend && action && get_backend_server(backend, value, &px, &sv)) {
2964 switch (action) {
2965 case 1:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002966 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002967 /* Not already in maintenance, we can change the server state */
2968 sv->state |= SRV_MAINTAIN;
2969 set_server_down(sv);
Willy Tarreau295a8372011-03-10 11:25:07 +01002970 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002971 }
2972 break;
2973 case 2:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002974 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002975 /* Already in maintenance, we can change the server state */
2976 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002977 sv->health = sv->rise; /* up, but will fall down at first failure */
Willy Tarreau295a8372011-03-10 11:25:07 +01002978 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002979 }
2980 break;
2981 }
2982 }
2983 }
2984 next_param = cur_param;
2985 }
2986 }
Cyril Bonté23b39d92011-02-10 22:54:44 +01002987 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002988}
2989
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002990/* returns a pointer to the first rule which forbids access (deny or http_auth),
2991 * or NULL if everything's OK.
2992 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002993static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002994http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2995{
Willy Tarreauff011f22011-01-06 17:51:27 +01002996 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002997
Willy Tarreauff011f22011-01-06 17:51:27 +01002998 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002999 int ret = 1;
3000
Willy Tarreauff011f22011-01-06 17:51:27 +01003001 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003002 continue;
3003
3004 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01003005 if (rule->cond) {
3006 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003007 ret = acl_pass(ret);
3008
Willy Tarreauff011f22011-01-06 17:51:27 +01003009 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003010 ret = !ret;
3011 }
3012
3013 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003014 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003015 return NULL; /* no problem */
3016 else
Willy Tarreauff011f22011-01-06 17:51:27 +01003017 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003018 }
3019 }
3020 return NULL;
3021}
3022
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023/* This stream analyser runs all HTTP request processing which is common to
3024 * frontends and backends, which means blocking ACLs, filters, connection-close,
3025 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003026 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003027 * either needs more data or wants to immediately abort the request (eg: deny,
3028 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003029 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003030int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003031{
Willy Tarreaud787e662009-07-07 10:14:51 +02003032 struct http_txn *txn = &s->txn;
3033 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003034 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003035 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003036 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003037 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003038 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003039
Willy Tarreau655dce92009-11-08 13:10:58 +01003040 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003041 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003042 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003043 return 0;
3044 }
3045
Willy Tarreau3a816292009-07-07 10:55:49 +02003046 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003047 req->analyse_exp = TICK_ETERNITY;
3048
3049 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3050 now_ms, __FUNCTION__,
3051 s,
3052 req,
3053 req->rex, req->wex,
3054 req->flags,
3055 req->l,
3056 req->analysers);
3057
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003058 /* first check whether we have some ACLs set to block this request */
3059 list_for_each_entry(cond, &px->block_cond, list) {
3060 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003061
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003062 ret = acl_pass(ret);
3063 if (cond->pol == ACL_COND_UNLESS)
3064 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003065
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003066 if (ret) {
3067 txn->status = 403;
3068 /* let's log the request time */
3069 s->logs.tv_request = now;
3070 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003071 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003072 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003073 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003074 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003075
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003076 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003077 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003078
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003079 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003080 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003081 do_stats = stats_check_uri(s->rep->prod, txn, px);
3082 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003083 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 +01003084 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003085 else
3086 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003087
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003088 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003089 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003090 txn->status = 403;
3091 s->logs.tv_request = now;
3092 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003093 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003094 s->fe->fe_counters.denied_req++;
3095 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3096 s->be->be_counters.denied_req++;
3097 if (s->listener->counters)
3098 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003099 goto return_prx_cond;
3100 }
3101
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003102 /* try headers filters */
3103 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003104 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003105 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003106
Willy Tarreau59234e92008-11-30 23:51:27 +01003107 /* has the request been denied ? */
3108 if (txn->flags & TX_CLDENY) {
3109 /* no need to go further */
3110 txn->status = 403;
3111 /* let's log the request time */
3112 s->logs.tv_request = now;
3113 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003114 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003115 goto return_prx_cond;
3116 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003117
3118 /* When a connection is tarpitted, we use the tarpit timeout,
3119 * which may be the same as the connect timeout if unspecified.
3120 * If unset, then set it to zero because we really want it to
3121 * eventually expire. We build the tarpit as an analyser.
3122 */
3123 if (txn->flags & TX_CLTARPIT) {
3124 buffer_erase(s->req);
3125 /* wipe the request out so that we can drop the connection early
3126 * if the client closes first.
3127 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003128 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003129 req->analysers = 0; /* remove switching rules etc... */
3130 req->analysers |= AN_REQ_HTTP_TARPIT;
3131 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3132 if (!req->analyse_exp)
3133 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003134 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003135 return 1;
3136 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003137 }
Willy Tarreau06619262006-12-17 08:37:22 +01003138
Willy Tarreau5b154472009-12-21 20:11:07 +01003139 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3140 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003141 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3142 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003143 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3144 * avoid to redo the same work if FE and BE have the same settings (common).
3145 * The method consists in checking if options changed between the two calls
3146 * (implying that either one is non-null, or one of them is non-null and we
3147 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003148 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003149
Willy Tarreaudc008c52010-02-01 16:20:08 +01003150 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3151 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3152 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3153 (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 +01003154 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003155
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003156 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3157 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003158 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003159 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3160 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003161 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003162 tmp = TX_CON_WANT_CLO;
3163
Willy Tarreau5b154472009-12-21 20:11:07 +01003164 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3165 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003166
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003167 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3168 /* parse the Connection header and possibly clean it */
3169 int to_del = 0;
3170 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003171 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3172 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003173 to_del |= 2; /* remove "keep-alive" */
3174 if (!(txn->flags & TX_REQ_VER_11))
3175 to_del |= 1; /* remove "close" */
3176 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003177 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003178
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003179 /* check if client or config asks for explicit close in KAL/SCL */
3180 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3181 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3182 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
3183 (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 +01003184 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003185 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
3186 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003187 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3188 }
Willy Tarreau78599912009-10-17 20:12:21 +02003189
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003190 /* we can be blocked here because the request needs to be authenticated,
3191 * either to pass or to access stats.
3192 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003193 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003194 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01003195 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003196
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003197 if (!realm)
3198 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3199
Willy Tarreau844a7e72010-01-31 21:46:18 +01003200 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003201 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
3202 txn->status = 401;
3203 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003204 /* on 401 we still count one error, because normal browsing
3205 * won't significantly increase the counter but brute force
3206 * attempts will.
3207 */
3208 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003209 goto return_prx_cond;
3210 }
3211
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003212 /* add request headers from the rule sets in the same order */
3213 list_for_each_entry(wl, &px->req_add, list) {
3214 if (wl->cond) {
3215 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
3216 ret = acl_pass(ret);
3217 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3218 ret = !ret;
3219 if (!ret)
3220 continue;
3221 }
3222
3223 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
3224 goto return_bad_req;
3225 }
3226
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003227 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003228 struct stats_admin_rule *stats_admin_rule;
3229
3230 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003231 * FIXME!!! that one is rather dangerous, we want to
3232 * make it follow standard rules (eg: clear req->analysers).
3233 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003234
Cyril Bonté474be412010-10-12 00:14:36 +02003235 /* now check whether we have some admin rules for this request */
3236 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3237 int ret = 1;
3238
3239 if (stats_admin_rule->cond) {
3240 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
3241 ret = acl_pass(ret);
3242 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3243 ret = !ret;
3244 }
3245
3246 if (ret) {
3247 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003248 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003249 break;
3250 }
3251 }
3252
Cyril Bonté70be45d2010-10-12 00:14:35 +02003253 /* Was the status page requested with a POST ? */
3254 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003255 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003256 if (msg->msg_state < HTTP_MSG_100_SENT) {
3257 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3258 * send an HTTP/1.1 100 Continue intermediate response.
3259 */
3260 if (txn->flags & TX_REQ_VER_11) {
3261 struct hdr_ctx ctx;
3262 ctx.idx = 0;
3263 /* Expect is allowed in 1.1, look for it */
3264 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3265 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3266 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3267 }
3268 }
3269 msg->msg_state = HTTP_MSG_100_SENT;
3270 s->logs.tv_request = now; /* update the request timer to reflect full request */
3271 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003272 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003273 /* we need more data */
3274 req->analysers |= an_bit;
3275 buffer_dont_connect(req);
3276 return 0;
3277 }
Cyril Bonté474be412010-10-12 00:14:36 +02003278 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003279 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003280 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003281 }
3282
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003283 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003284 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003285 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003286 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003287 s->rep->prod->applet.private = s;
3288 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003289 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003290 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3291 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003292
3293 return 0;
3294
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003295 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003296
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003297 /* check whether we have some ACLs set to redirect this request */
3298 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003299 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003300
Willy Tarreauf285f542010-01-03 20:03:03 +01003301 if (rule->cond) {
3302 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3303 ret = acl_pass(ret);
3304 if (rule->cond->pol == ACL_COND_UNLESS)
3305 ret = !ret;
3306 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003307
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003308 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003309 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003310 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003311
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003312 /* build redirect message */
3313 switch(rule->code) {
3314 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003315 msg_fmt = HTTP_303;
3316 break;
3317 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003318 msg_fmt = HTTP_301;
3319 break;
3320 case 302:
3321 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003322 msg_fmt = HTTP_302;
3323 break;
3324 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003325
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003326 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003327 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003328
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003329 switch(rule->type) {
3330 case REDIRECT_TYPE_PREFIX: {
3331 const char *path;
3332 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003333
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003334 path = http_get_path(txn);
3335 /* build message using path */
3336 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003337 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003338 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3339 int qs = 0;
3340 while (qs < pathlen) {
3341 if (path[qs] == '?') {
3342 pathlen = qs;
3343 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003344 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003345 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003346 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003347 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003348 } else {
3349 path = "/";
3350 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003351 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003352
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003353 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003354 goto return_bad_req;
3355
3356 /* add prefix. Note that if prefix == "/", we don't want to
3357 * add anything, otherwise it makes it hard for the user to
3358 * configure a self-redirection.
3359 */
3360 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003361 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3362 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003363 }
3364
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003365 /* add path */
3366 memcpy(rdr.str + rdr.len, path, pathlen);
3367 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003368
3369 /* append a slash at the end of the location is needed and missing */
3370 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3371 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3372 if (rdr.len > rdr.size - 5)
3373 goto return_bad_req;
3374 rdr.str[rdr.len] = '/';
3375 rdr.len++;
3376 }
3377
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003378 break;
3379 }
3380 case REDIRECT_TYPE_LOCATION:
3381 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003382 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003383 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003384
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003385 /* add location */
3386 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3387 rdr.len += rule->rdr_len;
3388 break;
3389 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003390
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003391 if (rule->cookie_len) {
3392 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3393 rdr.len += 14;
3394 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3395 rdr.len += rule->cookie_len;
3396 memcpy(rdr.str + rdr.len, "\r\n", 2);
3397 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003398 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003399
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003400 /* add end of headers and the keep-alive/close status.
3401 * We may choose to set keep-alive if the Location begins
3402 * with a slash, because the client will come back to the
3403 * same server.
3404 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003405 txn->status = rule->code;
3406 /* let's log the request time */
3407 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003408
3409 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3410 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003411 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003412 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3413 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3414 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003415 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003416 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3417 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3418 rdr.len += 30;
3419 } else {
3420 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3421 rdr.len += 24;
3422 }
Willy Tarreau75661452010-01-10 10:35:01 +01003423 }
3424 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3425 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003426 buffer_write(req->prod->ob, rdr.str, rdr.len);
3427 /* "eat" the request */
3428 buffer_ignore(req, msg->sov - msg->som);
3429 msg->som = msg->sov;
3430 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003431 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3432 txn->req.msg_state = HTTP_MSG_CLOSED;
3433 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003434 break;
3435 } else {
3436 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003437 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3438 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3439 rdr.len += 29;
3440 } else {
3441 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3442 rdr.len += 23;
3443 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003444 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003445 goto return_prx_cond;
3446 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003447 }
3448 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003449
Willy Tarreau2be39392010-01-03 17:24:51 +01003450 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3451 * If this happens, then the data will not come immediately, so we must
3452 * send all what we have without waiting. Note that due to the small gain
3453 * in waiting for the body of the request, it's easier to simply put the
3454 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3455 * itself once used.
3456 */
3457 req->flags |= BF_SEND_DONTWAIT;
3458
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003459 /* that's OK for us now, let's move on to next analysers */
3460 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003461
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003462 return_bad_req:
3463 /* We centralize bad requests processing here */
3464 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3465 /* we detected a parsing error. We want to archive this request
3466 * in the dedicated proxy area for later troubleshooting.
3467 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003468 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003469 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003470
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003471 txn->req.msg_state = HTTP_MSG_ERROR;
3472 txn->status = 400;
3473 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003474
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003475 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003476 if (s->listener->counters)
3477 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003478
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003479 return_prx_cond:
3480 if (!(s->flags & SN_ERR_MASK))
3481 s->flags |= SN_ERR_PRXCOND;
3482 if (!(s->flags & SN_FINST_MASK))
3483 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003484
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003485 req->analysers = 0;
3486 req->analyse_exp = TICK_ETERNITY;
3487 return 0;
3488}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003489
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003490/* This function performs all the processing enabled for the current request.
3491 * It returns 1 if the processing can continue on next analysers, or zero if it
3492 * needs more data, encounters an error, or wants to immediately abort the
3493 * request. It relies on buffers flags, and updates s->req->analysers.
3494 */
3495int http_process_request(struct session *s, struct buffer *req, int an_bit)
3496{
3497 struct http_txn *txn = &s->txn;
3498 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003499
Willy Tarreau655dce92009-11-08 13:10:58 +01003500 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003501 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003502 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003503 return 0;
3504 }
3505
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003506 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3507 now_ms, __FUNCTION__,
3508 s,
3509 req,
3510 req->rex, req->wex,
3511 req->flags,
3512 req->l,
3513 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003514
Willy Tarreau59234e92008-11-30 23:51:27 +01003515 /*
3516 * Right now, we know that we have processed the entire headers
3517 * and that unwanted requests have been filtered out. We can do
3518 * whatever we want with the remaining request. Also, now we
3519 * may have separate values for ->fe, ->be.
3520 */
Willy Tarreau06619262006-12-17 08:37:22 +01003521
Willy Tarreau59234e92008-11-30 23:51:27 +01003522 /*
3523 * If HTTP PROXY is set we simply get remote server address
3524 * parsing incoming request.
3525 */
3526 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau6471afb2011-09-23 10:54:59 +02003527 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003528 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003529
Willy Tarreau59234e92008-11-30 23:51:27 +01003530 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003531 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003532 * Note that doing so might move headers in the request, but
3533 * the fields will stay coherent and the URI will not move.
3534 * This should only be performed in the backend.
3535 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003536 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003537 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3538 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003539
Willy Tarreau59234e92008-11-30 23:51:27 +01003540 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003541 * 8: the appsession cookie was looked up very early in 1.2,
3542 * so let's do the same now.
3543 */
3544
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003545 /* It needs to look into the URI unless persistence must be ignored */
3546 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003547 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003548 }
3549
3550 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003551 * 9: add X-Forwarded-For if either the frontend or the backend
3552 * asks for it.
3553 */
3554 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003555 struct hdr_ctx ctx = { .idx = 0 };
3556
3557 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Sagi Bashari1611e2d2011-10-08 22:48:48 +02003558 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 +02003559 /* The header is set to be added only if none is present
3560 * and we found it, so don't do anything.
3561 */
3562 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003563 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003564 /* Add an X-Forwarded-For header unless the source IP is
3565 * in the 'except' network range.
3566 */
3567 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003568 (((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 +01003569 != s->fe->except_net.s_addr) &&
3570 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003571 (((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 +01003572 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003573 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003574 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003575 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003576
3577 /* Note: we rely on the backend to get the header name to be used for
3578 * x-forwarded-for, because the header is really meant for the backends.
3579 * However, if the backend did not specify any option, we have to rely
3580 * on the frontend's header name.
3581 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003582 if (s->be->fwdfor_hdr_len) {
3583 len = s->be->fwdfor_hdr_len;
3584 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003585 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003586 len = s->fe->fwdfor_hdr_len;
3587 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003588 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003589 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003590
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003591 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003592 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003593 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003594 }
3595 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003596 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003597 /* FIXME: for the sake of completeness, we should also support
3598 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003599 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003600 int len;
3601 char pn[INET6_ADDRSTRLEN];
3602 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003603 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003604 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003605
Willy Tarreau59234e92008-11-30 23:51:27 +01003606 /* Note: we rely on the backend to get the header name to be used for
3607 * x-forwarded-for, because the header is really meant for the backends.
3608 * However, if the backend did not specify any option, we have to rely
3609 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003610 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003611 if (s->be->fwdfor_hdr_len) {
3612 len = s->be->fwdfor_hdr_len;
3613 memcpy(trash, s->be->fwdfor_hdr_name, len);
3614 } else {
3615 len = s->fe->fwdfor_hdr_len;
3616 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003617 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003618 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003619
Willy Tarreau59234e92008-11-30 23:51:27 +01003620 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003621 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003622 goto return_bad_req;
3623 }
3624 }
3625
3626 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003627 * 10: add X-Original-To if either the frontend or the backend
3628 * asks for it.
3629 */
3630 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3631
3632 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003633 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003634 /* Add an X-Original-To header unless the destination IP is
3635 * in the 'except' network range.
3636 */
3637 if (!(s->flags & SN_FRT_ADDR_SET))
3638 get_frt_addr(s);
3639
Willy Tarreau6471afb2011-09-23 10:54:59 +02003640 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003641 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003642 (((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 +02003643 != s->fe->except_to.s_addr) &&
3644 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003645 (((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 +02003646 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003647 int len;
3648 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003649 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003650
3651 /* Note: we rely on the backend to get the header name to be used for
3652 * x-original-to, because the header is really meant for the backends.
3653 * However, if the backend did not specify any option, we have to rely
3654 * on the frontend's header name.
3655 */
3656 if (s->be->orgto_hdr_len) {
3657 len = s->be->orgto_hdr_len;
3658 memcpy(trash, s->be->orgto_hdr_name, len);
3659 } else {
3660 len = s->fe->orgto_hdr_len;
3661 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003662 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003663 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3664
3665 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003666 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003667 goto return_bad_req;
3668 }
3669 }
3670 }
3671
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003672 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3673 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003674 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003675 unsigned int want_flags = 0;
3676
3677 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003678 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3679 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3680 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003681 want_flags |= TX_CON_CLO_SET;
3682 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003683 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3684 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3685 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003686 want_flags |= TX_CON_KAL_SET;
3687 }
3688
3689 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3690 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003691 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003692
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003693
Willy Tarreau522d6c02009-12-06 18:49:18 +01003694 /* If we have no server assigned yet and we're balancing on url_param
3695 * with a POST request, we may be interested in checking the body for
3696 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003697 */
3698 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3699 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003700 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003701 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003702 buffer_dont_connect(req);
3703 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003704 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003705
Willy Tarreau5e205522011-12-17 16:34:27 +01003706 if (txn->flags & TX_REQ_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003707 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003708#ifdef TCP_QUICKACK
3709 /* We expect some data from the client. Unless we know for sure
3710 * we already have a full request, we have to re-enable quick-ack
3711 * in case we previously disabled it, otherwise we might cause
3712 * the client to delay further data.
3713 */
3714 if ((s->listener->options & LI_O_NOQUICKACK) &&
3715 ((txn->flags & TX_REQ_TE_CHNK) ||
3716 (msg->body_len > req->l - txn->req.eoh - 2)))
3717 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3718#endif
3719 }
Willy Tarreau03945942009-12-22 16:50:27 +01003720
Willy Tarreau59234e92008-11-30 23:51:27 +01003721 /*************************************************************
3722 * OK, that's finished for the headers. We have done what we *
3723 * could. Let's switch to the DATA state. *
3724 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003725 req->analyse_exp = TICK_ETERNITY;
3726 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003727
Willy Tarreau59234e92008-11-30 23:51:27 +01003728 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003729 /* OK let's go on with the BODY now */
3730 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003731
Willy Tarreau59234e92008-11-30 23:51:27 +01003732 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003733 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003734 /* we detected a parsing error. We want to archive this request
3735 * in the dedicated proxy area for later troubleshooting.
3736 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003737 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003738 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003739
Willy Tarreau59234e92008-11-30 23:51:27 +01003740 txn->req.msg_state = HTTP_MSG_ERROR;
3741 txn->status = 400;
3742 req->analysers = 0;
3743 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003744
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003745 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003746 if (s->listener->counters)
3747 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003748
Willy Tarreau59234e92008-11-30 23:51:27 +01003749 if (!(s->flags & SN_ERR_MASK))
3750 s->flags |= SN_ERR_PRXCOND;
3751 if (!(s->flags & SN_FINST_MASK))
3752 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003753 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003754}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003755
Willy Tarreau60b85b02008-11-30 23:28:40 +01003756/* This function is an analyser which processes the HTTP tarpit. It always
3757 * returns zero, at the beginning because it prevents any other processing
3758 * from occurring, and at the end because it terminates the request.
3759 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003760int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003761{
3762 struct http_txn *txn = &s->txn;
3763
3764 /* This connection is being tarpitted. The CLIENT side has
3765 * already set the connect expiration date to the right
3766 * timeout. We just have to check that the client is still
3767 * there and that the timeout has not expired.
3768 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003769 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003770 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3771 !tick_is_expired(req->analyse_exp, now_ms))
3772 return 0;
3773
3774 /* We will set the queue timer to the time spent, just for
3775 * logging purposes. We fake a 500 server error, so that the
3776 * attacker will not suspect his connection has been tarpitted.
3777 * It will not cause trouble to the logs because we can exclude
3778 * the tarpitted connections by filtering on the 'PT' status flags.
3779 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003780 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3781
3782 txn->status = 500;
3783 if (req->flags != BF_READ_ERROR)
3784 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3785
3786 req->analysers = 0;
3787 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003788
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003789 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003790 if (s->listener->counters)
3791 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003792
Willy Tarreau60b85b02008-11-30 23:28:40 +01003793 if (!(s->flags & SN_ERR_MASK))
3794 s->flags |= SN_ERR_PRXCOND;
3795 if (!(s->flags & SN_FINST_MASK))
3796 s->flags |= SN_FINST_T;
3797 return 0;
3798}
3799
Willy Tarreaud34af782008-11-30 23:36:37 +01003800/* This function is an analyser which processes the HTTP request body. It looks
3801 * for parameters to be used for the load balancing algorithm (url_param). It
3802 * must only be called after the standard HTTP request processing has occurred,
3803 * because it expects the request to be parsed. It returns zero if it needs to
3804 * read more data, or 1 once it has completed its analysis.
3805 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003806int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003807{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003808 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003809 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003810 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003811
3812 /* We have to parse the HTTP request body to find any required data.
3813 * "balance url_param check_post" should have been the only way to get
3814 * into this. We were brought here after HTTP header analysis, so all
3815 * related structures are ready.
3816 */
3817
Willy Tarreau522d6c02009-12-06 18:49:18 +01003818 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3819 goto missing_data;
3820
3821 if (msg->msg_state < HTTP_MSG_100_SENT) {
3822 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3823 * send an HTTP/1.1 100 Continue intermediate response.
3824 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003825 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003826 struct hdr_ctx ctx;
3827 ctx.idx = 0;
3828 /* Expect is allowed in 1.1, look for it */
3829 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3830 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3831 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3832 }
3833 }
3834 msg->msg_state = HTTP_MSG_100_SENT;
3835 }
3836
3837 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003838 /* we have msg->col and msg->sov which both point to the first
3839 * byte of message body. msg->som still points to the beginning
3840 * of the message. We must save the body in req->lr because it
3841 * survives buffer re-alignments.
3842 */
3843 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003844 if (txn->flags & TX_REQ_TE_CHNK)
3845 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3846 else
3847 msg->msg_state = HTTP_MSG_DATA;
3848 }
3849
3850 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003851 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003852 * set ->sov and ->lr to point to the body and switch to DATA or
3853 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003854 */
3855 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003856
Willy Tarreau115acb92009-12-26 13:56:06 +01003857 if (!ret)
3858 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003859 else if (ret < 0) {
3860 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003861 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003862 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003863 }
3864
Willy Tarreaud98cf932009-12-27 22:54:55 +01003865 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003866 * We have the first non-header byte in msg->col, which is either the
3867 * beginning of the chunk size or of the data. The first data byte is in
3868 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3869 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003870 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003871
Willy Tarreau124d9912011-03-01 20:30:48 +01003872 if (msg->body_len < limit)
3873 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003874
Willy Tarreau7c96f672009-12-27 22:47:25 +01003875 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003876 goto http_end;
3877
3878 missing_data:
3879 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003880 if (req->flags & BF_FULL) {
3881 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003882 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003883 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003884
Willy Tarreau522d6c02009-12-06 18:49:18 +01003885 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3886 txn->status = 408;
3887 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003888
3889 if (!(s->flags & SN_ERR_MASK))
3890 s->flags |= SN_ERR_CLITO;
3891 if (!(s->flags & SN_FINST_MASK))
3892 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003893 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003894 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003895
3896 /* we get here if we need to wait for more data */
3897 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003898 /* Not enough data. We'll re-use the http-request
3899 * timeout here. Ideally, we should set the timeout
3900 * relative to the accept() date. We just set the
3901 * request timeout once at the beginning of the
3902 * request.
3903 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003904 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003905 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003906 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003907 return 0;
3908 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003909
3910 http_end:
3911 /* The situation will not evolve, so let's give up on the analysis. */
3912 s->logs.tv_request = now; /* update the request timer to reflect full request */
3913 req->analysers &= ~an_bit;
3914 req->analyse_exp = TICK_ETERNITY;
3915 return 1;
3916
3917 return_bad_req: /* let's centralize all bad requests */
3918 txn->req.msg_state = HTTP_MSG_ERROR;
3919 txn->status = 400;
3920 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3921
Willy Tarreau79ebac62010-06-07 13:47:49 +02003922 if (!(s->flags & SN_ERR_MASK))
3923 s->flags |= SN_ERR_PRXCOND;
3924 if (!(s->flags & SN_FINST_MASK))
3925 s->flags |= SN_FINST_R;
3926
Willy Tarreau522d6c02009-12-06 18:49:18 +01003927 return_err_msg:
3928 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003929 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003930 if (s->listener->counters)
3931 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003932 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003933}
3934
Willy Tarreau610ecce2010-01-04 21:15:02 +01003935/* Terminate current transaction and prepare a new one. This is very tricky
3936 * right now but it works.
3937 */
3938void http_end_txn_clean_session(struct session *s)
3939{
3940 /* FIXME: We need a more portable way of releasing a backend's and a
3941 * server's connections. We need a safer way to reinitialize buffer
3942 * flags. We also need a more accurate method for computing per-request
3943 * data.
3944 */
3945 http_silent_debug(__LINE__, s);
3946
3947 s->req->cons->flags |= SI_FL_NOLINGER;
3948 s->req->cons->shutr(s->req->cons);
3949 s->req->cons->shutw(s->req->cons);
3950
3951 http_silent_debug(__LINE__, s);
3952
3953 if (s->flags & SN_BE_ASSIGNED)
3954 s->be->beconn--;
3955
3956 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3957 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003958 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003959
3960 if (s->txn.status) {
3961 int n;
3962
3963 n = s->txn.status / 100;
3964 if (n < 1 || n > 5)
3965 n = 0;
3966
3967 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003968 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003969
Willy Tarreau24657792010-02-26 10:30:28 +01003970 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003971 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003972 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003973 }
3974
3975 /* don't count other requests' data */
3976 s->logs.bytes_in -= s->req->l - s->req->send_max;
3977 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3978
3979 /* let's do a final log if we need it */
3980 if (s->logs.logwait &&
3981 !(s->flags & SN_MONITOR) &&
3982 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3983 s->do_log(s);
3984 }
3985
3986 s->logs.accept_date = date; /* user-visible date for logging */
3987 s->logs.tv_accept = now; /* corrected date for internal use */
3988 tv_zero(&s->logs.tv_request);
3989 s->logs.t_queue = -1;
3990 s->logs.t_connect = -1;
3991 s->logs.t_data = -1;
3992 s->logs.t_close = 0;
3993 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3994 s->logs.srv_queue_size = 0; /* we will get this number soon */
3995
3996 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3997 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3998
3999 if (s->pend_pos)
4000 pendconn_free(s->pend_pos);
4001
Willy Tarreau827aee92011-03-10 16:55:02 +01004002 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 if (s->flags & SN_CURR_SESS) {
4004 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01004005 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004006 }
Willy Tarreau827aee92011-03-10 16:55:02 +01004007 if (may_dequeue_tasks(target_srv(&s->target), s->be))
4008 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 }
4010
4011 if (unlikely(s->srv_conn))
4012 sess_change_server(s, NULL);
Willy Tarreau9e000c62011-03-10 14:03:36 +01004013 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004014
4015 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
4016 s->req->cons->fd = -1; /* just to help with debugging */
4017 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004018 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 s->req->cons->err_loc = NULL;
4020 s->req->cons->exp = TICK_ETERNITY;
4021 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02004022 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
4023 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 +02004024 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 +01004025 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
4026 s->txn.meth = 0;
4027 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004028 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004029 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004030 s->req->cons->flags |= SI_FL_INDEP_STR;
4031
Willy Tarreau96e31212011-05-30 18:10:30 +02004032 if (s->fe->options2 & PR_O2_NODELAY) {
4033 s->req->flags |= BF_NEVER_WAIT;
4034 s->rep->flags |= BF_NEVER_WAIT;
4035 }
4036
Willy Tarreau610ecce2010-01-04 21:15:02 +01004037 /* if the request buffer is not empty, it means we're
4038 * about to process another request, so send pending
4039 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004040 * Just don't do this if the buffer is close to be full,
4041 * because the request will wait for it to flush a little
4042 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004043 */
Willy Tarreau065e8332010-01-08 00:30:20 +01004044 if (s->req->l > s->req->send_max) {
4045 if (s->rep->send_max &&
4046 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01004047 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
4048 s->rep->flags |= BF_EXPECT_MORE;
4049 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004050
4051 /* we're removing the analysers, we MUST re-enable events detection */
4052 buffer_auto_read(s->req);
4053 buffer_auto_close(s->req);
4054 buffer_auto_read(s->rep);
4055 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004056
4057 /* make ->lr point to the first non-forwarded byte */
4058 s->req->lr = s->req->w + s->req->send_max;
4059 if (s->req->lr >= s->req->data + s->req->size)
4060 s->req->lr -= s->req->size;
4061 s->rep->lr = s->rep->w + s->rep->send_max;
4062 if (s->rep->lr >= s->rep->data + s->rep->size)
4063 s->rep->lr -= s->req->size;
4064
Willy Tarreau342b11c2010-11-24 16:22:09 +01004065 s->req->analysers = s->listener->analysers;
4066 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004067 s->rep->analysers = 0;
4068
4069 http_silent_debug(__LINE__, s);
4070}
4071
4072
4073/* This function updates the request state machine according to the response
4074 * state machine and buffer flags. It returns 1 if it changes anything (flag
4075 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4076 * it is only used to find when a request/response couple is complete. Both
4077 * this function and its equivalent should loop until both return zero. It
4078 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4079 */
4080int http_sync_req_state(struct session *s)
4081{
4082 struct buffer *buf = s->req;
4083 struct http_txn *txn = &s->txn;
4084 unsigned int old_flags = buf->flags;
4085 unsigned int old_state = txn->req.msg_state;
4086
4087 http_silent_debug(__LINE__, s);
4088 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4089 return 0;
4090
4091 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004092 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004093 * We can shut the read side unless we want to abort_on_close,
4094 * or we have a POST request. The issue with POST requests is
4095 * that some browsers still send a CRLF after the request, and
4096 * this CRLF must be read so that it does not remain in the kernel
4097 * buffers, otherwise a close could cause an RST on some systems
4098 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004099 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004100 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01004101 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004102
4103 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4104 goto wait_other_side;
4105
4106 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4107 /* The server has not finished to respond, so we
4108 * don't want to move in order not to upset it.
4109 */
4110 goto wait_other_side;
4111 }
4112
4113 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4114 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004115 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004116 txn->req.msg_state = HTTP_MSG_TUNNEL;
4117 goto wait_other_side;
4118 }
4119
4120 /* When we get here, it means that both the request and the
4121 * response have finished receiving. Depending on the connection
4122 * mode, we'll have to wait for the last bytes to leave in either
4123 * direction, and sometimes for a close to be effective.
4124 */
4125
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004126 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4127 /* Server-close mode : queue a connection close to the server */
4128 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004130 }
4131 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4132 /* Option forceclose is set, or either side wants to close,
4133 * let's enforce it now that we're not expecting any new
4134 * data to come. The caller knows the session is complete
4135 * once both states are CLOSED.
4136 */
4137 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004138 buffer_shutr_now(buf);
4139 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004140 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004141 }
4142 else {
4143 /* The last possible modes are keep-alive and tunnel. Since tunnel
4144 * mode does not set the body analyser, we can't reach this place
4145 * in tunnel mode, so we're left with keep-alive only.
4146 * This mode is currently not implemented, we switch to tunnel mode.
4147 */
4148 buffer_auto_read(buf);
4149 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004150 }
4151
4152 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4153 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004154 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4155
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156 if (!(buf->flags & BF_OUT_EMPTY)) {
4157 txn->req.msg_state = HTTP_MSG_CLOSING;
4158 goto http_msg_closing;
4159 }
4160 else {
4161 txn->req.msg_state = HTTP_MSG_CLOSED;
4162 goto http_msg_closed;
4163 }
4164 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004165 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004166 }
4167
4168 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4169 http_msg_closing:
4170 /* nothing else to forward, just waiting for the output buffer
4171 * to be empty and for the shutw_now to take effect.
4172 */
4173 if (buf->flags & BF_OUT_EMPTY) {
4174 txn->req.msg_state = HTTP_MSG_CLOSED;
4175 goto http_msg_closed;
4176 }
4177 else if (buf->flags & BF_SHUTW) {
4178 txn->req.msg_state = HTTP_MSG_ERROR;
4179 goto wait_other_side;
4180 }
4181 }
4182
4183 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4184 http_msg_closed:
4185 goto wait_other_side;
4186 }
4187
4188 wait_other_side:
4189 http_silent_debug(__LINE__, s);
4190 return txn->req.msg_state != old_state || buf->flags != old_flags;
4191}
4192
4193
4194/* This function updates the response state machine according to the request
4195 * state machine and buffer flags. It returns 1 if it changes anything (flag
4196 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4197 * it is only used to find when a request/response couple is complete. Both
4198 * this function and its equivalent should loop until both return zero. It
4199 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4200 */
4201int http_sync_res_state(struct session *s)
4202{
4203 struct buffer *buf = s->rep;
4204 struct http_txn *txn = &s->txn;
4205 unsigned int old_flags = buf->flags;
4206 unsigned int old_state = txn->rsp.msg_state;
4207
4208 http_silent_debug(__LINE__, s);
4209 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4210 return 0;
4211
4212 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4213 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004214 * still monitor the server connection for a possible close
4215 * while the request is being uploaded, so we don't disable
4216 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004218 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004219
4220 if (txn->req.msg_state == HTTP_MSG_ERROR)
4221 goto wait_other_side;
4222
4223 if (txn->req.msg_state < HTTP_MSG_DONE) {
4224 /* The client seems to still be sending data, probably
4225 * because we got an error response during an upload.
4226 * We have the choice of either breaking the connection
4227 * or letting it pass through. Let's do the later.
4228 */
4229 goto wait_other_side;
4230 }
4231
4232 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4233 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004234 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004235 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4236 goto wait_other_side;
4237 }
4238
4239 /* When we get here, it means that both the request and the
4240 * response have finished receiving. Depending on the connection
4241 * mode, we'll have to wait for the last bytes to leave in either
4242 * direction, and sometimes for a close to be effective.
4243 */
4244
4245 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4246 /* Server-close mode : shut read and wait for the request
4247 * side to close its output buffer. The caller will detect
4248 * when we're in DONE and the other is in CLOSED and will
4249 * catch that for the final cleanup.
4250 */
4251 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4252 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004253 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004254 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4255 /* Option forceclose is set, or either side wants to close,
4256 * let's enforce it now that we're not expecting any new
4257 * data to come. The caller knows the session is complete
4258 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004259 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004260 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4261 buffer_shutr_now(buf);
4262 buffer_shutw_now(buf);
4263 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004264 }
4265 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004266 /* The last possible modes are keep-alive and tunnel. Since tunnel
4267 * mode does not set the body analyser, we can't reach this place
4268 * in tunnel mode, so we're left with keep-alive only.
4269 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004271 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004272 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273 }
4274
4275 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4276 /* if we've just closed an output, let's switch */
4277 if (!(buf->flags & BF_OUT_EMPTY)) {
4278 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4279 goto http_msg_closing;
4280 }
4281 else {
4282 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4283 goto http_msg_closed;
4284 }
4285 }
4286 goto wait_other_side;
4287 }
4288
4289 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4290 http_msg_closing:
4291 /* nothing else to forward, just waiting for the output buffer
4292 * to be empty and for the shutw_now to take effect.
4293 */
4294 if (buf->flags & BF_OUT_EMPTY) {
4295 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4296 goto http_msg_closed;
4297 }
4298 else if (buf->flags & BF_SHUTW) {
4299 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004300 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004301 if (target_srv(&s->target))
4302 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004303 goto wait_other_side;
4304 }
4305 }
4306
4307 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4308 http_msg_closed:
4309 /* drop any pending data */
4310 buffer_ignore(buf, buf->l - buf->send_max);
4311 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004312 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004313 goto wait_other_side;
4314 }
4315
4316 wait_other_side:
4317 http_silent_debug(__LINE__, s);
4318 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4319}
4320
4321
4322/* Resync the request and response state machines. Return 1 if either state
4323 * changes.
4324 */
4325int http_resync_states(struct session *s)
4326{
4327 struct http_txn *txn = &s->txn;
4328 int old_req_state = txn->req.msg_state;
4329 int old_res_state = txn->rsp.msg_state;
4330
4331 http_silent_debug(__LINE__, s);
4332 http_sync_req_state(s);
4333 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004334 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335 if (!http_sync_res_state(s))
4336 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004337 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338 if (!http_sync_req_state(s))
4339 break;
4340 }
4341 http_silent_debug(__LINE__, s);
4342 /* OK, both state machines agree on a compatible state.
4343 * There are a few cases we're interested in :
4344 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4345 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4346 * directions, so let's simply disable both analysers.
4347 * - HTTP_MSG_CLOSED on the response only means we must abort the
4348 * request.
4349 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4350 * with server-close mode means we've completed one request and we
4351 * must re-initialize the server connection.
4352 */
4353
4354 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4355 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4356 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4357 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4358 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004359 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004360 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004361 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004362 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004363 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004364 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004365 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4366 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004367 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004368 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004369 s->rep->analysers = 0;
4370 buffer_auto_close(s->rep);
4371 buffer_auto_read(s->rep);
4372 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373 buffer_abort(s->req);
4374 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004375 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004376 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004377 }
4378 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4379 txn->rsp.msg_state == HTTP_MSG_DONE &&
4380 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4381 /* server-close: terminate this server connection and
4382 * reinitialize a fresh-new transaction.
4383 */
4384 http_end_txn_clean_session(s);
4385 }
4386
4387 http_silent_debug(__LINE__, s);
4388 return txn->req.msg_state != old_req_state ||
4389 txn->rsp.msg_state != old_res_state;
4390}
4391
Willy Tarreaud98cf932009-12-27 22:54:55 +01004392/* This function is an analyser which forwards request body (including chunk
4393 * sizes if any). It is called as soon as we must forward, even if we forward
4394 * zero byte. The only situation where it must not be called is when we're in
4395 * tunnel mode and we want to forward till the close. It's used both to forward
4396 * remaining data and to resync after end of body. It expects the msg_state to
4397 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4398 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004399 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004400 * bytes of pending data + the headers if not already done (between som and sov).
4401 * It eventually adjusts som to match sov after the data in between have been sent.
4402 */
4403int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4404{
4405 struct http_txn *txn = &s->txn;
4406 struct http_msg *msg = &s->txn.req;
4407
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004408 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4409 return 0;
4410
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004411 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4412 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004413 /* Output closed while we were sending data. We must abort and
4414 * wake the other side up.
4415 */
4416 msg->msg_state = HTTP_MSG_ERROR;
4417 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004418 return 1;
4419 }
4420
Willy Tarreau4fe41902010-06-07 22:27:41 +02004421 /* in most states, we should abort in case of early close */
4422 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004423
4424 /* Note that we don't have to send 100-continue back because we don't
4425 * need the data to complete our job, and it's up to the server to
4426 * decide whether to return 100, 417 or anything else in return of
4427 * an "Expect: 100-continue" header.
4428 */
4429
4430 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4431 /* we have msg->col and msg->sov which both point to the first
4432 * byte of message body. msg->som still points to the beginning
4433 * of the message. We must save the body in req->lr because it
4434 * survives buffer re-alignments.
4435 */
4436 req->lr = req->data + msg->sov;
4437 if (txn->flags & TX_REQ_TE_CHNK)
4438 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4439 else {
4440 msg->msg_state = HTTP_MSG_DATA;
4441 }
4442 }
4443
Willy Tarreaud98cf932009-12-27 22:54:55 +01004444 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004445 int bytes;
4446
Willy Tarreau610ecce2010-01-04 21:15:02 +01004447 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004448 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004449 bytes = msg->sov - msg->som;
4450 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004451 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004452 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4453 bytes += req->size;
4454 msg->chunk_len += (unsigned int)bytes;
4455 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004456 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004457
Willy Tarreaucaabe412010-01-03 23:08:28 +01004458 if (msg->msg_state == HTTP_MSG_DATA) {
4459 /* must still forward */
4460 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004461 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004462
4463 /* nothing left to forward */
4464 if (txn->flags & TX_REQ_TE_CHNK)
4465 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004466 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004467 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004468 }
4469 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004470 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004471 * set ->sov and ->lr to point to the body and switch to DATA or
4472 * TRAILERS state.
4473 */
4474 int ret = http_parse_chunk_size(req, msg);
4475
4476 if (!ret)
4477 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004478 else if (ret < 0) {
4479 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004480 if (msg->err_pos >= 0)
4481 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004482 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004483 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004484 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004485 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004486 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4487 /* we want the CRLF after the data */
4488 int ret;
4489
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004490 req->lr = req->w + req->send_max;
4491 if (req->lr >= req->data + req->size)
4492 req->lr -= req->size;
4493
Willy Tarreaud98cf932009-12-27 22:54:55 +01004494 ret = http_skip_chunk_crlf(req, msg);
4495
4496 if (ret == 0)
4497 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004498 else if (ret < 0) {
4499 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004500 if (msg->err_pos >= 0)
4501 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004502 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004503 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004504 /* we're in MSG_CHUNK_SIZE now */
4505 }
4506 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4507 int ret = http_forward_trailers(req, msg);
4508
4509 if (ret == 0)
4510 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004511 else if (ret < 0) {
4512 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004513 if (msg->err_pos >= 0)
4514 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004515 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004516 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004517 /* we're in HTTP_MSG_DONE now */
4518 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004519 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004520 int old_state = msg->msg_state;
4521
Willy Tarreau610ecce2010-01-04 21:15:02 +01004522 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004523 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004524 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4525 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4526 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004527 if (http_resync_states(s)) {
4528 /* some state changes occurred, maybe the analyser
4529 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004530 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004531 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4532 if (req->flags & BF_SHUTW) {
4533 /* request errors are most likely due to
4534 * the server aborting the transfer.
4535 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004536 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004537 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004538 if (msg->err_pos >= 0)
4539 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004540 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004541 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004542 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004543 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004544
4545 /* If "option abortonclose" is set on the backend, we
4546 * want to monitor the client's connection and forward
4547 * any shutdown notification to the server, which will
4548 * decide whether to close or to go on processing the
4549 * request.
4550 */
4551 if (s->be->options & PR_O_ABRT_CLOSE) {
4552 buffer_auto_read(req);
4553 buffer_auto_close(req);
4554 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004555 else if (s->txn.meth == HTTP_METH_POST) {
4556 /* POST requests may require to read extra CRLF
4557 * sent by broken browsers and which could cause
4558 * an RST to be sent upon close on some systems
4559 * (eg: Linux).
4560 */
4561 buffer_auto_read(req);
4562 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004563
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004565 }
4566 }
4567
Willy Tarreaud98cf932009-12-27 22:54:55 +01004568 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004569 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004570 if (req->flags & BF_SHUTR) {
4571 if (!(s->flags & SN_ERR_MASK))
4572 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004573 if (!(s->flags & SN_FINST_MASK)) {
4574 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4575 s->flags |= SN_FINST_H;
4576 else
4577 s->flags |= SN_FINST_D;
4578 }
4579
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004580 s->fe->fe_counters.cli_aborts++;
4581 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004582 if (target_srv(&s->target))
4583 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004584
4585 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004586 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004587
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004588 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004589 if (req->flags & BF_SHUTW)
4590 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004591
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004592 /* When TE: chunked is used, we need to get there again to parse remaining
4593 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4594 */
4595 if (txn->flags & TX_REQ_TE_CHNK)
4596 buffer_dont_close(req);
4597
Willy Tarreau5c620922011-05-11 19:56:11 +02004598 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004599 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4600 * system knows it must not set a PUSH on this first part. Interactive
4601 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02004602 */
Willy Tarreau07293032011-05-30 18:29:28 +02004603 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004604
Willy Tarreau610ecce2010-01-04 21:15:02 +01004605 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004606 return 0;
4607
Willy Tarreaud98cf932009-12-27 22:54:55 +01004608 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004609 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004610 if (s->listener->counters)
4611 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004612 return_bad_req_stats_ok:
4613 txn->req.msg_state = HTTP_MSG_ERROR;
4614 if (txn->status) {
4615 /* Note: we don't send any error if some data were already sent */
4616 stream_int_retnclose(req->prod, NULL);
4617 } else {
4618 txn->status = 400;
4619 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4620 }
4621 req->analysers = 0;
4622 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004623
4624 if (!(s->flags & SN_ERR_MASK))
4625 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004626 if (!(s->flags & SN_FINST_MASK)) {
4627 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4628 s->flags |= SN_FINST_H;
4629 else
4630 s->flags |= SN_FINST_D;
4631 }
4632 return 0;
4633
4634 aborted_xfer:
4635 txn->req.msg_state = HTTP_MSG_ERROR;
4636 if (txn->status) {
4637 /* Note: we don't send any error if some data were already sent */
4638 stream_int_retnclose(req->prod, NULL);
4639 } else {
4640 txn->status = 502;
4641 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4642 }
4643 req->analysers = 0;
4644 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4645
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004646 s->fe->fe_counters.srv_aborts++;
4647 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004648 if (target_srv(&s->target))
4649 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004650
4651 if (!(s->flags & SN_ERR_MASK))
4652 s->flags |= SN_ERR_SRVCL;
4653 if (!(s->flags & SN_FINST_MASK)) {
4654 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4655 s->flags |= SN_FINST_H;
4656 else
4657 s->flags |= SN_FINST_D;
4658 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004659 return 0;
4660}
4661
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004662/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4663 * processing can continue on next analysers, or zero if it either needs more
4664 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4665 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4666 * when it has nothing left to do, and may remove any analyser when it wants to
4667 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004668 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004669int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004670{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004671 struct http_txn *txn = &s->txn;
4672 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004673 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004674 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004675 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004676 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004677
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004678 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 +02004679 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004680 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004681 rep,
4682 rep->rex, rep->wex,
4683 rep->flags,
4684 rep->l,
4685 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004686
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004687 /*
4688 * Now parse the partial (or complete) lines.
4689 * We will check the response syntax, and also join multi-line
4690 * headers. An index of all the lines will be elaborated while
4691 * parsing.
4692 *
4693 * For the parsing, we use a 28 states FSM.
4694 *
4695 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004696 * rep->data + msg->som = beginning of response
4697 * rep->data + msg->eoh = end of processed headers / start of current one
4698 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004699 * rep->lr = first non-visited byte
4700 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004701 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004702 */
4703
Willy Tarreau83e3af02009-12-28 17:39:57 +01004704 /* There's a protected area at the end of the buffer for rewriting
4705 * purposes. We don't want to start to parse the request if the
4706 * protected area is affected, because we may have to move processed
4707 * data later, which is much more complicated.
4708 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004709 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4710 if (unlikely((rep->flags & BF_FULL) ||
4711 rep->r < rep->lr ||
4712 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4713 if (rep->send_max) {
4714 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004715 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4716 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004717 buffer_dont_close(rep);
4718 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4719 return 0;
4720 }
4721 if (rep->l <= rep->size - global.tune.maxrewrite)
4722 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004723 }
4724
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004725 if (likely(rep->lr < rep->r))
4726 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004727 }
4728
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004729 /* 1: we might have to print this header in debug mode */
4730 if (unlikely((global.mode & MODE_DEBUG) &&
4731 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004732 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004733 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004734 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004735
Willy Tarreau663308b2010-06-07 14:06:08 +02004736 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004737 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004738 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004739
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004740 sol += hdr_idx_first_pos(&txn->hdr_idx);
4741 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004742
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004743 while (cur_idx) {
4744 eol = sol + txn->hdr_idx.v[cur_idx].len;
4745 debug_hdr("srvhdr", s, sol, eol);
4746 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4747 cur_idx = txn->hdr_idx.v[cur_idx].next;
4748 }
4749 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004750
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004751 /*
4752 * Now we quickly check if we have found a full valid response.
4753 * If not so, we check the FD and buffer states before leaving.
4754 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004755 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004756 * responses are checked first.
4757 *
4758 * Depending on whether the client is still there or not, we
4759 * may send an error response back or not. Note that normally
4760 * we should only check for HTTP status there, and check I/O
4761 * errors somewhere else.
4762 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004763
Willy Tarreau655dce92009-11-08 13:10:58 +01004764 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004765 /* Invalid response */
4766 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4767 /* we detected a parsing error. We want to archive this response
4768 * in the dedicated proxy area for later troubleshooting.
4769 */
4770 hdr_response_bad:
4771 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004772 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004774 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004775 if (target_srv(&s->target)) {
4776 target_srv(&s->target)->counters.failed_resp++;
4777 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004778 }
Willy Tarreau64648412010-03-05 10:41:54 +01004779 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004780 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004781 rep->analysers = 0;
4782 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004783 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004784 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004785 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4786
4787 if (!(s->flags & SN_ERR_MASK))
4788 s->flags |= SN_ERR_PRXCOND;
4789 if (!(s->flags & SN_FINST_MASK))
4790 s->flags |= SN_FINST_H;
4791
4792 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004793 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004794
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004795 /* too large response does not fit in buffer. */
4796 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004797 if (msg->err_pos < 0)
4798 msg->err_pos = rep->l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004799 goto hdr_response_bad;
4800 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004801
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004802 /* read error */
4803 else if (rep->flags & BF_READ_ERROR) {
4804 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004805 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004806
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004807 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004808 if (target_srv(&s->target)) {
4809 target_srv(&s->target)->counters.failed_resp++;
4810 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004811 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004812
Willy Tarreau90deb182010-01-07 00:20:41 +01004813 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004814 rep->analysers = 0;
4815 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004816 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004817 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004818 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004819
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004820 if (!(s->flags & SN_ERR_MASK))
4821 s->flags |= SN_ERR_SRVCL;
4822 if (!(s->flags & SN_FINST_MASK))
4823 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004824 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004825 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004826
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004827 /* read timeout : return a 504 to the client. */
4828 else if (rep->flags & BF_READ_TIMEOUT) {
4829 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004830 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004831
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004832 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004833 if (target_srv(&s->target)) {
4834 target_srv(&s->target)->counters.failed_resp++;
4835 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004836 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004837
Willy Tarreau90deb182010-01-07 00:20:41 +01004838 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004839 rep->analysers = 0;
4840 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004841 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004842 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004843 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004844
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004845 if (!(s->flags & SN_ERR_MASK))
4846 s->flags |= SN_ERR_SRVTO;
4847 if (!(s->flags & SN_FINST_MASK))
4848 s->flags |= SN_FINST_H;
4849 return 0;
4850 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004851
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004852 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004853 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004854 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004855 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004856
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004857 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004858 if (target_srv(&s->target)) {
4859 target_srv(&s->target)->counters.failed_resp++;
4860 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004861 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004862
Willy Tarreau90deb182010-01-07 00:20:41 +01004863 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 rep->analysers = 0;
4865 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004866 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004867 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004868 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004869
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004870 if (!(s->flags & SN_ERR_MASK))
4871 s->flags |= SN_ERR_SRVCL;
4872 if (!(s->flags & SN_FINST_MASK))
4873 s->flags |= SN_FINST_H;
4874 return 0;
4875 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004876
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004877 /* write error to client (we don't send any message then) */
4878 else if (rep->flags & BF_WRITE_ERROR) {
4879 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004880 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004881
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004882 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004883 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004884 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004885
4886 if (!(s->flags & SN_ERR_MASK))
4887 s->flags |= SN_ERR_CLICL;
4888 if (!(s->flags & SN_FINST_MASK))
4889 s->flags |= SN_FINST_H;
4890
4891 /* process_session() will take care of the error */
4892 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004893 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004894
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004895 buffer_dont_close(rep);
4896 return 0;
4897 }
4898
4899 /* More interesting part now : we know that we have a complete
4900 * response which at least looks like HTTP. We have an indicator
4901 * of each header's length, so we can parse them quickly.
4902 */
4903
4904 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004905 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004906
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004907 /*
4908 * 1: get the status code
4909 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004910 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004911 if (n < 1 || n > 5)
4912 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004913 /* when the client triggers a 4xx from the server, it's most often due
4914 * to a missing object or permission. These events should be tracked
4915 * because if they happen often, it may indicate a brute force or a
4916 * vulnerability scan.
4917 */
4918 if (n == 4)
4919 session_inc_http_err_ctr(s);
4920
Willy Tarreau827aee92011-03-10 16:55:02 +01004921 if (target_srv(&s->target))
4922 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004923
Willy Tarreau5b154472009-12-21 20:11:07 +01004924 /* check if the response is HTTP/1.1 or above */
4925 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004926 ((msg->sol[5] > '1') ||
4927 ((msg->sol[5] == '1') &&
4928 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004929 txn->flags |= TX_RES_VER_11;
4930
4931 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004932 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 +01004933
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004934 /* transfer length unknown*/
4935 txn->flags &= ~TX_RES_XFER_LEN;
4936
Willy Tarreau962c3f42010-01-10 00:15:35 +01004937 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004938
Willy Tarreau39650402010-03-15 19:44:39 +01004939 /* Adjust server's health based on status code. Note: status codes 501
4940 * and 505 are triggered on demand by client request, so we must not
4941 * count them as server failures.
4942 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004943 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004944 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004945 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004946 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004947 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004948 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004949
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004950 /*
4951 * 2: check for cacheability.
4952 */
4953
4954 switch (txn->status) {
4955 case 200:
4956 case 203:
4957 case 206:
4958 case 300:
4959 case 301:
4960 case 410:
4961 /* RFC2616 @13.4:
4962 * "A response received with a status code of
4963 * 200, 203, 206, 300, 301 or 410 MAY be stored
4964 * by a cache (...) unless a cache-control
4965 * directive prohibits caching."
4966 *
4967 * RFC2616 @9.5: POST method :
4968 * "Responses to this method are not cacheable,
4969 * unless the response includes appropriate
4970 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004971 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004972 if (likely(txn->meth != HTTP_METH_POST) &&
4973 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4974 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4975 break;
4976 default:
4977 break;
4978 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004979
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004980 /*
4981 * 3: we may need to capture headers
4982 */
4983 s->logs.logwait &= ~LW_RESP;
4984 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004985 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004986 txn->rsp.cap, s->fe->rsp_cap);
4987
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004988 /* 4: determine the transfer-length.
4989 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4990 * the presence of a message-body in a RESPONSE and its transfer length
4991 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004992 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004993 * All responses to the HEAD request method MUST NOT include a
4994 * message-body, even though the presence of entity-header fields
4995 * might lead one to believe they do. All 1xx (informational), 204
4996 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4997 * message-body. All other responses do include a message-body,
4998 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004999 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005000 * 1. Any response which "MUST NOT" include a message-body (such as the
5001 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5002 * always terminated by the first empty line after the header fields,
5003 * regardless of the entity-header fields present in the message.
5004 *
5005 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5006 * the "chunked" transfer-coding (Section 6.2) is used, the
5007 * transfer-length is defined by the use of this transfer-coding.
5008 * If a Transfer-Encoding header field is present and the "chunked"
5009 * transfer-coding is not present, the transfer-length is defined by
5010 * the sender closing the connection.
5011 *
5012 * 3. If a Content-Length header field is present, its decimal value in
5013 * OCTETs represents both the entity-length and the transfer-length.
5014 * If a message is received with both a Transfer-Encoding header
5015 * field and a Content-Length header field, the latter MUST be ignored.
5016 *
5017 * 4. If the message uses the media type "multipart/byteranges", and
5018 * the transfer-length is not otherwise specified, then this self-
5019 * delimiting media type defines the transfer-length. This media
5020 * type MUST NOT be used unless the sender knows that the recipient
5021 * can parse it; the presence in a request of a Range header with
5022 * multiple byte-range specifiers from a 1.1 client implies that the
5023 * client can parse multipart/byteranges responses.
5024 *
5025 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005026 */
5027
5028 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005029 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005030 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005031 * FIXME: should we parse anyway and return an error on chunked encoding ?
5032 */
5033 if (txn->meth == HTTP_METH_HEAD ||
5034 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005035 txn->status == 204 || txn->status == 304) {
5036 txn->flags |= TX_RES_XFER_LEN;
5037 goto skip_content_length;
5038 }
5039
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005040 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005041 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01005042 while ((txn->flags & TX_RES_VER_11) &&
5043 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005044 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
5045 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5046 else if (txn->flags & TX_RES_TE_CHNK) {
5047 /* bad transfer-encoding (chunked followed by something else) */
5048 use_close_only = 1;
5049 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5050 break;
5051 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005052 }
5053
5054 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5055 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005056 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005057 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
5058 signed long long cl;
5059
Willy Tarreauad14f752011-09-02 20:33:27 +02005060 if (!ctx.vlen) {
5061 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005062 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005063 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005064
Willy Tarreauad14f752011-09-02 20:33:27 +02005065 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
5066 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005067 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005068 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005069
Willy Tarreauad14f752011-09-02 20:33:27 +02005070 if (cl < 0) {
5071 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005072 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005073 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005074
Willy Tarreauad14f752011-09-02 20:33:27 +02005075 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) {
5076 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005077 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005078 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005079
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005080 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005081 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005082 }
5083
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005084 /* FIXME: we should also implement the multipart/byterange method.
5085 * For now on, we resort to close mode in this case (unknown length).
5086 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005087skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005088
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005089 /* end of job, return OK */
5090 rep->analysers &= ~an_bit;
5091 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01005092 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005093 return 1;
5094}
5095
5096/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005097 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5098 * and updates t->rep->analysers. It might make sense to explode it into several
5099 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005100 */
5101int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5102{
5103 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005104 struct http_msg *msg = &txn->rsp;
5105 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005106 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005107
5108 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5109 now_ms, __FUNCTION__,
5110 t,
5111 rep,
5112 rep->rex, rep->wex,
5113 rep->flags,
5114 rep->l,
5115 rep->analysers);
5116
Willy Tarreau655dce92009-11-08 13:10:58 +01005117 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005118 return 0;
5119
5120 rep->analysers &= ~an_bit;
5121 rep->analyse_exp = TICK_ETERNITY;
5122
Willy Tarreau5b154472009-12-21 20:11:07 +01005123 /* Now we have to check if we need to modify the Connection header.
5124 * This is more difficult on the response than it is on the request,
5125 * because we can have two different HTTP versions and we don't know
5126 * how the client will interprete a response. For instance, let's say
5127 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5128 * HTTP/1.1 response without any header. Maybe it will bound itself to
5129 * HTTP/1.0 because it only knows about it, and will consider the lack
5130 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5131 * the lack of header as a keep-alive. Thus we will use two flags
5132 * indicating how a request MAY be understood by the client. In case
5133 * of multiple possibilities, we'll fix the header to be explicit. If
5134 * ambiguous cases such as both close and keepalive are seen, then we
5135 * will fall back to explicit close. Note that we won't take risks with
5136 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005137 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005138 */
5139
Willy Tarreaudc008c52010-02-01 16:20:08 +01005140 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5141 txn->status == 101)) {
5142 /* Either we've established an explicit tunnel, or we're
5143 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005144 * to understand the next protocols. We have to switch to tunnel
5145 * mode, so that we transfer the request and responses then let
5146 * this protocol pass unmodified. When we later implement specific
5147 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005148 * header which contains information about that protocol for
5149 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005150 */
5151 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5152 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005153 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5154 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5155 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005156 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005157
Willy Tarreau60466522010-01-18 19:08:45 +01005158 /* on unknown transfer length, we must close */
5159 if (!(txn->flags & TX_RES_XFER_LEN) &&
5160 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5161 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005162
Willy Tarreau60466522010-01-18 19:08:45 +01005163 /* now adjust header transformations depending on current state */
5164 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5165 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5166 to_del |= 2; /* remove "keep-alive" on any response */
5167 if (!(txn->flags & TX_RES_VER_11))
5168 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005169 }
Willy Tarreau60466522010-01-18 19:08:45 +01005170 else { /* SCL / KAL */
5171 to_del |= 1; /* remove "close" on any response */
5172 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5173 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005174 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005175
Willy Tarreau60466522010-01-18 19:08:45 +01005176 /* Parse and remove some headers from the connection header */
5177 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005178
Willy Tarreau60466522010-01-18 19:08:45 +01005179 /* Some keep-alive responses are converted to Server-close if
5180 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005181 */
Willy Tarreau60466522010-01-18 19:08:45 +01005182 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5183 if ((txn->flags & TX_HDR_CONN_CLO) ||
5184 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5185 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005186 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005187 }
5188
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005189 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005190 /*
5191 * 3: we will have to evaluate the filters.
5192 * As opposed to version 1.2, now they will be evaluated in the
5193 * filters order and not in the header order. This means that
5194 * each filter has to be validated among all headers.
5195 *
5196 * Filters are tried with ->be first, then with ->fe if it is
5197 * different from ->be.
5198 */
5199
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005200 cur_proxy = t->be;
5201 while (1) {
5202 struct proxy *rule_set = cur_proxy;
5203
5204 /* try headers filters */
5205 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005206 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005207 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005208 if (target_srv(&t->target)) {
5209 target_srv(&t->target)->counters.failed_resp++;
5210 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005211 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005212 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005213 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005214 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005215 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005216 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005217 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005218 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005219 if (!(t->flags & SN_ERR_MASK))
5220 t->flags |= SN_ERR_PRXCOND;
5221 if (!(t->flags & SN_FINST_MASK))
5222 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005223 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005224 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005225 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005226
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005227 /* has the response been denied ? */
5228 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005229 if (target_srv(&t->target))
5230 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005231
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005232 t->be->be_counters.denied_resp++;
5233 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005234 if (t->listener->counters)
5235 t->listener->counters->denied_resp++;
5236
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005237 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005238 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005239
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005240 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005241 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005242 if (txn->status < 200)
5243 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005244 if (wl->cond) {
5245 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5246 ret = acl_pass(ret);
5247 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5248 ret = !ret;
5249 if (!ret)
5250 continue;
5251 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005252 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005253 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005254 }
5255
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005256 /* check whether we're already working on the frontend */
5257 if (cur_proxy == t->fe)
5258 break;
5259 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005260 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005261
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005262 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005263 * We may be facing a 100-continue response, in which case this
5264 * is not the right response, and we're waiting for the next one.
5265 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005266 * next one.
5267 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005268 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005269 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005270 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005271 msg->msg_state = HTTP_MSG_RPBEFORE;
5272 txn->status = 0;
5273 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5274 return 1;
5275 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005276 else if (unlikely(txn->status < 200))
5277 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005278
5279 /* we don't have any 1xx status code now */
5280
5281 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005282 * 4: check for server cookie.
5283 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005284 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5285 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005286 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005287
Willy Tarreaubaaee002006-06-26 02:48:02 +02005288
Willy Tarreaua15645d2007-03-18 16:22:39 +01005289 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005290 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005291 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005292 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005293 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005294
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005295 /*
5296 * 6: add server cookie in the response if needed
5297 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005298 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005299 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005300 (!(t->flags & SN_DIRECT) ||
5301 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5302 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5303 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5304 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005305 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5306 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005307 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005308 /* the server is known, it's not the one the client requested, or the
5309 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005310 * insert a set-cookie here, except if we want to insert only on POST
5311 * requests and this one isn't. Note that servers which don't have cookies
5312 * (eg: some backup servers) will return a full cookie removal request.
5313 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005314 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005315 len = sprintf(trash,
5316 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5317 t->be->cookie_name);
5318 }
5319 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005320 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005321
5322 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5323 /* emit last_date, which is mandatory */
5324 trash[len++] = COOKIE_DELIM_DATE;
5325 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5326 if (t->be->cookie_maxlife) {
5327 /* emit first_date, which is either the original one or
5328 * the current date.
5329 */
5330 trash[len++] = COOKIE_DELIM_DATE;
5331 s30tob64(txn->cookie_first_date ?
5332 txn->cookie_first_date >> 2 :
5333 (date.tv_sec+3) >> 2, trash + len);
5334 len += 5;
5335 }
5336 }
5337 len += sprintf(trash + len, "; path=/");
5338 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005339
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005340 if (t->be->cookie_domain)
5341 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005342
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005343 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005344 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005345 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005346
Willy Tarreauf1348312010-10-07 15:54:11 +02005347 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005348 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005349 /* the server did not change, only the date was updated */
5350 txn->flags |= TX_SCK_UPDATED;
5351 else
5352 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005353
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005354 /* Here, we will tell an eventual cache on the client side that we don't
5355 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5356 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5357 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5358 */
5359 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005360
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005361 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5362
5363 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005364 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005365 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005366 }
5367 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005368
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005369 /*
5370 * 7: check if result will be cacheable with a cookie.
5371 * We'll block the response if security checks have caught
5372 * nasty things such as a cacheable cookie.
5373 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005374 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5375 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005376 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005377
5378 /* we're in presence of a cacheable response containing
5379 * a set-cookie header. We'll block it as requested by
5380 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005381 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005382 if (target_srv(&t->target))
5383 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005384
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005385 t->be->be_counters.denied_resp++;
5386 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005387 if (t->listener->counters)
5388 t->listener->counters->denied_resp++;
5389
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005390 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005391 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005392 send_log(t->be, LOG_ALERT,
5393 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005394 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005395 goto return_srv_prx_502;
5396 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005397
5398 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005399 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005400 */
Willy Tarreau60466522010-01-18 19:08:45 +01005401 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5402 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5403 unsigned int want_flags = 0;
5404
5405 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5406 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5407 /* we want a keep-alive response here. Keep-alive header
5408 * required if either side is not 1.1.
5409 */
5410 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5411 want_flags |= TX_CON_KAL_SET;
5412 }
5413 else {
5414 /* we want a close response here. Close header required if
5415 * the server is 1.1, regardless of the client.
5416 */
5417 if (txn->flags & TX_RES_VER_11)
5418 want_flags |= TX_CON_CLO_SET;
5419 }
5420
5421 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5422 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005423 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005424
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005425 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005426 if ((txn->flags & TX_RES_XFER_LEN) ||
5427 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005428 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005429
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005430 /*************************************************************
5431 * OK, that's finished for the headers. We have done what we *
5432 * could. Let's switch to the DATA state. *
5433 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005434
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005435 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005436
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005437 /* if the user wants to log as soon as possible, without counting
5438 * bytes from the server, then this is the right moment. We have
5439 * to temporarily assign bytes_out to log what we currently have.
5440 */
5441 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5442 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5443 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005444 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005445 t->logs.bytes_out = 0;
5446 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005447
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005448 /* Note: we must not try to cheat by jumping directly to DATA,
5449 * otherwise we would not let the client side wake up.
5450 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005451
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005452 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005453 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005454 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005455}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005456
Willy Tarreaud98cf932009-12-27 22:54:55 +01005457/* This function is an analyser which forwards response body (including chunk
5458 * sizes if any). It is called as soon as we must forward, even if we forward
5459 * zero byte. The only situation where it must not be called is when we're in
5460 * tunnel mode and we want to forward till the close. It's used both to forward
5461 * remaining data and to resync after end of body. It expects the msg_state to
5462 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5463 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005464 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005465 * bytes of pending data + the headers if not already done (between som and sov).
5466 * It eventually adjusts som to match sov after the data in between have been sent.
5467 */
5468int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5469{
5470 struct http_txn *txn = &s->txn;
5471 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005472 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005473
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005474 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5475 return 0;
5476
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005477 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005478 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005479 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005480 /* Output closed while we were sending data. We must abort and
5481 * wake the other side up.
5482 */
5483 msg->msg_state = HTTP_MSG_ERROR;
5484 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005485 return 1;
5486 }
5487
Willy Tarreau4fe41902010-06-07 22:27:41 +02005488 /* in most states, we should abort in case of early close */
5489 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005490
Willy Tarreaud98cf932009-12-27 22:54:55 +01005491 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5492 /* we have msg->col and msg->sov which both point to the first
5493 * byte of message body. msg->som still points to the beginning
5494 * of the message. We must save the body in req->lr because it
5495 * survives buffer re-alignments.
5496 */
5497 res->lr = res->data + msg->sov;
5498 if (txn->flags & TX_RES_TE_CHNK)
5499 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5500 else {
5501 msg->msg_state = HTTP_MSG_DATA;
5502 }
5503 }
5504
Willy Tarreaud98cf932009-12-27 22:54:55 +01005505 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005506 int bytes;
5507
Willy Tarreau610ecce2010-01-04 21:15:02 +01005508 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005509 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005510 bytes = msg->sov - msg->som;
5511 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005512 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005513 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5514 bytes += res->size;
5515 msg->chunk_len += (unsigned int)bytes;
5516 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005517 }
5518
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005519
Willy Tarreaucaabe412010-01-03 23:08:28 +01005520 if (msg->msg_state == HTTP_MSG_DATA) {
5521 /* must still forward */
5522 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005523 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005524
5525 /* nothing left to forward */
5526 if (txn->flags & TX_RES_TE_CHNK)
5527 msg->msg_state = HTTP_MSG_DATA_CRLF;
5528 else
5529 msg->msg_state = HTTP_MSG_DONE;
5530 }
5531 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005532 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005533 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5534 */
5535 int ret = http_parse_chunk_size(res, msg);
5536
5537 if (!ret)
5538 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005539 else if (ret < 0) {
5540 if (msg->err_pos >= 0)
5541 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005542 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005543 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005544 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005545 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005546 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5547 /* we want the CRLF after the data */
5548 int ret;
5549
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005550 res->lr = res->w + res->send_max;
5551 if (res->lr >= res->data + res->size)
5552 res->lr -= res->size;
5553
Willy Tarreaud98cf932009-12-27 22:54:55 +01005554 ret = http_skip_chunk_crlf(res, msg);
5555
5556 if (!ret)
5557 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005558 else if (ret < 0) {
5559 if (msg->err_pos >= 0)
5560 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005561 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005562 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005563 /* we're in MSG_CHUNK_SIZE now */
5564 }
5565 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5566 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005567
Willy Tarreaud98cf932009-12-27 22:54:55 +01005568 if (ret == 0)
5569 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005570 else if (ret < 0) {
5571 if (msg->err_pos >= 0)
5572 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005573 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005574 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005575 /* we're in HTTP_MSG_DONE now */
5576 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005577 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005578 int old_state = msg->msg_state;
5579
Willy Tarreau610ecce2010-01-04 21:15:02 +01005580 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005581 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005582 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5583 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5584 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005585 if (http_resync_states(s)) {
5586 http_silent_debug(__LINE__, s);
5587 /* some state changes occurred, maybe the analyser
5588 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005589 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005590 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5591 if (res->flags & BF_SHUTW) {
5592 /* response errors are most likely due to
5593 * the client aborting the transfer.
5594 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005595 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005596 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005597 if (msg->err_pos >= 0)
5598 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005599 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005600 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005601 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005602 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005603 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005604 }
5605 }
5606
Willy Tarreaud98cf932009-12-27 22:54:55 +01005607 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005608 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005609 if (res->flags & BF_SHUTR) {
5610 if (!(s->flags & SN_ERR_MASK))
5611 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005612 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005613 if (target_srv(&s->target))
5614 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005615 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005616 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005617
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005618 if (res->flags & BF_SHUTW)
5619 goto aborted_xfer;
5620
Willy Tarreau40dba092010-03-04 18:14:51 +01005621 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005622 if (!s->req->analysers)
5623 goto return_bad_res;
5624
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005625 /* forward any pending data */
5626 bytes = msg->sov - msg->som;
5627 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005628 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005629 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5630 bytes += res->size;
5631 msg->chunk_len += (unsigned int)bytes;
5632 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005633 }
5634
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005635 /* When TE: chunked is used, we need to get there again to parse remaining
5636 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5637 * Similarly, with keep-alive on the client side, we don't want to forward a
5638 * close.
5639 */
5640 if ((txn->flags & TX_RES_TE_CHNK) ||
5641 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5642 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5643 buffer_dont_close(res);
5644
Willy Tarreau5c620922011-05-11 19:56:11 +02005645 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005646 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5647 * system knows it must not set a PUSH on this first part. Interactive
5648 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02005649 */
Willy Tarreau07293032011-05-30 18:29:28 +02005650 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005651
Willy Tarreaud98cf932009-12-27 22:54:55 +01005652 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005653 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005654 return 0;
5655
Willy Tarreau40dba092010-03-04 18:14:51 +01005656 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005657 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005658 if (target_srv(&s->target))
5659 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005660
5661 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005662 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005663 /* don't send any error message as we're in the body */
5664 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005665 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005666 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005667 if (target_srv(&s->target))
5668 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005669
5670 if (!(s->flags & SN_ERR_MASK))
5671 s->flags |= SN_ERR_PRXCOND;
5672 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005673 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005674 return 0;
5675
5676 aborted_xfer:
5677 txn->rsp.msg_state = HTTP_MSG_ERROR;
5678 /* don't send any error message as we're in the body */
5679 stream_int_retnclose(res->cons, NULL);
5680 res->analysers = 0;
5681 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5682
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005683 s->fe->fe_counters.cli_aborts++;
5684 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005685 if (target_srv(&s->target))
5686 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005687
5688 if (!(s->flags & SN_ERR_MASK))
5689 s->flags |= SN_ERR_CLICL;
5690 if (!(s->flags & SN_FINST_MASK))
5691 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005692 return 0;
5693}
5694
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005695/* Iterate the same filter through all request headers.
5696 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005697 * Since it can manage the switch to another backend, it updates the per-proxy
5698 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005699 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005700int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005701{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702 char term;
5703 char *cur_ptr, *cur_end, *cur_next;
5704 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005705 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005706 struct hdr_idx_elem *cur_hdr;
5707 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005708
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709 last_hdr = 0;
5710
Willy Tarreau962c3f42010-01-10 00:15:35 +01005711 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712 old_idx = 0;
5713
5714 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005715 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005716 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005717 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005718 (exp->action == ACT_ALLOW ||
5719 exp->action == ACT_DENY ||
5720 exp->action == ACT_TARPIT))
5721 return 0;
5722
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005723 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005724 if (!cur_idx)
5725 break;
5726
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005727 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005728 cur_ptr = cur_next;
5729 cur_end = cur_ptr + cur_hdr->len;
5730 cur_next = cur_end + cur_hdr->cr + 1;
5731
5732 /* Now we have one header between cur_ptr and cur_end,
5733 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005734 */
5735
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736 /* The annoying part is that pattern matching needs
5737 * that we modify the contents to null-terminate all
5738 * strings before testing them.
5739 */
5740
5741 term = *cur_end;
5742 *cur_end = '\0';
5743
5744 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5745 switch (exp->action) {
5746 case ACT_SETBE:
5747 /* It is not possible to jump a second time.
5748 * FIXME: should we return an HTTP/500 here so that
5749 * the admin knows there's a problem ?
5750 */
5751 if (t->be != t->fe)
5752 break;
5753
5754 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005755 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005756 last_hdr = 1;
5757 break;
5758
5759 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005760 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005761 last_hdr = 1;
5762 break;
5763
5764 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005765 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005766 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005767
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005768 t->fe->fe_counters.denied_req++;
5769 if (t->fe != t->be)
5770 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005771 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005772 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005773
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005774 break;
5775
5776 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005777 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005778 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005779
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005780 t->fe->fe_counters.denied_req++;
5781 if (t->fe != t->be)
5782 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005783 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005784 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005785
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005786 break;
5787
5788 case ACT_REPLACE:
5789 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5790 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5791 /* FIXME: if the user adds a newline in the replacement, the
5792 * index will not be recalculated for now, and the new line
5793 * will not be counted as a new header.
5794 */
5795
5796 cur_end += delta;
5797 cur_next += delta;
5798 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005799 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005800 break;
5801
5802 case ACT_REMOVE:
5803 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5804 cur_next += delta;
5805
Willy Tarreaufa355d42009-11-29 18:12:29 +01005806 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005807 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5808 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005809 cur_hdr->len = 0;
5810 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005811 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005812 break;
5813
5814 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005815 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005816 if (cur_end)
5817 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005819 /* keep the link from this header to next one in case of later
5820 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005821 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005822 old_idx = cur_idx;
5823 }
5824 return 0;
5825}
5826
5827
5828/* Apply the filter to the request line.
5829 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5830 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005831 * Since it can manage the switch to another backend, it updates the per-proxy
5832 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005833 */
5834int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5835{
5836 char term;
5837 char *cur_ptr, *cur_end;
5838 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005839 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005840 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005841
Willy Tarreau58f10d72006-12-04 02:26:12 +01005842
Willy Tarreau3d300592007-03-18 18:34:41 +01005843 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005844 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005845 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005846 (exp->action == ACT_ALLOW ||
5847 exp->action == ACT_DENY ||
5848 exp->action == ACT_TARPIT))
5849 return 0;
5850 else if (exp->action == ACT_REMOVE)
5851 return 0;
5852
5853 done = 0;
5854
Willy Tarreau962c3f42010-01-10 00:15:35 +01005855 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005856 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005857
5858 /* Now we have the request line between cur_ptr and cur_end */
5859
5860 /* The annoying part is that pattern matching needs
5861 * that we modify the contents to null-terminate all
5862 * strings before testing them.
5863 */
5864
5865 term = *cur_end;
5866 *cur_end = '\0';
5867
5868 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5869 switch (exp->action) {
5870 case ACT_SETBE:
5871 /* It is not possible to jump a second time.
5872 * FIXME: should we return an HTTP/500 here so that
5873 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005874 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005875 if (t->be != t->fe)
5876 break;
5877
5878 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005879 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005880 done = 1;
5881 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005882
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005883 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005884 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005885 done = 1;
5886 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005887
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005888 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005889 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005890
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005891 t->fe->fe_counters.denied_req++;
5892 if (t->fe != t->be)
5893 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005894 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005895 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005896
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005897 done = 1;
5898 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005899
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005900 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005901 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005902
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005903 t->fe->fe_counters.denied_req++;
5904 if (t->fe != t->be)
5905 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005906 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005907 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005908
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005909 done = 1;
5910 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005911
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005912 case ACT_REPLACE:
5913 *cur_end = term; /* restore the string terminator */
5914 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5915 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5916 /* FIXME: if the user adds a newline in the replacement, the
5917 * index will not be recalculated for now, and the new line
5918 * will not be counted as a new header.
5919 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005920
Willy Tarreaufa355d42009-11-29 18:12:29 +01005921 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005922 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005923 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005924 HTTP_MSG_RQMETH,
5925 cur_ptr, cur_end + 1,
5926 NULL, NULL);
5927 if (unlikely(!cur_end))
5928 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005929
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005930 /* we have a full request and we know that we have either a CR
5931 * or an LF at <ptr>.
5932 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005933 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5934 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005935 /* there is no point trying this regex on headers */
5936 return 1;
5937 }
5938 }
5939 *cur_end = term; /* restore the string terminator */
5940 return done;
5941}
Willy Tarreau97de6242006-12-27 17:18:38 +01005942
Willy Tarreau58f10d72006-12-04 02:26:12 +01005943
Willy Tarreau58f10d72006-12-04 02:26:12 +01005944
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005945/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005946 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005947 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005948 * unparsable request. Since it can manage the switch to another backend, it
5949 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005951int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005952{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005953 struct http_txn *txn = &s->txn;
5954 struct hdr_exp *exp;
5955
5956 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005957 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005958
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005959 /*
5960 * The interleaving of transformations and verdicts
5961 * makes it difficult to decide to continue or stop
5962 * the evaluation.
5963 */
5964
Willy Tarreau6c123b12010-01-28 20:22:06 +01005965 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5966 break;
5967
Willy Tarreau3d300592007-03-18 18:34:41 +01005968 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005969 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005970 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005971 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005972
5973 /* if this filter had a condition, evaluate it now and skip to
5974 * next filter if the condition does not match.
5975 */
5976 if (exp->cond) {
5977 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5978 ret = acl_pass(ret);
5979 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5980 ret = !ret;
5981
5982 if (!ret)
5983 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005984 }
5985
5986 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005987 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005988 if (unlikely(ret < 0))
5989 return -1;
5990
5991 if (likely(ret == 0)) {
5992 /* The filter did not match the request, it can be
5993 * iterated through all headers.
5994 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005995 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005996 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005997 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005998 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005999}
6000
6001
Willy Tarreaua15645d2007-03-18 16:22:39 +01006002
Willy Tarreau58f10d72006-12-04 02:26:12 +01006003/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006004 * Try to retrieve the server associated to the appsession.
6005 * If the server is found, it's assigned to the session.
6006 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006007void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006008 struct http_txn *txn = &t->txn;
6009 appsess *asession = NULL;
6010 char *sessid_temp = NULL;
6011
Cyril Bontéb21570a2009-11-29 20:04:48 +01006012 if (len > t->be->appsession_len) {
6013 len = t->be->appsession_len;
6014 }
6015
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006016 if (t->be->options2 & PR_O2_AS_REQL) {
6017 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006018 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006019 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006020 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006021 }
6022
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006023 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006024 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6025 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6026 return;
6027 }
6028
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006029 memcpy(txn->sessid, buf, len);
6030 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006031 }
6032
6033 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6034 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6035 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6036 return;
6037 }
6038
Cyril Bontéb21570a2009-11-29 20:04:48 +01006039 memcpy(sessid_temp, buf, len);
6040 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006041
6042 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6043 /* free previously allocated memory */
6044 pool_free2(apools.sessid, sessid_temp);
6045
6046 if (asession != NULL) {
6047 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6048 if (!(t->be->options2 & PR_O2_AS_REQL))
6049 asession->request_count++;
6050
6051 if (asession->serverid != NULL) {
6052 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006053
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006054 while (srv) {
6055 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006056 if ((srv->state & SRV_RUNNING) ||
6057 (t->be->options & PR_O_PERSIST) ||
6058 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006059 /* we found the server and it's usable */
6060 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006061 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006062 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006063 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006064
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006065 break;
6066 } else {
6067 txn->flags &= ~TX_CK_MASK;
6068 txn->flags |= TX_CK_DOWN;
6069 }
6070 }
6071 srv = srv->next;
6072 }
6073 }
6074 }
6075}
6076
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006077/* Find the end of a cookie value contained between <s> and <e>. It works the
6078 * same way as with headers above except that the semi-colon also ends a token.
6079 * See RFC2965 for more information. Note that it requires a valid header to
6080 * return a valid result.
6081 */
6082char *find_cookie_value_end(char *s, const char *e)
6083{
6084 int quoted, qdpair;
6085
6086 quoted = qdpair = 0;
6087 for (; s < e; s++) {
6088 if (qdpair) qdpair = 0;
6089 else if (quoted) {
6090 if (*s == '\\') qdpair = 1;
6091 else if (*s == '"') quoted = 0;
6092 }
6093 else if (*s == '"') quoted = 1;
6094 else if (*s == ',' || *s == ';') return s;
6095 }
6096 return s;
6097}
6098
6099/* Delete a value in a header between delimiters <from> and <next> in buffer
6100 * <buf>. The number of characters displaced is returned, and the pointer to
6101 * the first delimiter is updated if required. The function tries as much as
6102 * possible to respect the following principles :
6103 * - replace <from> delimiter by the <next> one unless <from> points to a
6104 * colon, in which case <next> is simply removed
6105 * - set exactly one space character after the new first delimiter, unless
6106 * there are not enough characters in the block being moved to do so.
6107 * - remove unneeded spaces before the previous delimiter and after the new
6108 * one.
6109 *
6110 * It is the caller's responsibility to ensure that :
6111 * - <from> points to a valid delimiter or the colon ;
6112 * - <next> points to a valid delimiter or the final CR/LF ;
6113 * - there are non-space chars before <from> ;
6114 * - there is a CR/LF at or after <next>.
6115 */
6116int del_hdr_value(struct buffer *buf, char **from, char *next)
6117{
6118 char *prev = *from;
6119
6120 if (*prev == ':') {
6121 /* We're removing the first value, preserve the colon and add a
6122 * space if possible.
6123 */
6124 if (!http_is_crlf[(unsigned char)*next])
6125 next++;
6126 prev++;
6127 if (prev < next)
6128 *prev++ = ' ';
6129
6130 while (http_is_spht[(unsigned char)*next])
6131 next++;
6132 } else {
6133 /* Remove useless spaces before the old delimiter. */
6134 while (http_is_spht[(unsigned char)*(prev-1)])
6135 prev--;
6136 *from = prev;
6137
6138 /* copy the delimiter and if possible a space if we're
6139 * not at the end of the line.
6140 */
6141 if (!http_is_crlf[(unsigned char)*next]) {
6142 *prev++ = *next++;
6143 if (prev + 1 < next)
6144 *prev++ = ' ';
6145 while (http_is_spht[(unsigned char)*next])
6146 next++;
6147 }
6148 }
6149 return buffer_replace2(buf, prev, next, NULL, 0);
6150}
6151
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006152/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006153 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006154 * desirable to call it only when needed. This code is quite complex because
6155 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6156 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006157 */
6158void manage_client_side_cookies(struct session *t, struct buffer *req)
6159{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006160 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006161 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006162 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006163 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6164 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006165
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006166 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006167 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006168 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006169
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006170 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006171 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006172 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006173
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006174 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006175 hdr_beg = hdr_next;
6176 hdr_end = hdr_beg + cur_hdr->len;
6177 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006178
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006179 /* We have one full header between hdr_beg and hdr_end, and the
6180 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006181 * "Cookie:" headers.
6182 */
6183
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006184 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006185 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006186 old_idx = cur_idx;
6187 continue;
6188 }
6189
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006190 del_from = NULL; /* nothing to be deleted */
6191 preserve_hdr = 0; /* assume we may kill the whole header */
6192
Willy Tarreau58f10d72006-12-04 02:26:12 +01006193 /* Now look for cookies. Conforming to RFC2109, we have to support
6194 * attributes whose name begin with a '$', and associate them with
6195 * the right cookie, if we want to delete this cookie.
6196 * So there are 3 cases for each cookie read :
6197 * 1) it's a special attribute, beginning with a '$' : ignore it.
6198 * 2) it's a server id cookie that we *MAY* want to delete : save
6199 * some pointers on it (last semi-colon, beginning of cookie...)
6200 * 3) it's an application cookie : we *MAY* have to delete a previous
6201 * "special" cookie.
6202 * At the end of loop, if a "special" cookie remains, we may have to
6203 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204 * *MUST* delete it.
6205 *
6206 * Note: RFC2965 is unclear about the processing of spaces around
6207 * the equal sign in the ATTR=VALUE form. A careful inspection of
6208 * the RFC explicitly allows spaces before it, and not within the
6209 * tokens (attrs or values). An inspection of RFC2109 allows that
6210 * too but section 10.1.3 lets one think that spaces may be allowed
6211 * after the equal sign too, resulting in some (rare) buggy
6212 * implementations trying to do that. So let's do what servers do.
6213 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6214 * allowed quoted strings in values, with any possible character
6215 * after a backslash, including control chars and delimitors, which
6216 * causes parsing to become ambiguous. Browsers also allow spaces
6217 * within values even without quotes.
6218 *
6219 * We have to keep multiple pointers in order to support cookie
6220 * removal at the beginning, middle or end of header without
6221 * corrupting the header. All of these headers are valid :
6222 *
6223 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6224 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6225 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6226 * | | | | | | | | |
6227 * | | | | | | | | hdr_end <--+
6228 * | | | | | | | +--> next
6229 * | | | | | | +----> val_end
6230 * | | | | | +-----------> val_beg
6231 * | | | | +--------------> equal
6232 * | | | +----------------> att_end
6233 * | | +---------------------> att_beg
6234 * | +--------------------------> prev
6235 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236 */
6237
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006238 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6239 /* Iterate through all cookies on this line */
6240
6241 /* find att_beg */
6242 att_beg = prev + 1;
6243 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6244 att_beg++;
6245
6246 /* find att_end : this is the first character after the last non
6247 * space before the equal. It may be equal to hdr_end.
6248 */
6249 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 while (equal < hdr_end) {
6252 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006253 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006254 if (http_is_spht[(unsigned char)*equal++])
6255 continue;
6256 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257 }
6258
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006259 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6260 * is between <att_beg> and <equal>, both may be identical.
6261 */
6262
6263 /* look for end of cookie if there is an equal sign */
6264 if (equal < hdr_end && *equal == '=') {
6265 /* look for the beginning of the value */
6266 val_beg = equal + 1;
6267 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6268 val_beg++;
6269
6270 /* find the end of the value, respecting quotes */
6271 next = find_cookie_value_end(val_beg, hdr_end);
6272
6273 /* make val_end point to the first white space or delimitor after the value */
6274 val_end = next;
6275 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6276 val_end--;
6277 } else {
6278 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006279 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006280
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006281 /* We have nothing to do with attributes beginning with '$'. However,
6282 * they will automatically be removed if a header before them is removed,
6283 * since they're supposed to be linked together.
6284 */
6285 if (*att_beg == '$')
6286 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 /* Ignore cookies with no equal sign */
6289 if (equal == next) {
6290 /* This is not our cookie, so we must preserve it. But if we already
6291 * scheduled another cookie for removal, we cannot remove the
6292 * complete header, but we can remove the previous block itself.
6293 */
6294 preserve_hdr = 1;
6295 if (del_from != NULL) {
6296 int delta = del_hdr_value(req, &del_from, prev);
6297 val_end += delta;
6298 next += delta;
6299 hdr_end += delta;
6300 hdr_next += delta;
6301 cur_hdr->len += delta;
6302 http_msg_move_end(&txn->req, delta);
6303 prev = del_from;
6304 del_from = NULL;
6305 }
6306 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006307 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006308
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 /* if there are spaces around the equal sign, we need to
6310 * strip them otherwise we'll get trouble for cookie captures,
6311 * or even for rewrites. Since this happens extremely rarely,
6312 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006313 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006314 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6315 int stripped_before = 0;
6316 int stripped_after = 0;
6317
6318 if (att_end != equal) {
6319 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6320 equal += stripped_before;
6321 val_beg += stripped_before;
6322 }
6323
6324 if (val_beg > equal + 1) {
6325 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6326 val_beg += stripped_after;
6327 stripped_before += stripped_after;
6328 }
6329
6330 val_end += stripped_before;
6331 next += stripped_before;
6332 hdr_end += stripped_before;
6333 hdr_next += stripped_before;
6334 cur_hdr->len += stripped_before;
6335 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006337 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 /* First, let's see if we want to capture this cookie. We check
6340 * that we don't already have a client side cookie, because we
6341 * can only capture one. Also as an optimisation, we ignore
6342 * cookies shorter than the declared name.
6343 */
6344 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6345 (val_end - att_beg >= t->fe->capture_namelen) &&
6346 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6347 int log_len = val_end - att_beg;
6348
6349 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6350 Alert("HTTP logging : out of memory.\n");
6351 } else {
6352 if (log_len > t->fe->capture_len)
6353 log_len = t->fe->capture_len;
6354 memcpy(txn->cli_cookie, att_beg, log_len);
6355 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006356 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006357 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006358
Willy Tarreaubca99692010-10-06 19:25:55 +02006359 /* Persistence cookies in passive, rewrite or insert mode have the
6360 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006361 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006362 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006363 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006364 * For cookies in prefix mode, the form is :
6365 *
6366 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006367 */
6368 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6369 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6370 struct server *srv = t->be->srv;
6371 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006372
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006373 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6374 * have the server ID between val_beg and delim, and the original cookie between
6375 * delim+1 and val_end. Otherwise, delim==val_end :
6376 *
6377 * Cookie: NAME=SRV; # in all but prefix modes
6378 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6379 * | || || | |+-> next
6380 * | || || | +--> val_end
6381 * | || || +---------> delim
6382 * | || |+------------> val_beg
6383 * | || +-------------> att_end = equal
6384 * | |+-----------------> att_beg
6385 * | +------------------> prev
6386 * +-------------------------> hdr_beg
6387 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006388
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006389 if (t->be->options & PR_O_COOK_PFX) {
6390 for (delim = val_beg; delim < val_end; delim++)
6391 if (*delim == COOKIE_DELIM)
6392 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006393 } else {
6394 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006395 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006396 /* Now check if the cookie contains a date field, which would
6397 * appear after a vertical bar ('|') just after the server name
6398 * and before the delimiter.
6399 */
6400 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6401 if (vbar1) {
6402 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006403 * right is the last seen date. It is a base64 encoded
6404 * 30-bit value representing the UNIX date since the
6405 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006406 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006407 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006408 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006409 if (val_end - vbar1 >= 5) {
6410 val = b64tos30(vbar1);
6411 if (val > 0)
6412 txn->cookie_last_date = val << 2;
6413 }
6414 /* look for a second vertical bar */
6415 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6416 if (vbar1 && (val_end - vbar1 > 5)) {
6417 val = b64tos30(vbar1 + 1);
6418 if (val > 0)
6419 txn->cookie_first_date = val << 2;
6420 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006421 }
6422 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006423
Willy Tarreauf64d1412010-10-07 20:06:11 +02006424 /* if the cookie has an expiration date and the proxy wants to check
6425 * it, then we do that now. We first check if the cookie is too old,
6426 * then only if it has expired. We detect strict overflow because the
6427 * time resolution here is not great (4 seconds). Cookies with dates
6428 * in the future are ignored if their offset is beyond one day. This
6429 * allows an admin to fix timezone issues without expiring everyone
6430 * and at the same time avoids keeping unwanted side effects for too
6431 * long.
6432 */
6433 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006434 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6435 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006436 txn->flags &= ~TX_CK_MASK;
6437 txn->flags |= TX_CK_OLD;
6438 delim = val_beg; // let's pretend we have not found the cookie
6439 txn->cookie_first_date = 0;
6440 txn->cookie_last_date = 0;
6441 }
6442 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006443 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6444 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006445 txn->flags &= ~TX_CK_MASK;
6446 txn->flags |= TX_CK_EXPIRED;
6447 delim = val_beg; // let's pretend we have not found the cookie
6448 txn->cookie_first_date = 0;
6449 txn->cookie_last_date = 0;
6450 }
6451
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006452 /* Here, we'll look for the first running server which supports the cookie.
6453 * This allows to share a same cookie between several servers, for example
6454 * to dedicate backup servers to specific servers only.
6455 * However, to prevent clients from sticking to cookie-less backup server
6456 * when they have incidentely learned an empty cookie, we simply ignore
6457 * empty cookies and mark them as invalid.
6458 * The same behaviour is applied when persistence must be ignored.
6459 */
6460 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6461 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006462
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006463 while (srv) {
6464 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6465 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6466 if ((srv->state & SRV_RUNNING) ||
6467 (t->be->options & PR_O_PERSIST) ||
6468 (t->flags & SN_FORCE_PRST)) {
6469 /* we found the server and we can use it */
6470 txn->flags &= ~TX_CK_MASK;
6471 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6472 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006473 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006474 break;
6475 } else {
6476 /* we found a server, but it's down,
6477 * mark it as such and go on in case
6478 * another one is available.
6479 */
6480 txn->flags &= ~TX_CK_MASK;
6481 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006482 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006483 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006484 srv = srv->next;
6485 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006486
Willy Tarreauf64d1412010-10-07 20:06:11 +02006487 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006488 /* no server matched this cookie */
6489 txn->flags &= ~TX_CK_MASK;
6490 txn->flags |= TX_CK_INVALID;
6491 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006492
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006493 /* depending on the cookie mode, we may have to either :
6494 * - delete the complete cookie if we're in insert+indirect mode, so that
6495 * the server never sees it ;
6496 * - remove the server id from the cookie value, and tag the cookie as an
6497 * application cookie so that it does not get accidentely removed later,
6498 * if we're in cookie prefix mode
6499 */
6500 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6501 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006502
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006503 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6504 val_end += delta;
6505 next += delta;
6506 hdr_end += delta;
6507 hdr_next += delta;
6508 cur_hdr->len += delta;
6509 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006510
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006511 del_from = NULL;
6512 preserve_hdr = 1; /* we want to keep this cookie */
6513 }
6514 else if (del_from == NULL &&
6515 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6516 del_from = prev;
6517 }
6518 } else {
6519 /* This is not our cookie, so we must preserve it. But if we already
6520 * scheduled another cookie for removal, we cannot remove the
6521 * complete header, but we can remove the previous block itself.
6522 */
6523 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006524
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006525 if (del_from != NULL) {
6526 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006527 if (att_beg >= del_from)
6528 att_beg += delta;
6529 if (att_end >= del_from)
6530 att_end += delta;
6531 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006532 val_end += delta;
6533 next += delta;
6534 hdr_end += delta;
6535 hdr_next += delta;
6536 cur_hdr->len += delta;
6537 http_msg_move_end(&txn->req, delta);
6538 prev = del_from;
6539 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006540 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006541 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006542
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006543 /* Look for the appsession cookie unless persistence must be ignored */
6544 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6545 int cmp_len, value_len;
6546 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 if (t->be->options2 & PR_O2_AS_PFX) {
6549 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6550 value_begin = att_beg + t->be->appsession_name_len;
6551 value_len = val_end - att_beg - t->be->appsession_name_len;
6552 } else {
6553 cmp_len = att_end - att_beg;
6554 value_begin = val_beg;
6555 value_len = val_end - val_beg;
6556 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006557
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006558 /* let's see if the cookie is our appcookie */
6559 if (cmp_len == t->be->appsession_name_len &&
6560 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6561 manage_client_side_appsession(t, value_begin, value_len);
6562 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006563 }
6564
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006565 /* continue with next cookie on this header line */
6566 att_beg = next;
6567 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006568
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006569 /* There are no more cookies on this line.
6570 * We may still have one (or several) marked for deletion at the
6571 * end of the line. We must do this now in two ways :
6572 * - if some cookies must be preserved, we only delete from the
6573 * mark to the end of line ;
6574 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006576 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006577 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006578 if (preserve_hdr) {
6579 delta = del_hdr_value(req, &del_from, hdr_end);
6580 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006581 cur_hdr->len += delta;
6582 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006583 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006584
6585 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006586 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6587 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006589 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006590 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006591 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006592 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006593 }
6594
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006595 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006596 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006597 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006598}
6599
6600
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601/* Iterate the same filter through all response headers contained in <rtr>.
6602 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6603 */
6604int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6605{
6606 char term;
6607 char *cur_ptr, *cur_end, *cur_next;
6608 int cur_idx, old_idx, last_hdr;
6609 struct http_txn *txn = &t->txn;
6610 struct hdr_idx_elem *cur_hdr;
6611 int len, delta;
6612
6613 last_hdr = 0;
6614
Willy Tarreau962c3f42010-01-10 00:15:35 +01006615 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616 old_idx = 0;
6617
6618 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006619 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006621 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006622 (exp->action == ACT_ALLOW ||
6623 exp->action == ACT_DENY))
6624 return 0;
6625
6626 cur_idx = txn->hdr_idx.v[old_idx].next;
6627 if (!cur_idx)
6628 break;
6629
6630 cur_hdr = &txn->hdr_idx.v[cur_idx];
6631 cur_ptr = cur_next;
6632 cur_end = cur_ptr + cur_hdr->len;
6633 cur_next = cur_end + cur_hdr->cr + 1;
6634
6635 /* Now we have one header between cur_ptr and cur_end,
6636 * and the next header starts at cur_next.
6637 */
6638
6639 /* The annoying part is that pattern matching needs
6640 * that we modify the contents to null-terminate all
6641 * strings before testing them.
6642 */
6643
6644 term = *cur_end;
6645 *cur_end = '\0';
6646
6647 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6648 switch (exp->action) {
6649 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006650 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006651 last_hdr = 1;
6652 break;
6653
6654 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006655 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006656 last_hdr = 1;
6657 break;
6658
6659 case ACT_REPLACE:
6660 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6661 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6662 /* FIXME: if the user adds a newline in the replacement, the
6663 * index will not be recalculated for now, and the new line
6664 * will not be counted as a new header.
6665 */
6666
6667 cur_end += delta;
6668 cur_next += delta;
6669 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006670 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006671 break;
6672
6673 case ACT_REMOVE:
6674 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6675 cur_next += delta;
6676
Willy Tarreaufa355d42009-11-29 18:12:29 +01006677 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006678 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6679 txn->hdr_idx.used--;
6680 cur_hdr->len = 0;
6681 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006682 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006683 break;
6684
6685 }
6686 }
6687 if (cur_end)
6688 *cur_end = term; /* restore the string terminator */
6689
6690 /* keep the link from this header to next one in case of later
6691 * removal of next header.
6692 */
6693 old_idx = cur_idx;
6694 }
6695 return 0;
6696}
6697
6698
6699/* Apply the filter to the status line in the response buffer <rtr>.
6700 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6701 * or -1 if a replacement resulted in an invalid status line.
6702 */
6703int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6704{
6705 char term;
6706 char *cur_ptr, *cur_end;
6707 int done;
6708 struct http_txn *txn = &t->txn;
6709 int len, delta;
6710
6711
Willy Tarreau3d300592007-03-18 18:34:41 +01006712 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006713 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006714 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006715 (exp->action == ACT_ALLOW ||
6716 exp->action == ACT_DENY))
6717 return 0;
6718 else if (exp->action == ACT_REMOVE)
6719 return 0;
6720
6721 done = 0;
6722
Willy Tarreau962c3f42010-01-10 00:15:35 +01006723 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006724 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006725
6726 /* Now we have the status line between cur_ptr and cur_end */
6727
6728 /* The annoying part is that pattern matching needs
6729 * that we modify the contents to null-terminate all
6730 * strings before testing them.
6731 */
6732
6733 term = *cur_end;
6734 *cur_end = '\0';
6735
6736 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6737 switch (exp->action) {
6738 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006739 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740 done = 1;
6741 break;
6742
6743 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006744 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006745 done = 1;
6746 break;
6747
6748 case ACT_REPLACE:
6749 *cur_end = term; /* restore the string terminator */
6750 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6751 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6752 /* FIXME: if the user adds a newline in the replacement, the
6753 * index will not be recalculated for now, and the new line
6754 * will not be counted as a new header.
6755 */
6756
Willy Tarreaufa355d42009-11-29 18:12:29 +01006757 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006760 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006761 cur_ptr, cur_end + 1,
6762 NULL, NULL);
6763 if (unlikely(!cur_end))
6764 return -1;
6765
6766 /* we have a full respnse and we know that we have either a CR
6767 * or an LF at <ptr>.
6768 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006769 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006770 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771 /* there is no point trying this regex on headers */
6772 return 1;
6773 }
6774 }
6775 *cur_end = term; /* restore the string terminator */
6776 return done;
6777}
6778
6779
6780
6781/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006782 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006783 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6784 * unparsable response.
6785 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006786int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006788 struct http_txn *txn = &s->txn;
6789 struct hdr_exp *exp;
6790
6791 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006792 int ret;
6793
6794 /*
6795 * The interleaving of transformations and verdicts
6796 * makes it difficult to decide to continue or stop
6797 * the evaluation.
6798 */
6799
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006800 if (txn->flags & TX_SVDENY)
6801 break;
6802
Willy Tarreau3d300592007-03-18 18:34:41 +01006803 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006804 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6805 exp->action == ACT_PASS)) {
6806 exp = exp->next;
6807 continue;
6808 }
6809
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006810 /* if this filter had a condition, evaluate it now and skip to
6811 * next filter if the condition does not match.
6812 */
6813 if (exp->cond) {
6814 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6815 ret = acl_pass(ret);
6816 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6817 ret = !ret;
6818 if (!ret)
6819 continue;
6820 }
6821
Willy Tarreaua15645d2007-03-18 16:22:39 +01006822 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006823 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006824 if (unlikely(ret < 0))
6825 return -1;
6826
6827 if (likely(ret == 0)) {
6828 /* The filter did not match the response, it can be
6829 * iterated through all headers.
6830 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006831 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 }
6834 return 0;
6835}
6836
6837
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006839 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006840 * desirable to call it only when needed. This function is also used when we
6841 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006842 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006843void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844{
6845 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006846 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006847 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006849 char *hdr_beg, *hdr_end, *hdr_next;
6850 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006851
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852 /* Iterate through the headers.
6853 * we start with the start line.
6854 */
6855 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006856 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006857
6858 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6859 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006860 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861
6862 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006863 hdr_beg = hdr_next;
6864 hdr_end = hdr_beg + cur_hdr->len;
6865 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006866
Willy Tarreau24581ba2010-08-31 22:39:35 +02006867 /* We have one full header between hdr_beg and hdr_end, and the
6868 * next header starts at hdr_next. We're only interested in
6869 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006870 */
6871
Willy Tarreau24581ba2010-08-31 22:39:35 +02006872 is_cookie2 = 0;
6873 prev = hdr_beg + 10;
6874 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006875 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006876 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6877 if (!val) {
6878 old_idx = cur_idx;
6879 continue;
6880 }
6881 is_cookie2 = 1;
6882 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883 }
6884
Willy Tarreau24581ba2010-08-31 22:39:35 +02006885 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6886 * <prev> points to the colon.
6887 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006888 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006889
Willy Tarreau24581ba2010-08-31 22:39:35 +02006890 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6891 * check-cache is enabled) and we are not interested in checking
6892 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006893 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006894 if (t->be->cookie_name == NULL &&
6895 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006896 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006897 return;
6898
Willy Tarreau24581ba2010-08-31 22:39:35 +02006899 /* OK so now we know we have to process this response cookie.
6900 * The format of the Set-Cookie header is slightly different
6901 * from the format of the Cookie header in that it does not
6902 * support the comma as a cookie delimiter (thus the header
6903 * cannot be folded) because the Expires attribute described in
6904 * the original Netscape's spec may contain an unquoted date
6905 * with a comma inside. We have to live with this because
6906 * many browsers don't support Max-Age and some browsers don't
6907 * support quoted strings. However the Set-Cookie2 header is
6908 * clean.
6909 *
6910 * We have to keep multiple pointers in order to support cookie
6911 * removal at the beginning, middle or end of header without
6912 * corrupting the header (in case of set-cookie2). A special
6913 * pointer, <scav> points to the beginning of the set-cookie-av
6914 * fields after the first semi-colon. The <next> pointer points
6915 * either to the end of line (set-cookie) or next unquoted comma
6916 * (set-cookie2). All of these headers are valid :
6917 *
6918 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6919 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6920 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6921 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6922 * | | | | | | | | | |
6923 * | | | | | | | | +-> next hdr_end <--+
6924 * | | | | | | | +------------> scav
6925 * | | | | | | +--------------> val_end
6926 * | | | | | +--------------------> val_beg
6927 * | | | | +----------------------> equal
6928 * | | | +------------------------> att_end
6929 * | | +----------------------------> att_beg
6930 * | +------------------------------> prev
6931 * +-----------------------------------------> hdr_beg
6932 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006933
Willy Tarreau24581ba2010-08-31 22:39:35 +02006934 for (; prev < hdr_end; prev = next) {
6935 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006936
Willy Tarreau24581ba2010-08-31 22:39:35 +02006937 /* find att_beg */
6938 att_beg = prev + 1;
6939 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6940 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006941
Willy Tarreau24581ba2010-08-31 22:39:35 +02006942 /* find att_end : this is the first character after the last non
6943 * space before the equal. It may be equal to hdr_end.
6944 */
6945 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006946
Willy Tarreau24581ba2010-08-31 22:39:35 +02006947 while (equal < hdr_end) {
6948 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6949 break;
6950 if (http_is_spht[(unsigned char)*equal++])
6951 continue;
6952 att_end = equal;
6953 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006954
Willy Tarreau24581ba2010-08-31 22:39:35 +02006955 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6956 * is between <att_beg> and <equal>, both may be identical.
6957 */
6958
6959 /* look for end of cookie if there is an equal sign */
6960 if (equal < hdr_end && *equal == '=') {
6961 /* look for the beginning of the value */
6962 val_beg = equal + 1;
6963 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6964 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006965
Willy Tarreau24581ba2010-08-31 22:39:35 +02006966 /* find the end of the value, respecting quotes */
6967 next = find_cookie_value_end(val_beg, hdr_end);
6968
6969 /* make val_end point to the first white space or delimitor after the value */
6970 val_end = next;
6971 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6972 val_end--;
6973 } else {
6974 /* <equal> points to next comma, semi-colon or EOL */
6975 val_beg = val_end = next = equal;
6976 }
6977
6978 if (next < hdr_end) {
6979 /* Set-Cookie2 supports multiple cookies, and <next> points to
6980 * a colon or semi-colon before the end. So skip all attr-value
6981 * pairs and look for the next comma. For Set-Cookie, since
6982 * commas are permitted in values, skip to the end.
6983 */
6984 if (is_cookie2)
6985 next = find_hdr_value_end(next, hdr_end);
6986 else
6987 next = hdr_end;
6988 }
6989
6990 /* Now everything is as on the diagram above */
6991
6992 /* Ignore cookies with no equal sign */
6993 if (equal == val_end)
6994 continue;
6995
6996 /* If there are spaces around the equal sign, we need to
6997 * strip them otherwise we'll get trouble for cookie captures,
6998 * or even for rewrites. Since this happens extremely rarely,
6999 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007001 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7002 int stripped_before = 0;
7003 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007004
Willy Tarreau24581ba2010-08-31 22:39:35 +02007005 if (att_end != equal) {
7006 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
7007 equal += stripped_before;
7008 val_beg += stripped_before;
7009 }
7010
7011 if (val_beg > equal + 1) {
7012 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
7013 val_beg += stripped_after;
7014 stripped_before += stripped_after;
7015 }
7016
7017 val_end += stripped_before;
7018 next += stripped_before;
7019 hdr_end += stripped_before;
7020 hdr_next += stripped_before;
7021 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007022 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007023 }
7024
7025 /* First, let's see if we want to capture this cookie. We check
7026 * that we don't already have a server side cookie, because we
7027 * can only capture one. Also as an optimisation, we ignore
7028 * cookies shorter than the declared name.
7029 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007030 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007031 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007032 (val_end - att_beg >= t->fe->capture_namelen) &&
7033 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7034 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007035 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036 Alert("HTTP logging : out of memory.\n");
7037 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007038 else {
7039 if (log_len > t->fe->capture_len)
7040 log_len = t->fe->capture_len;
7041 memcpy(txn->srv_cookie, att_beg, log_len);
7042 txn->srv_cookie[log_len] = 0;
7043 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007044 }
7045
Willy Tarreau827aee92011-03-10 16:55:02 +01007046 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007048 if (!(t->flags & SN_IGNORE_PRST) &&
7049 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7050 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007051 /* assume passive cookie by default */
7052 txn->flags &= ~TX_SCK_MASK;
7053 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007054
7055 /* If the cookie is in insert mode on a known server, we'll delete
7056 * this occurrence because we'll insert another one later.
7057 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007058 * a direct access.
7059 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007060 if (t->be->options2 & PR_O2_COOK_PSV) {
7061 /* The "preserve" flag was set, we don't want to touch the
7062 * server's cookie.
7063 */
7064 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007065 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007066 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007067 /* this cookie must be deleted */
7068 if (*prev == ':' && next == hdr_end) {
7069 /* whole header */
7070 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
7071 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7072 txn->hdr_idx.used--;
7073 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007074 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007075 hdr_next += delta;
7076 http_msg_move_end(&txn->rsp, delta);
7077 /* note: while both invalid now, <next> and <hdr_end>
7078 * are still equal, so the for() will stop as expected.
7079 */
7080 } else {
7081 /* just remove the value */
7082 int delta = del_hdr_value(res, &prev, next);
7083 next = prev;
7084 hdr_end += delta;
7085 hdr_next += delta;
7086 cur_hdr->len += delta;
7087 http_msg_move_end(&txn->rsp, delta);
7088 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007089 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007090 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007091 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007092 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007093 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007094 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007095 * with this server since we know it.
7096 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007097 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007098 next += delta;
7099 hdr_end += delta;
7100 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007101 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007102 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007103
Willy Tarreauf1348312010-10-07 15:54:11 +02007104 txn->flags &= ~TX_SCK_MASK;
7105 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007106 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007107 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007108 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007109 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007110 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007111 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007112 next += delta;
7113 hdr_end += delta;
7114 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007115 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007116 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007117
Willy Tarreau827aee92011-03-10 16:55:02 +01007118 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007119 txn->flags &= ~TX_SCK_MASK;
7120 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007121 }
7122 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007123 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7124 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007125 int cmp_len, value_len;
7126 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007127
Cyril Bontéb21570a2009-11-29 20:04:48 +01007128 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007129 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7130 value_begin = att_beg + t->be->appsession_name_len;
7131 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007132 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007133 cmp_len = att_end - att_beg;
7134 value_begin = val_beg;
7135 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007136 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007137
Cyril Bonté17530c32010-04-06 21:11:10 +02007138 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007139 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7140 /* free a possibly previously allocated memory */
7141 pool_free2(apools.sessid, txn->sessid);
7142
Cyril Bontéb21570a2009-11-29 20:04:48 +01007143 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007144 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007145 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7146 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7147 return;
7148 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007149 memcpy(txn->sessid, value_begin, value_len);
7150 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007151 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007152 }
7153 /* that's done for this cookie, check the next one on the same
7154 * line when next != hdr_end (only if is_cookie2).
7155 */
7156 }
7157 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007158 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007159 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007160
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007161 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007162 appsess *asession = NULL;
7163 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007164 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007165 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007166 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007167 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7168 Alert("Not enough Memory process_srv():asession:calloc().\n");
7169 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7170 return;
7171 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007172 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7173
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007174 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7175 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7176 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007177 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007178 return;
7179 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007180 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007181 asession->sessid[t->be->appsession_len] = 0;
7182
Willy Tarreau827aee92011-03-10 16:55:02 +01007183 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007184 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007185 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007186 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007187 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007188 return;
7189 }
7190 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007191 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007192
7193 asession->request_count = 0;
7194 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7195 }
7196
7197 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7198 asession->request_count++;
7199 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007200}
7201
7202
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203/*
7204 * Check if response is cacheable or not. Updates t->flags.
7205 */
7206void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7207{
7208 struct http_txn *txn = &t->txn;
7209 char *p1, *p2;
7210
7211 char *cur_ptr, *cur_end, *cur_next;
7212 int cur_idx;
7213
Willy Tarreau5df51872007-11-25 16:20:08 +01007214 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007215 return;
7216
7217 /* Iterate through the headers.
7218 * we start with the start line.
7219 */
7220 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007221 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007222
7223 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7224 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007225 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007226
7227 cur_hdr = &txn->hdr_idx.v[cur_idx];
7228 cur_ptr = cur_next;
7229 cur_end = cur_ptr + cur_hdr->len;
7230 cur_next = cur_end + cur_hdr->cr + 1;
7231
7232 /* We have one full header between cur_ptr and cur_end, and the
7233 * next header starts at cur_next. We're only interested in
7234 * "Cookie:" headers.
7235 */
7236
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007237 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7238 if (val) {
7239 if ((cur_end - (cur_ptr + val) >= 8) &&
7240 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7241 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7242 return;
7243 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007244 }
7245
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007246 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7247 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007248 continue;
7249
7250 /* OK, right now we know we have a cache-control header at cur_ptr */
7251
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007252 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007253
7254 if (p1 >= cur_end) /* no more info */
7255 continue;
7256
7257 /* p1 is at the beginning of the value */
7258 p2 = p1;
7259
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007260 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007261 p2++;
7262
7263 /* we have a complete value between p1 and p2 */
7264 if (p2 < cur_end && *p2 == '=') {
7265 /* we have something of the form no-cache="set-cookie" */
7266 if ((cur_end - p1 >= 21) &&
7267 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7268 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007269 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007270 continue;
7271 }
7272
7273 /* OK, so we know that either p2 points to the end of string or to a comma */
7274 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7275 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7276 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7277 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007278 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007279 return;
7280 }
7281
7282 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007283 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007284 continue;
7285 }
7286 }
7287}
7288
7289
Willy Tarreau58f10d72006-12-04 02:26:12 +01007290/*
7291 * Try to retrieve a known appsession in the URI, then the associated server.
7292 * If the server is found, it's assigned to the session.
7293 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007294void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007295{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007296 char *end_params, *first_param, *cur_param, *next_param;
7297 char separator;
7298 int value_len;
7299
7300 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007301
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007302 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007303 (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 +01007304 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007305 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007306
Cyril Bontéb21570a2009-11-29 20:04:48 +01007307 first_param = NULL;
7308 switch (mode) {
7309 case PR_O2_AS_M_PP:
7310 first_param = memchr(begin, ';', len);
7311 break;
7312 case PR_O2_AS_M_QS:
7313 first_param = memchr(begin, '?', len);
7314 break;
7315 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007316
Cyril Bontéb21570a2009-11-29 20:04:48 +01007317 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007319 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007320
Cyril Bontéb21570a2009-11-29 20:04:48 +01007321 switch (mode) {
7322 case PR_O2_AS_M_PP:
7323 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7324 end_params = (char *) begin + len;
7325 }
7326 separator = ';';
7327 break;
7328 case PR_O2_AS_M_QS:
7329 end_params = (char *) begin + len;
7330 separator = '&';
7331 break;
7332 default:
7333 /* unknown mode, shouldn't happen */
7334 return;
7335 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007336
Cyril Bontéb21570a2009-11-29 20:04:48 +01007337 cur_param = next_param = end_params;
7338 while (cur_param > first_param) {
7339 cur_param--;
7340 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7341 /* let's see if this is the appsession parameter */
7342 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7343 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7344 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7345 /* Cool... it's the right one */
7346 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7347 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7348 if (value_len > 0) {
7349 manage_client_side_appsession(t, cur_param, value_len);
7350 }
7351 break;
7352 }
7353 next_param = cur_param;
7354 }
7355 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007356#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007357 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007358 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007359#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007360}
7361
Willy Tarreaub2513902006-12-17 14:52:38 +01007362/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007363 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007364 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007365 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007366 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007367 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007368 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007369 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007370 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007371int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007372{
7373 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007374 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007375
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007376 if (!uri_auth)
7377 return 0;
7378
Cyril Bonté70be45d2010-10-12 00:14:35 +02007379 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007380 return 0;
7381
Willy Tarreau295a8372011-03-10 11:25:07 +01007382 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007383
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007384 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007385 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007386 return 0;
7387
Willy Tarreau962c3f42010-01-10 00:15:35 +01007388 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007389
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007390 /* the URI is in h */
7391 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007392 return 0;
7393
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007394 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007395 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007396 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007397 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007398 break;
7399 }
7400 h++;
7401 }
7402
7403 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007404 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7405 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007406 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007407 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007408 break;
7409 }
7410 h++;
7411 }
7412 }
7413
Willy Tarreau962c3f42010-01-10 00:15:35 +01007414 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7415 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007416 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007417 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007418 break;
7419 }
7420 h++;
7421 }
7422
Cyril Bonté70be45d2010-10-12 00:14:35 +02007423 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7424 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7425 if (memcmp(h, ";st=", 4) == 0) {
7426 h += 4;
7427
7428 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007429 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007430 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007431 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007432 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007433 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007434 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007435 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007436 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007437 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007438 break;
7439 }
7440 h++;
7441 }
7442
Willy Tarreau295a8372011-03-10 11:25:07 +01007443 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007444
Willy Tarreaub2513902006-12-17 14:52:38 +01007445 return 1;
7446}
7447
Willy Tarreau4076a152009-04-02 15:18:36 +02007448/*
7449 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007450 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7451 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007452 */
7453void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7454 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007455 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007456{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007457 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7458 int len1 = buf->size - msg->som;
7459 es->len = buf->r - (buf->data + msg->som) + buf->size;
7460 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7461 if (es->len > len1 && len1 < sizeof(es->buf))
7462 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7463 }
7464 else {
7465 es->len = buf->r - (buf->data + msg->som);
7466 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7467 }
7468
Willy Tarreau4076a152009-04-02 15:18:36 +02007469 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007470 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007471 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007472 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007473 else
7474 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7475
Willy Tarreau4076a152009-04-02 15:18:36 +02007476 es->when = date; // user-visible date
7477 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007478 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007479 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007480 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007481 es->state = state;
7482 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007483 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007484}
Willy Tarreaub2513902006-12-17 14:52:38 +01007485
Willy Tarreaubce70882009-09-07 11:51:47 +02007486/* return the IP address pointed to by occurrence <occ> of header <hname> in
7487 * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the
7488 * occurrence number corresponding to this value is returned. If <occ> is
7489 * strictly negative, the occurrence number before the end corresponding to
7490 * this value is returned. If <occ> is null, any value is returned, so it is
7491 * not recommended to use it that way. Negative occurrences are limited to
7492 * a small value because it is required to keep them in memory while scanning.
7493 * IP address 0.0.0.0 is returned if no match is found.
7494 */
7495unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ)
7496{
7497 struct hdr_ctx ctx;
7498 unsigned int hdr_hist[MAX_HDR_HISTORY];
7499 unsigned int hist_ptr;
7500 int found = 0;
7501
7502 ctx.idx = 0;
7503 if (occ >= 0) {
7504 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7505 occ--;
7506 if (occ <= 0) {
7507 found = 1;
7508 break;
7509 }
7510 }
7511 if (!found)
7512 return 0;
7513 return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7514 }
7515
7516 /* negative occurrence, we scan all the list then walk back */
7517 if (-occ > MAX_HDR_HISTORY)
7518 return 0;
7519
7520 hist_ptr = 0;
7521 hdr_hist[hist_ptr] = 0;
7522 while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) {
7523 hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen);
7524 if (hist_ptr >= MAX_HDR_HISTORY)
7525 hist_ptr = 0;
7526 found++;
7527 }
7528 if (-occ > found)
7529 return 0;
7530 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7531 * find occurrence -occ, so we have to check [hist_ptr+occ].
7532 */
7533 hist_ptr += occ;
7534 if (hist_ptr >= MAX_HDR_HISTORY)
7535 hist_ptr -= MAX_HDR_HISTORY;
7536 return hdr_hist[hist_ptr];
7537}
7538
Willy Tarreaubaaee002006-06-26 02:48:02 +02007539/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007540 * Print a debug line with a header
7541 */
7542void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7543{
7544 int len, max;
7545 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007546 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007547 max = end - start;
7548 UBOUND(max, sizeof(trash) - len - 1);
7549 len += strlcpy2(trash + len, start, max + 1);
7550 trash[len++] = '\n';
7551 write(1, trash, len);
7552}
7553
Willy Tarreau0937bc42009-12-22 15:03:09 +01007554/*
7555 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7556 * the required fields are properly allocated and that we only need to (re)init
7557 * them. This should be used before processing any new request.
7558 */
7559void http_init_txn(struct session *s)
7560{
7561 struct http_txn *txn = &s->txn;
7562 struct proxy *fe = s->fe;
7563
7564 txn->flags = 0;
7565 txn->status = -1;
7566
Willy Tarreauf64d1412010-10-07 20:06:11 +02007567 txn->cookie_first_date = 0;
7568 txn->cookie_last_date = 0;
7569
Willy Tarreau0937bc42009-12-22 15:03:09 +01007570 txn->req.sol = txn->req.eol = NULL;
7571 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7572 txn->rsp.sol = txn->rsp.eol = NULL;
7573 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007574 txn->req.chunk_len = 0LL;
7575 txn->req.body_len = 0LL;
7576 txn->rsp.chunk_len = 0LL;
7577 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007578 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7579 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007580
7581 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007582
7583 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7584 if (fe->options2 & PR_O2_REQBUG_OK)
7585 txn->req.err_pos = -1; /* let buggy requests pass */
7586
Willy Tarreau46023632010-01-07 22:51:47 +01007587 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007588 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7589
Willy Tarreau46023632010-01-07 22:51:47 +01007590 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007591 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7592
7593 if (txn->hdr_idx.v)
7594 hdr_idx_init(&txn->hdr_idx);
7595}
7596
7597/* to be used at the end of a transaction */
7598void http_end_txn(struct session *s)
7599{
7600 struct http_txn *txn = &s->txn;
7601
7602 /* these ones will have been dynamically allocated */
7603 pool_free2(pool2_requri, txn->uri);
7604 pool_free2(pool2_capture, txn->cli_cookie);
7605 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007606 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007607
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007608 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007609 txn->uri = NULL;
7610 txn->srv_cookie = NULL;
7611 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007612
7613 if (txn->req.cap) {
7614 struct cap_hdr *h;
7615 for (h = s->fe->req_cap; h; h = h->next)
7616 pool_free2(h->pool, txn->req.cap[h->index]);
7617 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7618 }
7619
7620 if (txn->rsp.cap) {
7621 struct cap_hdr *h;
7622 for (h = s->fe->rsp_cap; h; h = h->next)
7623 pool_free2(h->pool, txn->rsp.cap[h->index]);
7624 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7625 }
7626
Willy Tarreau0937bc42009-12-22 15:03:09 +01007627}
7628
7629/* to be used at the end of a transaction to prepare a new one */
7630void http_reset_txn(struct session *s)
7631{
7632 http_end_txn(s);
7633 http_init_txn(s);
7634
7635 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007636 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007637 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007638 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007639 /* re-init store persistence */
7640 s->store_count = 0;
7641
Willy Tarreau0937bc42009-12-22 15:03:09 +01007642 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007643
7644 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7645
Willy Tarreau739cfba2010-01-25 23:11:14 +01007646 /* We must trim any excess data from the response buffer, because we
7647 * may have blocked an invalid response from a server that we don't
7648 * want to accidentely forward once we disable the analysers, nor do
7649 * we want those data to come along with next response. A typical
7650 * example of such data would be from a buggy server responding to
7651 * a HEAD with some data, or sending more than the advertised
7652 * content-length.
7653 */
7654 if (unlikely(s->rep->l > s->rep->send_max)) {
7655 s->rep->l = s->rep->send_max;
7656 s->rep->r = s->rep->w + s->rep->l;
7657 if (s->rep->r >= s->rep->data + s->rep->size)
7658 s->rep->r -= s->rep->size;
7659 }
7660
Willy Tarreau0937bc42009-12-22 15:03:09 +01007661 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007662 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007663
Willy Tarreaud04e8582010-05-31 12:31:35 +02007664 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007665 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007666
7667 s->req->rex = TICK_ETERNITY;
7668 s->req->wex = TICK_ETERNITY;
7669 s->req->analyse_exp = TICK_ETERNITY;
7670 s->rep->rex = TICK_ETERNITY;
7671 s->rep->wex = TICK_ETERNITY;
7672 s->rep->analyse_exp = TICK_ETERNITY;
7673}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007674
Willy Tarreauff011f22011-01-06 17:51:27 +01007675void free_http_req_rules(struct list *r) {
7676 struct http_req_rule *tr, *pr;
7677
7678 list_for_each_entry_safe(pr, tr, r, list) {
7679 LIST_DEL(&pr->list);
7680 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7681 free(pr->http_auth.realm);
7682
7683 free(pr);
7684 }
7685}
7686
7687struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7688{
7689 struct http_req_rule *rule;
7690 int cur_arg;
7691
7692 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7693 if (!rule) {
7694 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7695 return NULL;
7696 }
7697
7698 if (!*args[0]) {
7699 goto req_error_parsing;
7700 } else if (!strcmp(args[0], "allow")) {
7701 rule->action = HTTP_REQ_ACT_ALLOW;
7702 cur_arg = 1;
7703 } else if (!strcmp(args[0], "deny")) {
7704 rule->action = HTTP_REQ_ACT_DENY;
7705 cur_arg = 1;
7706 } else if (!strcmp(args[0], "auth")) {
7707 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7708 cur_arg = 1;
7709
7710 while(*args[cur_arg]) {
7711 if (!strcmp(args[cur_arg], "realm")) {
7712 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7713 cur_arg+=2;
7714 continue;
7715 } else
7716 break;
7717 }
7718 } else {
7719req_error_parsing:
7720 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7721 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7722 return NULL;
7723 }
7724
7725 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7726 struct acl_cond *cond;
7727
7728 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7729 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7730 file, linenum, args[0]);
7731 return NULL;
7732 }
7733 rule->cond = cond;
7734 }
7735 else if (*args[cur_arg]) {
7736 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7737 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7738 file, linenum, args[0], args[cur_arg]);
7739 return NULL;
7740 }
7741
7742 return rule;
7743}
7744
Willy Tarreau8797c062007-05-07 00:55:35 +02007745/************************************************************************/
7746/* The code below is dedicated to ACL parsing and matching */
7747/************************************************************************/
7748
7749
7750
7751
7752/* 1. Check on METHOD
7753 * We use the pre-parsed method if it is known, and store its number as an
7754 * integer. If it is unknown, we use the pointer and the length.
7755 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007756static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007757{
7758 int len, meth;
7759
Willy Tarreauae8b7962007-06-09 23:10:04 +02007760 len = strlen(*text);
7761 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007762
7763 pattern->val.i = meth;
7764 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007765 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007766 if (!pattern->ptr.str)
7767 return 0;
7768 pattern->len = len;
7769 }
7770 return 1;
7771}
7772
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007773/* This function fetches the method of current HTTP request and stores
7774 * it in the global pattern struct as a chunk. There are two possibilities :
7775 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7776 * in <len> and <ptr> is NULL ;
7777 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7778 * <len> to its length.
7779 * This is intended to be used with acl_match_meth() only.
7780 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007781static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007782acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7783 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007784{
7785 int meth;
7786 struct http_txn *txn = l7;
7787
Willy Tarreaub6866442008-07-14 23:54:42 +02007788 if (!txn)
7789 return 0;
7790
Willy Tarreau655dce92009-11-08 13:10:58 +01007791 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007792 return 0;
7793
Willy Tarreau8797c062007-05-07 00:55:35 +02007794 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007795 temp_pattern.data.str.len = meth;
7796 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007797 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007798 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7799 /* ensure the indexes are not affected */
7800 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007801 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
7802 temp_pattern.data.str.str = txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007803 }
7804 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7805 return 1;
7806}
7807
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007808/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007809static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7810{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007811 int icase;
7812
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007813
7814 if (temp_pattern.data.str.str == NULL) {
7815 /* well-known method */
7816 if (temp_pattern.data.str.len == pattern->val.i)
7817 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007818 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007819 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007820
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007821 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7822 if (pattern->val.i != HTTP_METH_OTHER)
7823 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007824
7825 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007826 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007827 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007828
7829 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007830 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7831 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007832 return ACL_PAT_FAIL;
7833 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007834}
7835
7836/* 2. Check on Request/Status Version
7837 * We simply compare strings here.
7838 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007839static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007840{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007841 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007842 if (!pattern->ptr.str)
7843 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007844 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007845 return 1;
7846}
7847
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007848static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007849acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7850 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007851{
7852 struct http_txn *txn = l7;
7853 char *ptr;
7854 int len;
7855
Willy Tarreaub6866442008-07-14 23:54:42 +02007856 if (!txn)
7857 return 0;
7858
Willy Tarreau655dce92009-11-08 13:10:58 +01007859 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007860 return 0;
7861
Willy Tarreau8797c062007-05-07 00:55:35 +02007862 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007863 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007864
7865 while ((len-- > 0) && (*ptr++ != '/'));
7866 if (len <= 0)
7867 return 0;
7868
7869 test->ptr = ptr;
7870 test->len = len;
7871
7872 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7873 return 1;
7874}
7875
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007876static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007877acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7878 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007879{
7880 struct http_txn *txn = l7;
7881 char *ptr;
7882 int len;
7883
Willy Tarreaub6866442008-07-14 23:54:42 +02007884 if (!txn)
7885 return 0;
7886
Willy Tarreau655dce92009-11-08 13:10:58 +01007887 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007888 return 0;
7889
Willy Tarreau8797c062007-05-07 00:55:35 +02007890 len = txn->rsp.sl.st.v_l;
7891 ptr = txn->rsp.sol;
7892
7893 while ((len-- > 0) && (*ptr++ != '/'));
7894 if (len <= 0)
7895 return 0;
7896
7897 test->ptr = ptr;
7898 test->len = len;
7899
7900 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7901 return 1;
7902}
7903
7904/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007905static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007906acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7907 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007908{
7909 struct http_txn *txn = l7;
7910 char *ptr;
7911 int len;
7912
Willy Tarreaub6866442008-07-14 23:54:42 +02007913 if (!txn)
7914 return 0;
7915
Willy Tarreau655dce92009-11-08 13:10:58 +01007916 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007917 return 0;
7918
Willy Tarreau8797c062007-05-07 00:55:35 +02007919 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007920 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007921
7922 test->i = __strl2ui(ptr, len);
7923 test->flags = ACL_TEST_F_VOL_1ST;
7924 return 1;
7925}
7926
7927/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007928static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007929acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7930 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007931{
7932 struct http_txn *txn = l7;
7933
Willy Tarreaub6866442008-07-14 23:54:42 +02007934 if (!txn)
7935 return 0;
7936
Willy Tarreau655dce92009-11-08 13:10:58 +01007937 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007938 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007939
Willy Tarreauc11416f2007-06-17 16:58:38 +02007940 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7941 /* ensure the indexes are not affected */
7942 return 0;
7943
Willy Tarreau8797c062007-05-07 00:55:35 +02007944 test->len = txn->req.sl.rq.u_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007945 test->ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007946
Willy Tarreauf3d25982007-05-08 22:45:09 +02007947 /* we do not need to set READ_ONLY because the data is in a buffer */
7948 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007949 return 1;
7950}
7951
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007952static int
7953acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7954 struct acl_expr *expr, struct acl_test *test)
7955{
7956 struct http_txn *txn = l7;
7957
Willy Tarreaub6866442008-07-14 23:54:42 +02007958 if (!txn)
7959 return 0;
7960
Willy Tarreau655dce92009-11-08 13:10:58 +01007961 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007962 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007963
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007964 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7965 /* ensure the indexes are not affected */
7966 return 0;
7967
7968 /* Parse HTTP request */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007969 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
7970 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007971 test->i = AF_INET;
7972
7973 /*
7974 * If we are parsing url in frontend space, we prepare backend stage
7975 * to not parse again the same url ! optimization lazyness...
7976 */
7977 if (px->options & PR_O_HTTP_PROXY)
7978 l4->flags |= SN_ADDR_SET;
7979
7980 test->flags = ACL_TEST_F_READ_ONLY;
7981 return 1;
7982}
7983
7984static int
7985acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7986 struct acl_expr *expr, struct acl_test *test)
7987{
7988 struct http_txn *txn = l7;
7989
Willy Tarreaub6866442008-07-14 23:54:42 +02007990 if (!txn)
7991 return 0;
7992
Willy Tarreau655dce92009-11-08 13:10:58 +01007993 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007994 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007995
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007996 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7997 /* ensure the indexes are not affected */
7998 return 0;
7999
8000 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02008001 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
8002 test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008003
8004 if (px->options & PR_O_HTTP_PROXY)
8005 l4->flags |= SN_ADDR_SET;
8006
8007 test->flags = ACL_TEST_F_READ_ONLY;
8008 return 1;
8009}
8010
Willy Tarreauc11416f2007-06-17 16:58:38 +02008011/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
8012 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
8013 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008014static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008015acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008016 struct acl_expr *expr, struct acl_test *test)
8017{
8018 struct http_txn *txn = l7;
8019 struct hdr_idx *idx = &txn->hdr_idx;
8020 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008021
Willy Tarreaub6866442008-07-14 23:54:42 +02008022 if (!txn)
8023 return 0;
8024
Willy Tarreau33a7e692007-06-10 19:45:56 +02008025 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8026 /* search for header from the beginning */
8027 ctx->idx = 0;
8028
Willy Tarreau33a7e692007-06-10 19:45:56 +02008029 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8030 test->flags |= ACL_TEST_F_FETCH_MORE;
8031 test->flags |= ACL_TEST_F_VOL_HDR;
8032 test->len = ctx->vlen;
8033 test->ptr = (char *)ctx->line + ctx->val;
8034 return 1;
8035 }
8036
8037 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8038 test->flags |= ACL_TEST_F_VOL_HDR;
8039 return 0;
8040}
8041
Willy Tarreau33a7e692007-06-10 19:45:56 +02008042static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008043acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
8044 struct acl_expr *expr, struct acl_test *test)
8045{
8046 struct http_txn *txn = l7;
8047
Willy Tarreaub6866442008-07-14 23:54:42 +02008048 if (!txn)
8049 return 0;
8050
Willy Tarreau655dce92009-11-08 13:10:58 +01008051 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008052 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008053
Willy Tarreauc11416f2007-06-17 16:58:38 +02008054 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8055 /* ensure the indexes are not affected */
8056 return 0;
8057
8058 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
8059}
8060
8061static int
8062acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
8063 struct acl_expr *expr, struct acl_test *test)
8064{
8065 struct http_txn *txn = l7;
8066
Willy Tarreaub6866442008-07-14 23:54:42 +02008067 if (!txn)
8068 return 0;
8069
Willy Tarreau655dce92009-11-08 13:10:58 +01008070 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008071 return 0;
8072
8073 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
8074}
8075
8076/* 6. Check on HTTP header count. The number of occurrences is returned.
8077 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8078 */
8079static int
8080acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008081 struct acl_expr *expr, struct acl_test *test)
8082{
8083 struct http_txn *txn = l7;
8084 struct hdr_idx *idx = &txn->hdr_idx;
8085 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008086 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008087
Willy Tarreaub6866442008-07-14 23:54:42 +02008088 if (!txn)
8089 return 0;
8090
Willy Tarreau33a7e692007-06-10 19:45:56 +02008091 ctx.idx = 0;
8092 cnt = 0;
8093 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
8094 cnt++;
8095
8096 test->i = cnt;
8097 test->flags = ACL_TEST_F_VOL_HDR;
8098 return 1;
8099}
8100
Willy Tarreauc11416f2007-06-17 16:58:38 +02008101static int
8102acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8103 struct acl_expr *expr, struct acl_test *test)
8104{
8105 struct http_txn *txn = l7;
8106
Willy Tarreaub6866442008-07-14 23:54:42 +02008107 if (!txn)
8108 return 0;
8109
Willy Tarreau655dce92009-11-08 13:10:58 +01008110 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008111 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008112
Willy Tarreauc11416f2007-06-17 16:58:38 +02008113 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8114 /* ensure the indexes are not affected */
8115 return 0;
8116
8117 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
8118}
8119
8120static int
8121acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8122 struct acl_expr *expr, struct acl_test *test)
8123{
8124 struct http_txn *txn = l7;
8125
Willy Tarreaub6866442008-07-14 23:54:42 +02008126 if (!txn)
8127 return 0;
8128
Willy Tarreau655dce92009-11-08 13:10:58 +01008129 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008130 return 0;
8131
8132 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
8133}
8134
Willy Tarreau33a7e692007-06-10 19:45:56 +02008135/* 7. Check on HTTP header's integer value. The integer value is returned.
8136 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008137 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008138 */
8139static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008140acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008141 struct acl_expr *expr, struct acl_test *test)
8142{
8143 struct http_txn *txn = l7;
8144 struct hdr_idx *idx = &txn->hdr_idx;
8145 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008146
Willy Tarreaub6866442008-07-14 23:54:42 +02008147 if (!txn)
8148 return 0;
8149
Willy Tarreau33a7e692007-06-10 19:45:56 +02008150 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8151 /* search for header from the beginning */
8152 ctx->idx = 0;
8153
Willy Tarreau33a7e692007-06-10 19:45:56 +02008154 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8155 test->flags |= ACL_TEST_F_FETCH_MORE;
8156 test->flags |= ACL_TEST_F_VOL_HDR;
8157 test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
8158 return 1;
8159 }
8160
8161 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8162 test->flags |= ACL_TEST_F_VOL_HDR;
8163 return 0;
8164}
8165
Willy Tarreauc11416f2007-06-17 16:58:38 +02008166static int
8167acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8168 struct acl_expr *expr, struct acl_test *test)
8169{
8170 struct http_txn *txn = l7;
8171
Willy Tarreaub6866442008-07-14 23:54:42 +02008172 if (!txn)
8173 return 0;
8174
Willy Tarreau655dce92009-11-08 13:10:58 +01008175 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008176 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008177
Willy Tarreauc11416f2007-06-17 16:58:38 +02008178 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8179 /* ensure the indexes are not affected */
8180 return 0;
8181
8182 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
8183}
8184
8185static int
8186acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8187 struct acl_expr *expr, struct acl_test *test)
8188{
8189 struct http_txn *txn = l7;
8190
Willy Tarreaub6866442008-07-14 23:54:42 +02008191 if (!txn)
8192 return 0;
8193
Willy Tarreau655dce92009-11-08 13:10:58 +01008194 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008195 return 0;
8196
8197 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8198}
8199
Willy Tarreau106f9792009-09-19 07:54:16 +02008200/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8201 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8202 */
8203static int
8204acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8205 struct acl_expr *expr, struct acl_test *test)
8206{
8207 struct http_txn *txn = l7;
8208 struct hdr_idx *idx = &txn->hdr_idx;
8209 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8210
8211 if (!txn)
8212 return 0;
8213
8214 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8215 /* search for header from the beginning */
8216 ctx->idx = 0;
8217
8218 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8219 test->flags |= ACL_TEST_F_FETCH_MORE;
8220 test->flags |= ACL_TEST_F_VOL_HDR;
8221 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02008222 memset(&((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr, 0, sizeof(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr));
8223 url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr);
8224 test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Willy Tarreau106f9792009-09-19 07:54:16 +02008225 test->i = AF_INET;
8226 return 1;
8227 }
8228
8229 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8230 test->flags |= ACL_TEST_F_VOL_HDR;
8231 return 0;
8232}
8233
8234static int
8235acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8236 struct acl_expr *expr, struct acl_test *test)
8237{
8238 struct http_txn *txn = l7;
8239
8240 if (!txn)
8241 return 0;
8242
Willy Tarreau655dce92009-11-08 13:10:58 +01008243 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008244 return 0;
8245
8246 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8247 /* ensure the indexes are not affected */
8248 return 0;
8249
8250 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8251}
8252
8253static int
8254acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8255 struct acl_expr *expr, struct acl_test *test)
8256{
8257 struct http_txn *txn = l7;
8258
8259 if (!txn)
8260 return 0;
8261
Willy Tarreau655dce92009-11-08 13:10:58 +01008262 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008263 return 0;
8264
8265 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8266}
8267
Willy Tarreau737b0c12007-06-10 21:28:46 +02008268/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8269 * the first '/' after the possible hostname, and ends before the possible '?'.
8270 */
8271static int
8272acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8273 struct acl_expr *expr, struct acl_test *test)
8274{
8275 struct http_txn *txn = l7;
8276 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008277
Willy Tarreaub6866442008-07-14 23:54:42 +02008278 if (!txn)
8279 return 0;
8280
Willy Tarreau655dce92009-11-08 13:10:58 +01008281 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008282 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008283
Willy Tarreauc11416f2007-06-17 16:58:38 +02008284 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8285 /* ensure the indexes are not affected */
8286 return 0;
8287
Willy Tarreau962c3f42010-01-10 00:15:35 +01008288 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008289 ptr = http_get_path(txn);
8290 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008291 return 0;
8292
8293 /* OK, we got the '/' ! */
8294 test->ptr = ptr;
8295
8296 while (ptr < end && *ptr != '?')
8297 ptr++;
8298
8299 test->len = ptr - test->ptr;
8300
8301 /* we do not need to set READ_ONLY because the data is in a buffer */
8302 test->flags = ACL_TEST_F_VOL_1ST;
8303 return 1;
8304}
8305
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008306static int
8307acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8308 struct acl_expr *expr, struct acl_test *test)
8309{
8310 struct buffer *req = s->req;
8311 struct http_txn *txn = &s->txn;
8312 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008313
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008314 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8315 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8316 */
8317
8318 if (!s || !req)
8319 return 0;
8320
Willy Tarreau655dce92009-11-08 13:10:58 +01008321 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008322 /* Already decoded as OK */
8323 test->flags |= ACL_TEST_F_SET_RES_PASS;
8324 return 1;
8325 }
8326
8327 /* Try to decode HTTP request */
8328 if (likely(req->lr < req->r))
8329 http_msg_analyzer(req, msg, &txn->hdr_idx);
8330
Willy Tarreau655dce92009-11-08 13:10:58 +01008331 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008332 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8333 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8334 return 1;
8335 }
8336 /* wait for final state */
8337 test->flags |= ACL_TEST_F_MAY_CHANGE;
8338 return 0;
8339 }
8340
8341 /* OK we got a valid HTTP request. We have some minor preparation to
8342 * perform so that further checks can rely on HTTP tests.
8343 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008344 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008345 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8346 s->flags |= SN_REDIRECTABLE;
8347
8348 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8349 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8350 return 1;
8351 }
8352
8353 test->flags |= ACL_TEST_F_SET_RES_PASS;
8354 return 1;
8355}
8356
Willy Tarreau7f18e522010-10-22 20:04:13 +02008357/* return a valid test if the current request is the first one on the connection */
8358static int
8359acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8360 struct acl_expr *expr, struct acl_test *test)
8361{
8362 if (!s)
8363 return 0;
8364
8365 if (s->txn.flags & TX_NOT_FIRST)
8366 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8367 else
8368 test->flags |= ACL_TEST_F_SET_RES_PASS;
8369
8370 return 1;
8371}
8372
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008373static int
8374acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8375 struct acl_expr *expr, struct acl_test *test)
8376{
8377
8378 if (!s)
8379 return 0;
8380
8381 if (!get_http_auth(s))
8382 return 0;
8383
8384 test->ctx.a[0] = expr->arg.ul;
8385 test->ctx.a[1] = s->txn.auth.user;
8386 test->ctx.a[2] = s->txn.auth.pass;
8387
8388 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8389
8390 return 1;
8391}
Willy Tarreau8797c062007-05-07 00:55:35 +02008392
8393/************************************************************************/
8394/* All supported keywords must be declared here. */
8395/************************************************************************/
8396
8397/* Note: must not be declared <const> as its list will be overwritten */
8398static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008399 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8400
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008401 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008402 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8403 { "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 +02008404 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008405
Willy Tarreauc4262962010-05-10 23:42:40 +02008406 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008407 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8408 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8409 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8410 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8411 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8412 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008413 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008414 { "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 +02008415 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008416
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008417 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008418 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008419 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8420 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8421 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8422 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8423 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8424 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8425 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008426 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008427 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008428 { "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 +02008429
Willy Tarreauc4262962010-05-10 23:42:40 +02008430 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008431 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8432 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8433 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8434 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8435 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8436 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8437 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008438 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008439 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008440 { "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 +02008441
Willy Tarreauc4262962010-05-10 23:42:40 +02008442 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008443 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8444 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8445 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8446 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8447 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8448 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008449 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008450
Willy Tarreauf3d25982007-05-08 22:45:09 +02008451#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008452 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8453 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8454 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8455 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8456 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8457 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8458 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8459
Willy Tarreau8797c062007-05-07 00:55:35 +02008460 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8461 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8462 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8463 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8464 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8465 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8466 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8467 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008468#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008469
Willy Tarreau7f18e522010-10-22 20:04:13 +02008470 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8471 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8472 { "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 +02008473 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008474}};
8475
Willy Tarreau4a568972010-05-12 08:08:50 +02008476/************************************************************************/
8477/* The code below is dedicated to pattern fetching and matching */
8478/************************************************************************/
8479
8480/* extract the IP address from the last occurrence of specified header. Note
8481 * that we should normally first extract the string then convert it to IP,
8482 * but right now we have all the functions to do this seemlessly, and we will
8483 * be able to change that later without touching the configuration.
8484 */
8485static int
8486pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Emeric Brun485479d2010-09-23 18:02:19 +02008487 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008488{
8489 struct http_txn *txn = l7;
8490
Emeric Brun485479d2010-09-23 18:02:19 +02008491 data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, -1));
Willy Tarreau4a568972010-05-12 08:08:50 +02008492 return data->ip.s_addr != 0;
8493}
8494
David Cournapeau16023ee2010-12-23 20:55:41 +09008495/*
8496 * Given a path string and its length, find the position of beginning of the
8497 * query string. Returns NULL if no query string is found in the path.
8498 *
8499 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8500 *
8501 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8502 */
8503static inline char *find_query_string(char *path, size_t path_l)
8504{
8505 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008506
David Cournapeau16023ee2010-12-23 20:55:41 +09008507 p = memchr(path, '?', path_l);
8508 return p ? p + 1 : NULL;
8509}
8510
8511static inline int is_param_delimiter(char c)
8512{
8513 return c == '&' || c == ';';
8514}
8515
8516/*
8517 * Given a url parameter, find the starting position of the first occurence,
8518 * or NULL if the parameter is not found.
8519 *
8520 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8521 * the function will return query_string+8.
8522 */
8523static char*
8524find_url_param_pos(char* query_string, size_t query_string_l,
8525 char* url_param_name, size_t url_param_name_l)
8526{
8527 char *pos, *last;
8528
8529 pos = query_string;
8530 last = query_string + query_string_l - url_param_name_l - 1;
8531
8532 while (pos <= last) {
8533 if (pos[url_param_name_l] == '=') {
8534 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8535 return pos;
8536 pos += url_param_name_l + 1;
8537 }
8538 while (pos <= last && !is_param_delimiter(*pos))
8539 pos++;
8540 pos++;
8541 }
8542 return NULL;
8543}
8544
8545/*
8546 * Given a url parameter name, returns its value and size into *value and
8547 * *value_l respectively, and returns non-zero. If the parameter is not found,
8548 * zero is returned and value/value_l are not touched.
8549 */
8550static int
8551find_url_param_value(char* path, size_t path_l,
8552 char* url_param_name, size_t url_param_name_l,
8553 char** value, size_t* value_l)
8554{
8555 char *query_string, *qs_end;
8556 char *arg_start;
8557 char *value_start, *value_end;
8558
8559 query_string = find_query_string(path, path_l);
8560 if (!query_string)
8561 return 0;
8562
8563 qs_end = path + path_l;
8564 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8565 url_param_name, url_param_name_l);
8566 if (!arg_start)
8567 return 0;
8568
8569 value_start = arg_start + url_param_name_l + 1;
8570 value_end = value_start;
8571
8572 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8573 value_end++;
8574
8575 *value = value_start;
8576 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008577 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008578}
8579
8580static int
8581pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8582 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8583{
8584 struct http_txn *txn = l7;
8585 struct http_msg *msg = &txn->req;
8586 char *url_param_value;
8587 size_t url_param_value_l;
8588
8589 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8590 arg_p->data.str.str, arg_p->data.str.len,
8591 &url_param_value, &url_param_value_l))
8592 return 0;
8593
8594 data->str.str = url_param_value;
8595 data->str.len = url_param_value_l;
8596 return 1;
8597}
8598
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008599/* Try to find the last occurrence of a cookie name in a cookie header value.
8600 * The pointer and size of the last occurrence of the cookie value is returned
8601 * into *value and *value_l, and the function returns non-zero if the value was
8602 * found. Otherwise if the cookie was not found, zero is returned and neither
8603 * value nor value_l are touched. The input hdr string should begin at the
8604 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8605 * value may represent a list of values (cookie headers).
8606 */
8607static int
8608extract_cookie_value(char *hdr, size_t hdr_l,
8609 char *cookie_name, size_t cookie_name_l, int list,
8610 char **value, size_t *value_l)
8611{
8612 int found = 0;
8613 char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end;
8614 char *next;
8615
8616 /* Note that multiple cookies may be delimited with semi-colons, so we
8617 * also have to loop on this.
8618 */
8619
8620 /* we search at least a cookie name followed by an equal, and more
8621 * generally something like this :
8622 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8623 */
8624 if (hdr_l < cookie_name_l + 1)
8625 return 0;
8626
8627 hdr_end = hdr + hdr_l;
8628
8629 for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) {
8630 /* Iterate through all cookies on this line */
8631
8632 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8633 att_beg++;
8634
8635 /* find att_end : this is the first character after the last non
8636 * space before the equal. It may be equal to hdr_end.
8637 */
8638 equal = att_end = att_beg;
8639
8640 while (equal < hdr_end) {
8641 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8642 break;
8643 if (http_is_spht[(unsigned char)*equal++])
8644 continue;
8645 att_end = equal;
8646 }
8647
8648 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8649 * is between <att_beg> and <equal>, both may be identical.
8650 */
8651
8652 /* look for end of cookie if there is an equal sign */
8653 if (equal < hdr_end && *equal == '=') {
8654 /* look for the beginning of the value */
8655 val_beg = equal + 1;
8656 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8657 val_beg++;
8658
8659 /* find the end of the value, respecting quotes */
8660 next = find_cookie_value_end(val_beg, hdr_end);
8661
8662 /* make val_end point to the first white space or delimitor after the value */
8663 val_end = next;
8664 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8665 val_end--;
8666 } else {
8667 val_beg = val_end = next = equal;
8668 }
8669
8670 /* We have nothing to do with attributes beginning with '$'. However,
8671 * they will automatically be removed if a header before them is removed,
8672 * since they're supposed to be linked together.
8673 */
8674 if (*att_beg == '$')
8675 continue;
8676
8677 /* Ignore cookies with no equal sign */
8678 if (equal == next)
8679 continue;
8680
8681 /* Now we have the cookie name between att_beg and att_end, and
8682 * its value between val_beg and val_end.
8683 */
8684
8685 if (att_end - att_beg == cookie_name_l &&
8686 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8687 found = 1;
8688 *value = val_beg;
8689 *value_l = val_end - val_beg;
8690 /* right now we want to catch the last occurrence
8691 * of the cookie, so let's go on searching.
8692 */
8693 }
8694
8695 /* Set-Cookie headers only have the name in the first attr=value part */
8696 if (!list)
8697 break;
8698 }
8699
8700 return found;
8701}
8702
8703/* Try to find in request or response message is in <msg> and whose transaction
8704 * is in <txn> the last occurrence of a cookie name in all cookie header values
8705 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8706 * pointer and size of the last occurrence of the cookie value is returned into
8707 * <value> and <value_l>, and the function returns non-zero if the value was
8708 * found. Otherwise if the cookie was not found, zero is returned and neither
8709 * value nor value_l are touched. The input hdr string should begin at the
8710 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8711 * value may represent a list of values (cookie headers).
8712 */
8713
8714static int
8715find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8716 const char *hdr_name, int hdr_name_len,
8717 char *cookie_name, size_t cookie_name_l, int list,
8718 char **value, size_t *value_l)
8719{
8720 struct hdr_ctx ctx;
8721 int found = 0;
8722
8723 ctx.idx = 0;
8724 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
8725 if (ctx.vlen < cookie_name_l + 1)
8726 continue;
8727
8728 found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen,
8729 cookie_name, cookie_name_l, 1,
8730 value, value_l);
8731 }
8732 return found;
8733}
8734
8735static int
8736pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8737 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8738{
8739 struct http_txn *txn = l7;
8740 struct http_msg *msg = &txn->req;
8741 char *cookie_value;
8742 size_t cookie_value_l;
8743 int found = 0;
8744
8745 found = find_cookie_value(msg, txn, "Cookie", 6,
8746 arg_p->data.str.str, arg_p->data.str.len, 1,
8747 &cookie_value, &cookie_value_l);
8748 if (found) {
8749 data->str.str = cookie_value;
8750 data->str.len = cookie_value_l;
8751 }
8752
8753 return found;
8754}
8755
8756
8757static int
8758pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8759 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8760{
8761 struct http_txn *txn = l7;
8762 struct http_msg *msg = &txn->rsp;
8763 char *cookie_value;
8764 size_t cookie_value_l;
8765 int found = 0;
8766
8767 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8768 arg_p->data.str.str, arg_p->data.str.len, 1,
8769 &cookie_value, &cookie_value_l);
8770 if (found) {
8771 data->str.str = cookie_value;
8772 data->str.len = cookie_value_l;
8773 }
8774
8775 return found;
8776}
8777
Emeric Brun485479d2010-09-23 18:02:19 +02008778
Willy Tarreau4a568972010-05-12 08:08:50 +02008779/************************************************************************/
8780/* All supported keywords must be declared here. */
8781/************************************************************************/
8782/* Note: must not be declared <const> as its list will be overwritten */
8783static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Emeric Brun485479d2010-09-23 18:02:19 +02008784 { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008785 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008786 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8787 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008788 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008789}};
8790
Willy Tarreau8797c062007-05-07 00:55:35 +02008791
8792__attribute__((constructor))
8793static void __http_protocol_init(void)
8794{
8795 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008796 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008797}
8798
8799
Willy Tarreau58f10d72006-12-04 02:26:12 +01008800/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008801 * Local variables:
8802 * c-indent-level: 8
8803 * c-basic-offset: 8
8804 * End:
8805 */