blob: c6cfb358c0a3a8ce9e6d4caa4b60a51c5d4bb267 [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
Mark Lamourinec2247f02012-01-04 13:02:01 -05003935int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) {
3936
3937 struct hdr_ctx ctx;
3938
3939 ctx.idx = 0;
3940
3941 char *hdr_name = be->server_id_hdr_name;
3942 int hdr_name_len = be->server_id_hdr_len;
3943
3944 char *hdr_val;
3945
3946 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
3947 /* remove any existing values from the header */
3948 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
3949 }
3950
3951 /* Add the new header requested with the server value */
3952 hdr_val = trash;
3953 memcpy(hdr_val, hdr_name, hdr_name_len);
3954 hdr_val += hdr_name_len;
3955 *hdr_val++ = ':';
3956 *hdr_val++ = ' ';
3957 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
3958 http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash);
3959
3960 return 0;
3961}
3962
Willy Tarreau610ecce2010-01-04 21:15:02 +01003963/* Terminate current transaction and prepare a new one. This is very tricky
3964 * right now but it works.
3965 */
3966void http_end_txn_clean_session(struct session *s)
3967{
3968 /* FIXME: We need a more portable way of releasing a backend's and a
3969 * server's connections. We need a safer way to reinitialize buffer
3970 * flags. We also need a more accurate method for computing per-request
3971 * data.
3972 */
3973 http_silent_debug(__LINE__, s);
3974
3975 s->req->cons->flags |= SI_FL_NOLINGER;
3976 s->req->cons->shutr(s->req->cons);
3977 s->req->cons->shutw(s->req->cons);
3978
3979 http_silent_debug(__LINE__, s);
3980
3981 if (s->flags & SN_BE_ASSIGNED)
3982 s->be->beconn--;
3983
3984 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3985 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003986 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003987
3988 if (s->txn.status) {
3989 int n;
3990
3991 n = s->txn.status / 100;
3992 if (n < 1 || n > 5)
3993 n = 0;
3994
3995 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003996 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997
Willy Tarreau24657792010-02-26 10:30:28 +01003998 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003999 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004000 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004001 }
4002
4003 /* don't count other requests' data */
4004 s->logs.bytes_in -= s->req->l - s->req->send_max;
4005 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
4006
4007 /* let's do a final log if we need it */
4008 if (s->logs.logwait &&
4009 !(s->flags & SN_MONITOR) &&
4010 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4011 s->do_log(s);
4012 }
4013
4014 s->logs.accept_date = date; /* user-visible date for logging */
4015 s->logs.tv_accept = now; /* corrected date for internal use */
4016 tv_zero(&s->logs.tv_request);
4017 s->logs.t_queue = -1;
4018 s->logs.t_connect = -1;
4019 s->logs.t_data = -1;
4020 s->logs.t_close = 0;
4021 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4022 s->logs.srv_queue_size = 0; /* we will get this number soon */
4023
4024 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
4025 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
4026
4027 if (s->pend_pos)
4028 pendconn_free(s->pend_pos);
4029
Willy Tarreau827aee92011-03-10 16:55:02 +01004030 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 if (s->flags & SN_CURR_SESS) {
4032 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01004033 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004034 }
Willy Tarreau827aee92011-03-10 16:55:02 +01004035 if (may_dequeue_tasks(target_srv(&s->target), s->be))
4036 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004037 }
4038
4039 if (unlikely(s->srv_conn))
4040 sess_change_server(s, NULL);
Willy Tarreau9e000c62011-03-10 14:03:36 +01004041 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004042
4043 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
4044 s->req->cons->fd = -1; /* just to help with debugging */
4045 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004046 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047 s->req->cons->err_loc = NULL;
4048 s->req->cons->exp = TICK_ETERNITY;
4049 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02004050 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
4051 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 +02004052 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 +01004053 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
4054 s->txn.meth = 0;
4055 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004056 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004057 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004058 s->req->cons->flags |= SI_FL_INDEP_STR;
4059
Willy Tarreau96e31212011-05-30 18:10:30 +02004060 if (s->fe->options2 & PR_O2_NODELAY) {
4061 s->req->flags |= BF_NEVER_WAIT;
4062 s->rep->flags |= BF_NEVER_WAIT;
4063 }
4064
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065 /* if the request buffer is not empty, it means we're
4066 * about to process another request, so send pending
4067 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004068 * Just don't do this if the buffer is close to be full,
4069 * because the request will wait for it to flush a little
4070 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004071 */
Willy Tarreau065e8332010-01-08 00:30:20 +01004072 if (s->req->l > s->req->send_max) {
4073 if (s->rep->send_max &&
4074 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01004075 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
4076 s->rep->flags |= BF_EXPECT_MORE;
4077 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004078
4079 /* we're removing the analysers, we MUST re-enable events detection */
4080 buffer_auto_read(s->req);
4081 buffer_auto_close(s->req);
4082 buffer_auto_read(s->rep);
4083 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004084
4085 /* make ->lr point to the first non-forwarded byte */
4086 s->req->lr = s->req->w + s->req->send_max;
4087 if (s->req->lr >= s->req->data + s->req->size)
4088 s->req->lr -= s->req->size;
4089 s->rep->lr = s->rep->w + s->rep->send_max;
4090 if (s->rep->lr >= s->rep->data + s->rep->size)
4091 s->rep->lr -= s->req->size;
4092
Willy Tarreau342b11c2010-11-24 16:22:09 +01004093 s->req->analysers = s->listener->analysers;
4094 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004095 s->rep->analysers = 0;
4096
4097 http_silent_debug(__LINE__, s);
4098}
4099
4100
4101/* This function updates the request state machine according to the response
4102 * state machine and buffer flags. It returns 1 if it changes anything (flag
4103 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4104 * it is only used to find when a request/response couple is complete. Both
4105 * this function and its equivalent should loop until both return zero. It
4106 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4107 */
4108int http_sync_req_state(struct session *s)
4109{
4110 struct buffer *buf = s->req;
4111 struct http_txn *txn = &s->txn;
4112 unsigned int old_flags = buf->flags;
4113 unsigned int old_state = txn->req.msg_state;
4114
4115 http_silent_debug(__LINE__, s);
4116 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4117 return 0;
4118
4119 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004120 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004121 * We can shut the read side unless we want to abort_on_close,
4122 * or we have a POST request. The issue with POST requests is
4123 * that some browsers still send a CRLF after the request, and
4124 * this CRLF must be read so that it does not remain in the kernel
4125 * buffers, otherwise a close could cause an RST on some systems
4126 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004127 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004128 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01004129 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004130
4131 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4132 goto wait_other_side;
4133
4134 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4135 /* The server has not finished to respond, so we
4136 * don't want to move in order not to upset it.
4137 */
4138 goto wait_other_side;
4139 }
4140
4141 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4142 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004143 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004144 txn->req.msg_state = HTTP_MSG_TUNNEL;
4145 goto wait_other_side;
4146 }
4147
4148 /* When we get here, it means that both the request and the
4149 * response have finished receiving. Depending on the connection
4150 * mode, we'll have to wait for the last bytes to leave in either
4151 * direction, and sometimes for a close to be effective.
4152 */
4153
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004154 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4155 /* Server-close mode : queue a connection close to the server */
4156 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01004157 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004158 }
4159 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4160 /* Option forceclose is set, or either side wants to close,
4161 * let's enforce it now that we're not expecting any new
4162 * data to come. The caller knows the session is complete
4163 * once both states are CLOSED.
4164 */
4165 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004166 buffer_shutr_now(buf);
4167 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004168 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004169 }
4170 else {
4171 /* The last possible modes are keep-alive and tunnel. Since tunnel
4172 * mode does not set the body analyser, we can't reach this place
4173 * in tunnel mode, so we're left with keep-alive only.
4174 * This mode is currently not implemented, we switch to tunnel mode.
4175 */
4176 buffer_auto_read(buf);
4177 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004178 }
4179
4180 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4181 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004182 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
4183
Willy Tarreau610ecce2010-01-04 21:15:02 +01004184 if (!(buf->flags & BF_OUT_EMPTY)) {
4185 txn->req.msg_state = HTTP_MSG_CLOSING;
4186 goto http_msg_closing;
4187 }
4188 else {
4189 txn->req.msg_state = HTTP_MSG_CLOSED;
4190 goto http_msg_closed;
4191 }
4192 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004193 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194 }
4195
4196 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4197 http_msg_closing:
4198 /* nothing else to forward, just waiting for the output buffer
4199 * to be empty and for the shutw_now to take effect.
4200 */
4201 if (buf->flags & BF_OUT_EMPTY) {
4202 txn->req.msg_state = HTTP_MSG_CLOSED;
4203 goto http_msg_closed;
4204 }
4205 else if (buf->flags & BF_SHUTW) {
4206 txn->req.msg_state = HTTP_MSG_ERROR;
4207 goto wait_other_side;
4208 }
4209 }
4210
4211 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4212 http_msg_closed:
4213 goto wait_other_side;
4214 }
4215
4216 wait_other_side:
4217 http_silent_debug(__LINE__, s);
4218 return txn->req.msg_state != old_state || buf->flags != old_flags;
4219}
4220
4221
4222/* This function updates the response state machine according to the request
4223 * state machine and buffer flags. It returns 1 if it changes anything (flag
4224 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4225 * it is only used to find when a request/response couple is complete. Both
4226 * this function and its equivalent should loop until both return zero. It
4227 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4228 */
4229int http_sync_res_state(struct session *s)
4230{
4231 struct buffer *buf = s->rep;
4232 struct http_txn *txn = &s->txn;
4233 unsigned int old_flags = buf->flags;
4234 unsigned int old_state = txn->rsp.msg_state;
4235
4236 http_silent_debug(__LINE__, s);
4237 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4238 return 0;
4239
4240 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4241 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004242 * still monitor the server connection for a possible close
4243 * while the request is being uploaded, so we don't disable
4244 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004245 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004246 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004247
4248 if (txn->req.msg_state == HTTP_MSG_ERROR)
4249 goto wait_other_side;
4250
4251 if (txn->req.msg_state < HTTP_MSG_DONE) {
4252 /* The client seems to still be sending data, probably
4253 * because we got an error response during an upload.
4254 * We have the choice of either breaking the connection
4255 * or letting it pass through. Let's do the later.
4256 */
4257 goto wait_other_side;
4258 }
4259
4260 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4261 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01004262 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
4264 goto wait_other_side;
4265 }
4266
4267 /* When we get here, it means that both the request and the
4268 * response have finished receiving. Depending on the connection
4269 * mode, we'll have to wait for the last bytes to leave in either
4270 * direction, and sometimes for a close to be effective.
4271 */
4272
4273 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4274 /* Server-close mode : shut read and wait for the request
4275 * side to close its output buffer. The caller will detect
4276 * when we're in DONE and the other is in CLOSED and will
4277 * catch that for the final cleanup.
4278 */
4279 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4280 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004281 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004282 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4283 /* Option forceclose is set, or either side wants to close,
4284 * let's enforce it now that we're not expecting any new
4285 * data to come. The caller knows the session is complete
4286 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004288 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4289 buffer_shutr_now(buf);
4290 buffer_shutw_now(buf);
4291 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004292 }
4293 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004294 /* The last possible modes are keep-alive and tunnel. Since tunnel
4295 * mode does not set the body analyser, we can't reach this place
4296 * in tunnel mode, so we're left with keep-alive only.
4297 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004298 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004299 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004300 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 }
4302
4303 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4304 /* if we've just closed an output, let's switch */
4305 if (!(buf->flags & BF_OUT_EMPTY)) {
4306 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4307 goto http_msg_closing;
4308 }
4309 else {
4310 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4311 goto http_msg_closed;
4312 }
4313 }
4314 goto wait_other_side;
4315 }
4316
4317 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4318 http_msg_closing:
4319 /* nothing else to forward, just waiting for the output buffer
4320 * to be empty and for the shutw_now to take effect.
4321 */
4322 if (buf->flags & BF_OUT_EMPTY) {
4323 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4324 goto http_msg_closed;
4325 }
4326 else if (buf->flags & BF_SHUTW) {
4327 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004328 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004329 if (target_srv(&s->target))
4330 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 goto wait_other_side;
4332 }
4333 }
4334
4335 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4336 http_msg_closed:
4337 /* drop any pending data */
4338 buffer_ignore(buf, buf->l - buf->send_max);
4339 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004340 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341 goto wait_other_side;
4342 }
4343
4344 wait_other_side:
4345 http_silent_debug(__LINE__, s);
4346 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4347}
4348
4349
4350/* Resync the request and response state machines. Return 1 if either state
4351 * changes.
4352 */
4353int http_resync_states(struct session *s)
4354{
4355 struct http_txn *txn = &s->txn;
4356 int old_req_state = txn->req.msg_state;
4357 int old_res_state = txn->rsp.msg_state;
4358
4359 http_silent_debug(__LINE__, s);
4360 http_sync_req_state(s);
4361 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004362 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004363 if (!http_sync_res_state(s))
4364 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004365 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366 if (!http_sync_req_state(s))
4367 break;
4368 }
4369 http_silent_debug(__LINE__, s);
4370 /* OK, both state machines agree on a compatible state.
4371 * There are a few cases we're interested in :
4372 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4373 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4374 * directions, so let's simply disable both analysers.
4375 * - HTTP_MSG_CLOSED on the response only means we must abort the
4376 * request.
4377 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4378 * with server-close mode means we've completed one request and we
4379 * must re-initialize the server connection.
4380 */
4381
4382 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4383 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4384 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4385 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4386 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004387 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004388 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004389 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004390 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004391 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004393 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4394 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004395 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004396 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004397 s->rep->analysers = 0;
4398 buffer_auto_close(s->rep);
4399 buffer_auto_read(s->rep);
4400 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004401 buffer_abort(s->req);
4402 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004403 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004404 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004405 }
4406 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4407 txn->rsp.msg_state == HTTP_MSG_DONE &&
4408 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4409 /* server-close: terminate this server connection and
4410 * reinitialize a fresh-new transaction.
4411 */
4412 http_end_txn_clean_session(s);
4413 }
4414
4415 http_silent_debug(__LINE__, s);
4416 return txn->req.msg_state != old_req_state ||
4417 txn->rsp.msg_state != old_res_state;
4418}
4419
Willy Tarreaud98cf932009-12-27 22:54:55 +01004420/* This function is an analyser which forwards request body (including chunk
4421 * sizes if any). It is called as soon as we must forward, even if we forward
4422 * zero byte. The only situation where it must not be called is when we're in
4423 * tunnel mode and we want to forward till the close. It's used both to forward
4424 * remaining data and to resync after end of body. It expects the msg_state to
4425 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4426 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004427 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004428 * bytes of pending data + the headers if not already done (between som and sov).
4429 * It eventually adjusts som to match sov after the data in between have been sent.
4430 */
4431int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4432{
4433 struct http_txn *txn = &s->txn;
4434 struct http_msg *msg = &s->txn.req;
4435
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004436 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4437 return 0;
4438
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004439 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4440 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004441 /* Output closed while we were sending data. We must abort and
4442 * wake the other side up.
4443 */
4444 msg->msg_state = HTTP_MSG_ERROR;
4445 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004446 return 1;
4447 }
4448
Willy Tarreau4fe41902010-06-07 22:27:41 +02004449 /* in most states, we should abort in case of early close */
4450 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004451
4452 /* Note that we don't have to send 100-continue back because we don't
4453 * need the data to complete our job, and it's up to the server to
4454 * decide whether to return 100, 417 or anything else in return of
4455 * an "Expect: 100-continue" header.
4456 */
4457
4458 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4459 /* we have msg->col and msg->sov which both point to the first
4460 * byte of message body. msg->som still points to the beginning
4461 * of the message. We must save the body in req->lr because it
4462 * survives buffer re-alignments.
4463 */
4464 req->lr = req->data + msg->sov;
4465 if (txn->flags & TX_REQ_TE_CHNK)
4466 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4467 else {
4468 msg->msg_state = HTTP_MSG_DATA;
4469 }
4470 }
4471
Willy Tarreaud98cf932009-12-27 22:54:55 +01004472 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004473 int bytes;
4474
Willy Tarreau610ecce2010-01-04 21:15:02 +01004475 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004476 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004477 bytes = msg->sov - msg->som;
4478 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004479 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004480 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4481 bytes += req->size;
4482 msg->chunk_len += (unsigned int)bytes;
4483 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004484 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004485
Willy Tarreaucaabe412010-01-03 23:08:28 +01004486 if (msg->msg_state == HTTP_MSG_DATA) {
4487 /* must still forward */
4488 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004489 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004490
4491 /* nothing left to forward */
4492 if (txn->flags & TX_REQ_TE_CHNK)
4493 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004495 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004496 }
4497 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004498 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004499 * set ->sov and ->lr to point to the body and switch to DATA or
4500 * TRAILERS state.
4501 */
4502 int ret = http_parse_chunk_size(req, msg);
4503
4504 if (!ret)
4505 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004506 else if (ret < 0) {
4507 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004508 if (msg->err_pos >= 0)
4509 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004510 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004511 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004512 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004513 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004514 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4515 /* we want the CRLF after the data */
4516 int ret;
4517
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004518 req->lr = req->w + req->send_max;
4519 if (req->lr >= req->data + req->size)
4520 req->lr -= req->size;
4521
Willy Tarreaud98cf932009-12-27 22:54:55 +01004522 ret = http_skip_chunk_crlf(req, msg);
4523
4524 if (ret == 0)
4525 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004526 else if (ret < 0) {
4527 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004528 if (msg->err_pos >= 0)
4529 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004530 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004531 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004532 /* we're in MSG_CHUNK_SIZE now */
4533 }
4534 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4535 int ret = http_forward_trailers(req, msg);
4536
4537 if (ret == 0)
4538 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004539 else if (ret < 0) {
4540 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004541 if (msg->err_pos >= 0)
4542 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004543 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004544 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004545 /* we're in HTTP_MSG_DONE now */
4546 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004548 int old_state = msg->msg_state;
4549
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004551 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004552 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4553 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4554 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004555 if (http_resync_states(s)) {
4556 /* some state changes occurred, maybe the analyser
4557 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004558 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004559 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4560 if (req->flags & BF_SHUTW) {
4561 /* request errors are most likely due to
4562 * the server aborting the transfer.
4563 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004564 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004565 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004566 if (msg->err_pos >= 0)
4567 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004568 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004569 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004570 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004571 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004572
4573 /* If "option abortonclose" is set on the backend, we
4574 * want to monitor the client's connection and forward
4575 * any shutdown notification to the server, which will
4576 * decide whether to close or to go on processing the
4577 * request.
4578 */
4579 if (s->be->options & PR_O_ABRT_CLOSE) {
4580 buffer_auto_read(req);
4581 buffer_auto_close(req);
4582 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004583 else if (s->txn.meth == HTTP_METH_POST) {
4584 /* POST requests may require to read extra CRLF
4585 * sent by broken browsers and which could cause
4586 * an RST to be sent upon close on some systems
4587 * (eg: Linux).
4588 */
4589 buffer_auto_read(req);
4590 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004591
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004593 }
4594 }
4595
Willy Tarreaud98cf932009-12-27 22:54:55 +01004596 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004597 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004598 if (req->flags & BF_SHUTR) {
4599 if (!(s->flags & SN_ERR_MASK))
4600 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004601 if (!(s->flags & SN_FINST_MASK)) {
4602 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4603 s->flags |= SN_FINST_H;
4604 else
4605 s->flags |= SN_FINST_D;
4606 }
4607
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004608 s->fe->fe_counters.cli_aborts++;
4609 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004610 if (target_srv(&s->target))
4611 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004612
4613 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004614 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004615
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004616 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004617 if (req->flags & BF_SHUTW)
4618 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004620 /* When TE: chunked is used, we need to get there again to parse remaining
4621 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4622 */
4623 if (txn->flags & TX_REQ_TE_CHNK)
4624 buffer_dont_close(req);
4625
Willy Tarreau5c620922011-05-11 19:56:11 +02004626 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004627 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4628 * system knows it must not set a PUSH on this first part. Interactive
4629 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02004630 */
Willy Tarreau07293032011-05-30 18:29:28 +02004631 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004632
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004634 return 0;
4635
Willy Tarreaud98cf932009-12-27 22:54:55 +01004636 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004637 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004638 if (s->listener->counters)
4639 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004640 return_bad_req_stats_ok:
4641 txn->req.msg_state = HTTP_MSG_ERROR;
4642 if (txn->status) {
4643 /* Note: we don't send any error if some data were already sent */
4644 stream_int_retnclose(req->prod, NULL);
4645 } else {
4646 txn->status = 400;
4647 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4648 }
4649 req->analysers = 0;
4650 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004651
4652 if (!(s->flags & SN_ERR_MASK))
4653 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004654 if (!(s->flags & SN_FINST_MASK)) {
4655 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4656 s->flags |= SN_FINST_H;
4657 else
4658 s->flags |= SN_FINST_D;
4659 }
4660 return 0;
4661
4662 aborted_xfer:
4663 txn->req.msg_state = HTTP_MSG_ERROR;
4664 if (txn->status) {
4665 /* Note: we don't send any error if some data were already sent */
4666 stream_int_retnclose(req->prod, NULL);
4667 } else {
4668 txn->status = 502;
4669 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4670 }
4671 req->analysers = 0;
4672 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4673
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004674 s->fe->fe_counters.srv_aborts++;
4675 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004676 if (target_srv(&s->target))
4677 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004678
4679 if (!(s->flags & SN_ERR_MASK))
4680 s->flags |= SN_ERR_SRVCL;
4681 if (!(s->flags & SN_FINST_MASK)) {
4682 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4683 s->flags |= SN_FINST_H;
4684 else
4685 s->flags |= SN_FINST_D;
4686 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004687 return 0;
4688}
4689
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004690/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4691 * processing can continue on next analysers, or zero if it either needs more
4692 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4693 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4694 * when it has nothing left to do, and may remove any analyser when it wants to
4695 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004696 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004697int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004698{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004699 struct http_txn *txn = &s->txn;
4700 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004701 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004702 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004703 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004704 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004705
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004706 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 +02004707 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004708 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004709 rep,
4710 rep->rex, rep->wex,
4711 rep->flags,
4712 rep->l,
4713 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004714
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004715 /*
4716 * Now parse the partial (or complete) lines.
4717 * We will check the response syntax, and also join multi-line
4718 * headers. An index of all the lines will be elaborated while
4719 * parsing.
4720 *
4721 * For the parsing, we use a 28 states FSM.
4722 *
4723 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004724 * rep->data + msg->som = beginning of response
4725 * rep->data + msg->eoh = end of processed headers / start of current one
4726 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004727 * rep->lr = first non-visited byte
4728 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004729 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004730 */
4731
Willy Tarreau83e3af02009-12-28 17:39:57 +01004732 /* There's a protected area at the end of the buffer for rewriting
4733 * purposes. We don't want to start to parse the request if the
4734 * protected area is affected, because we may have to move processed
4735 * data later, which is much more complicated.
4736 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004737 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4738 if (unlikely((rep->flags & BF_FULL) ||
4739 rep->r < rep->lr ||
4740 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4741 if (rep->send_max) {
4742 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004743 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4744 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004745 buffer_dont_close(rep);
4746 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4747 return 0;
4748 }
4749 if (rep->l <= rep->size - global.tune.maxrewrite)
4750 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004751 }
4752
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004753 if (likely(rep->lr < rep->r))
4754 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004755 }
4756
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004757 /* 1: we might have to print this header in debug mode */
4758 if (unlikely((global.mode & MODE_DEBUG) &&
4759 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004760 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004761 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004762 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004763
Willy Tarreau663308b2010-06-07 14:06:08 +02004764 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004765 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004766 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004767
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004768 sol += hdr_idx_first_pos(&txn->hdr_idx);
4769 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004770
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004771 while (cur_idx) {
4772 eol = sol + txn->hdr_idx.v[cur_idx].len;
4773 debug_hdr("srvhdr", s, sol, eol);
4774 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4775 cur_idx = txn->hdr_idx.v[cur_idx].next;
4776 }
4777 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004778
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004779 /*
4780 * Now we quickly check if we have found a full valid response.
4781 * If not so, we check the FD and buffer states before leaving.
4782 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004783 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004784 * responses are checked first.
4785 *
4786 * Depending on whether the client is still there or not, we
4787 * may send an error response back or not. Note that normally
4788 * we should only check for HTTP status there, and check I/O
4789 * errors somewhere else.
4790 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004791
Willy Tarreau655dce92009-11-08 13:10:58 +01004792 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004793 /* Invalid response */
4794 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4795 /* we detected a parsing error. We want to archive this response
4796 * in the dedicated proxy area for later troubleshooting.
4797 */
4798 hdr_response_bad:
4799 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004800 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004801
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004802 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004803 if (target_srv(&s->target)) {
4804 target_srv(&s->target)->counters.failed_resp++;
4805 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004806 }
Willy Tarreau64648412010-03-05 10:41:54 +01004807 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004808 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004809 rep->analysers = 0;
4810 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004811 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004812 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004813 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4814
4815 if (!(s->flags & SN_ERR_MASK))
4816 s->flags |= SN_ERR_PRXCOND;
4817 if (!(s->flags & SN_FINST_MASK))
4818 s->flags |= SN_FINST_H;
4819
4820 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004821 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004822
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004823 /* too large response does not fit in buffer. */
4824 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004825 if (msg->err_pos < 0)
4826 msg->err_pos = rep->l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004827 goto hdr_response_bad;
4828 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004829
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004830 /* read error */
4831 else if (rep->flags & BF_READ_ERROR) {
4832 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004833 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004834
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004835 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004836 if (target_srv(&s->target)) {
4837 target_srv(&s->target)->counters.failed_resp++;
4838 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004839 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004840
Willy Tarreau90deb182010-01-07 00:20:41 +01004841 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004842 rep->analysers = 0;
4843 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004844 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004845 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004846 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004847
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004848 if (!(s->flags & SN_ERR_MASK))
4849 s->flags |= SN_ERR_SRVCL;
4850 if (!(s->flags & SN_FINST_MASK))
4851 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004852 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004853 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004854
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004855 /* read timeout : return a 504 to the client. */
4856 else if (rep->flags & BF_READ_TIMEOUT) {
4857 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004858 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004859
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004860 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004861 if (target_srv(&s->target)) {
4862 target_srv(&s->target)->counters.failed_resp++;
4863 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004864 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004865
Willy Tarreau90deb182010-01-07 00:20:41 +01004866 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004867 rep->analysers = 0;
4868 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004869 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004870 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004871 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004872
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004873 if (!(s->flags & SN_ERR_MASK))
4874 s->flags |= SN_ERR_SRVTO;
4875 if (!(s->flags & SN_FINST_MASK))
4876 s->flags |= SN_FINST_H;
4877 return 0;
4878 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004879
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004880 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004881 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004882 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004883 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004884
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004885 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004886 if (target_srv(&s->target)) {
4887 target_srv(&s->target)->counters.failed_resp++;
4888 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004889 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004890
Willy Tarreau90deb182010-01-07 00:20:41 +01004891 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004892 rep->analysers = 0;
4893 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004894 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004895 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004896 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004897
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004898 if (!(s->flags & SN_ERR_MASK))
4899 s->flags |= SN_ERR_SRVCL;
4900 if (!(s->flags & SN_FINST_MASK))
4901 s->flags |= SN_FINST_H;
4902 return 0;
4903 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004904
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004905 /* write error to client (we don't send any message then) */
4906 else if (rep->flags & BF_WRITE_ERROR) {
4907 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004908 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004909
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004910 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004911 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004912 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004913
4914 if (!(s->flags & SN_ERR_MASK))
4915 s->flags |= SN_ERR_CLICL;
4916 if (!(s->flags & SN_FINST_MASK))
4917 s->flags |= SN_FINST_H;
4918
4919 /* process_session() will take care of the error */
4920 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004921 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004922
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004923 buffer_dont_close(rep);
4924 return 0;
4925 }
4926
4927 /* More interesting part now : we know that we have a complete
4928 * response which at least looks like HTTP. We have an indicator
4929 * of each header's length, so we can parse them quickly.
4930 */
4931
4932 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004933 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004934
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004935 /*
4936 * 1: get the status code
4937 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004938 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004939 if (n < 1 || n > 5)
4940 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004941 /* when the client triggers a 4xx from the server, it's most often due
4942 * to a missing object or permission. These events should be tracked
4943 * because if they happen often, it may indicate a brute force or a
4944 * vulnerability scan.
4945 */
4946 if (n == 4)
4947 session_inc_http_err_ctr(s);
4948
Willy Tarreau827aee92011-03-10 16:55:02 +01004949 if (target_srv(&s->target))
4950 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004951
Willy Tarreau5b154472009-12-21 20:11:07 +01004952 /* check if the response is HTTP/1.1 or above */
4953 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004954 ((msg->sol[5] > '1') ||
4955 ((msg->sol[5] == '1') &&
4956 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004957 txn->flags |= TX_RES_VER_11;
4958
4959 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004960 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 +01004961
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004962 /* transfer length unknown*/
4963 txn->flags &= ~TX_RES_XFER_LEN;
4964
Willy Tarreau962c3f42010-01-10 00:15:35 +01004965 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004966
Willy Tarreau39650402010-03-15 19:44:39 +01004967 /* Adjust server's health based on status code. Note: status codes 501
4968 * and 505 are triggered on demand by client request, so we must not
4969 * count them as server failures.
4970 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004971 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004972 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004973 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004974 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004975 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004976 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004977
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004978 /*
4979 * 2: check for cacheability.
4980 */
4981
4982 switch (txn->status) {
4983 case 200:
4984 case 203:
4985 case 206:
4986 case 300:
4987 case 301:
4988 case 410:
4989 /* RFC2616 @13.4:
4990 * "A response received with a status code of
4991 * 200, 203, 206, 300, 301 or 410 MAY be stored
4992 * by a cache (...) unless a cache-control
4993 * directive prohibits caching."
4994 *
4995 * RFC2616 @9.5: POST method :
4996 * "Responses to this method are not cacheable,
4997 * unless the response includes appropriate
4998 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004999 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005000 if (likely(txn->meth != HTTP_METH_POST) &&
5001 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
5002 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5003 break;
5004 default:
5005 break;
5006 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005007
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005008 /*
5009 * 3: we may need to capture headers
5010 */
5011 s->logs.logwait &= ~LW_RESP;
5012 if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01005013 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005014 txn->rsp.cap, s->fe->rsp_cap);
5015
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005016 /* 4: determine the transfer-length.
5017 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5018 * the presence of a message-body in a RESPONSE and its transfer length
5019 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005020 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005021 * All responses to the HEAD request method MUST NOT include a
5022 * message-body, even though the presence of entity-header fields
5023 * might lead one to believe they do. All 1xx (informational), 204
5024 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5025 * message-body. All other responses do include a message-body,
5026 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005027 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005028 * 1. Any response which "MUST NOT" include a message-body (such as the
5029 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5030 * always terminated by the first empty line after the header fields,
5031 * regardless of the entity-header fields present in the message.
5032 *
5033 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5034 * the "chunked" transfer-coding (Section 6.2) is used, the
5035 * transfer-length is defined by the use of this transfer-coding.
5036 * If a Transfer-Encoding header field is present and the "chunked"
5037 * transfer-coding is not present, the transfer-length is defined by
5038 * the sender closing the connection.
5039 *
5040 * 3. If a Content-Length header field is present, its decimal value in
5041 * OCTETs represents both the entity-length and the transfer-length.
5042 * If a message is received with both a Transfer-Encoding header
5043 * field and a Content-Length header field, the latter MUST be ignored.
5044 *
5045 * 4. If the message uses the media type "multipart/byteranges", and
5046 * the transfer-length is not otherwise specified, then this self-
5047 * delimiting media type defines the transfer-length. This media
5048 * type MUST NOT be used unless the sender knows that the recipient
5049 * can parse it; the presence in a request of a Range header with
5050 * multiple byte-range specifiers from a 1.1 client implies that the
5051 * client can parse multipart/byteranges responses.
5052 *
5053 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005054 */
5055
5056 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005057 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005058 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005059 * FIXME: should we parse anyway and return an error on chunked encoding ?
5060 */
5061 if (txn->meth == HTTP_METH_HEAD ||
5062 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005063 txn->status == 204 || txn->status == 304) {
5064 txn->flags |= TX_RES_XFER_LEN;
5065 goto skip_content_length;
5066 }
5067
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005068 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005069 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01005070 while ((txn->flags & TX_RES_VER_11) &&
5071 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005072 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
5073 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5074 else if (txn->flags & TX_RES_TE_CHNK) {
5075 /* bad transfer-encoding (chunked followed by something else) */
5076 use_close_only = 1;
5077 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
5078 break;
5079 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005080 }
5081
5082 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5083 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005084 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005085 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
5086 signed long long cl;
5087
Willy Tarreauad14f752011-09-02 20:33:27 +02005088 if (!ctx.vlen) {
5089 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005090 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005091 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005092
Willy Tarreauad14f752011-09-02 20:33:27 +02005093 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
5094 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005095 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005096 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005097
Willy Tarreauad14f752011-09-02 20:33:27 +02005098 if (cl < 0) {
5099 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005100 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005101 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005102
Willy Tarreauad14f752011-09-02 20:33:27 +02005103 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) {
5104 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005105 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005106 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005107
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005108 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005109 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005110 }
5111
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005112 /* FIXME: we should also implement the multipart/byterange method.
5113 * For now on, we resort to close mode in this case (unknown length).
5114 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005115skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005116
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005117 /* end of job, return OK */
5118 rep->analysers &= ~an_bit;
5119 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01005120 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005121 return 1;
5122}
5123
5124/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005125 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5126 * and updates t->rep->analysers. It might make sense to explode it into several
5127 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005128 */
5129int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
5130{
5131 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132 struct http_msg *msg = &txn->rsp;
5133 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005134 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005135
5136 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
5137 now_ms, __FUNCTION__,
5138 t,
5139 rep,
5140 rep->rex, rep->wex,
5141 rep->flags,
5142 rep->l,
5143 rep->analysers);
5144
Willy Tarreau655dce92009-11-08 13:10:58 +01005145 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005146 return 0;
5147
5148 rep->analysers &= ~an_bit;
5149 rep->analyse_exp = TICK_ETERNITY;
5150
Willy Tarreau5b154472009-12-21 20:11:07 +01005151 /* Now we have to check if we need to modify the Connection header.
5152 * This is more difficult on the response than it is on the request,
5153 * because we can have two different HTTP versions and we don't know
5154 * how the client will interprete a response. For instance, let's say
5155 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5156 * HTTP/1.1 response without any header. Maybe it will bound itself to
5157 * HTTP/1.0 because it only knows about it, and will consider the lack
5158 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5159 * the lack of header as a keep-alive. Thus we will use two flags
5160 * indicating how a request MAY be understood by the client. In case
5161 * of multiple possibilities, we'll fix the header to be explicit. If
5162 * ambiguous cases such as both close and keepalive are seen, then we
5163 * will fall back to explicit close. Note that we won't take risks with
5164 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005165 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005166 */
5167
Willy Tarreaudc008c52010-02-01 16:20:08 +01005168 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5169 txn->status == 101)) {
5170 /* Either we've established an explicit tunnel, or we're
5171 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005172 * to understand the next protocols. We have to switch to tunnel
5173 * mode, so that we transfer the request and responses then let
5174 * this protocol pass unmodified. When we later implement specific
5175 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005176 * header which contains information about that protocol for
5177 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005178 */
5179 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5180 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005181 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5182 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5183 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005184 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005185
Willy Tarreau60466522010-01-18 19:08:45 +01005186 /* on unknown transfer length, we must close */
5187 if (!(txn->flags & TX_RES_XFER_LEN) &&
5188 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5189 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005190
Willy Tarreau60466522010-01-18 19:08:45 +01005191 /* now adjust header transformations depending on current state */
5192 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5193 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5194 to_del |= 2; /* remove "keep-alive" on any response */
5195 if (!(txn->flags & TX_RES_VER_11))
5196 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005197 }
Willy Tarreau60466522010-01-18 19:08:45 +01005198 else { /* SCL / KAL */
5199 to_del |= 1; /* remove "close" on any response */
5200 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
5201 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005202 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005203
Willy Tarreau60466522010-01-18 19:08:45 +01005204 /* Parse and remove some headers from the connection header */
5205 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005206
Willy Tarreau60466522010-01-18 19:08:45 +01005207 /* Some keep-alive responses are converted to Server-close if
5208 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005209 */
Willy Tarreau60466522010-01-18 19:08:45 +01005210 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5211 if ((txn->flags & TX_HDR_CONN_CLO) ||
5212 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
5213 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005214 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005215 }
5216
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005217 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005218 /*
5219 * 3: we will have to evaluate the filters.
5220 * As opposed to version 1.2, now they will be evaluated in the
5221 * filters order and not in the header order. This means that
5222 * each filter has to be validated among all headers.
5223 *
5224 * Filters are tried with ->be first, then with ->fe if it is
5225 * different from ->be.
5226 */
5227
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005228 cur_proxy = t->be;
5229 while (1) {
5230 struct proxy *rule_set = cur_proxy;
5231
5232 /* try headers filters */
5233 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005234 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005235 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005236 if (target_srv(&t->target)) {
5237 target_srv(&t->target)->counters.failed_resp++;
5238 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005239 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005240 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005241 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005242 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005243 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005244 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01005245 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02005246 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005247 if (!(t->flags & SN_ERR_MASK))
5248 t->flags |= SN_ERR_PRXCOND;
5249 if (!(t->flags & SN_FINST_MASK))
5250 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005251 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005252 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005253 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005254
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005255 /* has the response been denied ? */
5256 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005257 if (target_srv(&t->target))
5258 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005259
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005260 t->be->be_counters.denied_resp++;
5261 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005262 if (t->listener->counters)
5263 t->listener->counters->denied_resp++;
5264
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005265 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005266 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005267
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005268 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005269 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005270 if (txn->status < 200)
5271 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005272 if (wl->cond) {
5273 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5274 ret = acl_pass(ret);
5275 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5276 ret = !ret;
5277 if (!ret)
5278 continue;
5279 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005280 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005281 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005282 }
5283
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005284 /* check whether we're already working on the frontend */
5285 if (cur_proxy == t->fe)
5286 break;
5287 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005288 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005289
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005290 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005291 * We may be facing a 100-continue response, in which case this
5292 * is not the right response, and we're waiting for the next one.
5293 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005294 * next one.
5295 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005296 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005297 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005298 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005299 msg->msg_state = HTTP_MSG_RPBEFORE;
5300 txn->status = 0;
5301 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5302 return 1;
5303 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005304 else if (unlikely(txn->status < 200))
5305 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005306
5307 /* we don't have any 1xx status code now */
5308
5309 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005310 * 4: check for server cookie.
5311 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005312 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5313 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005314 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005315
Willy Tarreaubaaee002006-06-26 02:48:02 +02005316
Willy Tarreaua15645d2007-03-18 16:22:39 +01005317 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005318 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005319 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005320 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005321 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005322
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005323 /*
5324 * 6: add server cookie in the response if needed
5325 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005326 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005327 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005328 (!(t->flags & SN_DIRECT) ||
5329 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5330 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5331 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5332 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005333 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5334 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005335 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005336 /* the server is known, it's not the one the client requested, or the
5337 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005338 * insert a set-cookie here, except if we want to insert only on POST
5339 * requests and this one isn't. Note that servers which don't have cookies
5340 * (eg: some backup servers) will return a full cookie removal request.
5341 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005342 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005343 len = sprintf(trash,
5344 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5345 t->be->cookie_name);
5346 }
5347 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005348 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005349
5350 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5351 /* emit last_date, which is mandatory */
5352 trash[len++] = COOKIE_DELIM_DATE;
5353 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5354 if (t->be->cookie_maxlife) {
5355 /* emit first_date, which is either the original one or
5356 * the current date.
5357 */
5358 trash[len++] = COOKIE_DELIM_DATE;
5359 s30tob64(txn->cookie_first_date ?
5360 txn->cookie_first_date >> 2 :
5361 (date.tv_sec+3) >> 2, trash + len);
5362 len += 5;
5363 }
5364 }
5365 len += sprintf(trash + len, "; path=/");
5366 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005367
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005368 if (t->be->cookie_domain)
5369 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005370
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005371 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005372 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005373 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005374
Willy Tarreauf1348312010-10-07 15:54:11 +02005375 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005376 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005377 /* the server did not change, only the date was updated */
5378 txn->flags |= TX_SCK_UPDATED;
5379 else
5380 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005381
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005382 /* Here, we will tell an eventual cache on the client side that we don't
5383 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5384 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5385 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5386 */
5387 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005388
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005389 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5390
5391 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005392 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005393 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005394 }
5395 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005396
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005397 /*
5398 * 7: check if result will be cacheable with a cookie.
5399 * We'll block the response if security checks have caught
5400 * nasty things such as a cacheable cookie.
5401 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005402 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5403 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005404 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005405
5406 /* we're in presence of a cacheable response containing
5407 * a set-cookie header. We'll block it as requested by
5408 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005409 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005410 if (target_srv(&t->target))
5411 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005412
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005413 t->be->be_counters.denied_resp++;
5414 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005415 if (t->listener->counters)
5416 t->listener->counters->denied_resp++;
5417
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005418 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005419 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005420 send_log(t->be, LOG_ALERT,
5421 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005422 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005423 goto return_srv_prx_502;
5424 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005425
5426 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005427 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005428 */
Willy Tarreau60466522010-01-18 19:08:45 +01005429 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5430 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5431 unsigned int want_flags = 0;
5432
5433 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5434 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5435 /* we want a keep-alive response here. Keep-alive header
5436 * required if either side is not 1.1.
5437 */
5438 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5439 want_flags |= TX_CON_KAL_SET;
5440 }
5441 else {
5442 /* we want a close response here. Close header required if
5443 * the server is 1.1, regardless of the client.
5444 */
5445 if (txn->flags & TX_RES_VER_11)
5446 want_flags |= TX_CON_CLO_SET;
5447 }
5448
5449 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5450 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005451 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005452
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005453 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005454 if ((txn->flags & TX_RES_XFER_LEN) ||
5455 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005456 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005457
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005458 /*************************************************************
5459 * OK, that's finished for the headers. We have done what we *
5460 * could. Let's switch to the DATA state. *
5461 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005462
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005463 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005464
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005465 /* if the user wants to log as soon as possible, without counting
5466 * bytes from the server, then this is the right moment. We have
5467 * to temporarily assign bytes_out to log what we currently have.
5468 */
5469 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5470 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5471 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005472 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005473 t->logs.bytes_out = 0;
5474 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005475
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005476 /* Note: we must not try to cheat by jumping directly to DATA,
5477 * otherwise we would not let the client side wake up.
5478 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005479
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005480 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005481 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005482 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005483}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005484
Willy Tarreaud98cf932009-12-27 22:54:55 +01005485/* This function is an analyser which forwards response body (including chunk
5486 * sizes if any). It is called as soon as we must forward, even if we forward
5487 * zero byte. The only situation where it must not be called is when we're in
5488 * tunnel mode and we want to forward till the close. It's used both to forward
5489 * remaining data and to resync after end of body. It expects the msg_state to
5490 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5491 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005492 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005493 * bytes of pending data + the headers if not already done (between som and sov).
5494 * It eventually adjusts som to match sov after the data in between have been sent.
5495 */
5496int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5497{
5498 struct http_txn *txn = &s->txn;
5499 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005500 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005501
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005502 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5503 return 0;
5504
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005505 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005506 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005507 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005508 /* Output closed while we were sending data. We must abort and
5509 * wake the other side up.
5510 */
5511 msg->msg_state = HTTP_MSG_ERROR;
5512 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005513 return 1;
5514 }
5515
Willy Tarreau4fe41902010-06-07 22:27:41 +02005516 /* in most states, we should abort in case of early close */
5517 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005518
Willy Tarreaud98cf932009-12-27 22:54:55 +01005519 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5520 /* we have msg->col and msg->sov which both point to the first
5521 * byte of message body. msg->som still points to the beginning
5522 * of the message. We must save the body in req->lr because it
5523 * survives buffer re-alignments.
5524 */
5525 res->lr = res->data + msg->sov;
5526 if (txn->flags & TX_RES_TE_CHNK)
5527 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5528 else {
5529 msg->msg_state = HTTP_MSG_DATA;
5530 }
5531 }
5532
Willy Tarreaud98cf932009-12-27 22:54:55 +01005533 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005534 int bytes;
5535
Willy Tarreau610ecce2010-01-04 21:15:02 +01005536 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005537 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005538 bytes = msg->sov - msg->som;
5539 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005540 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005541 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5542 bytes += res->size;
5543 msg->chunk_len += (unsigned int)bytes;
5544 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005545 }
5546
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005547
Willy Tarreaucaabe412010-01-03 23:08:28 +01005548 if (msg->msg_state == HTTP_MSG_DATA) {
5549 /* must still forward */
5550 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005551 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005552
5553 /* nothing left to forward */
5554 if (txn->flags & TX_RES_TE_CHNK)
5555 msg->msg_state = HTTP_MSG_DATA_CRLF;
5556 else
5557 msg->msg_state = HTTP_MSG_DONE;
5558 }
5559 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005560 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005561 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5562 */
5563 int ret = http_parse_chunk_size(res, msg);
5564
5565 if (!ret)
5566 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005567 else if (ret < 0) {
5568 if (msg->err_pos >= 0)
5569 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005570 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005571 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005572 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005573 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5575 /* we want the CRLF after the data */
5576 int ret;
5577
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005578 res->lr = res->w + res->send_max;
5579 if (res->lr >= res->data + res->size)
5580 res->lr -= res->size;
5581
Willy Tarreaud98cf932009-12-27 22:54:55 +01005582 ret = http_skip_chunk_crlf(res, msg);
5583
5584 if (!ret)
5585 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005586 else if (ret < 0) {
5587 if (msg->err_pos >= 0)
5588 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005589 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005590 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005591 /* we're in MSG_CHUNK_SIZE now */
5592 }
5593 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5594 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005595
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596 if (ret == 0)
5597 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005598 else if (ret < 0) {
5599 if (msg->err_pos >= 0)
5600 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005601 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005602 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005603 /* we're in HTTP_MSG_DONE now */
5604 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005605 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005606 int old_state = msg->msg_state;
5607
Willy Tarreau610ecce2010-01-04 21:15:02 +01005608 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005609 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005610 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5611 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5612 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005613 if (http_resync_states(s)) {
5614 http_silent_debug(__LINE__, s);
5615 /* some state changes occurred, maybe the analyser
5616 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005617 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005618 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5619 if (res->flags & BF_SHUTW) {
5620 /* response errors are most likely due to
5621 * the client aborting the transfer.
5622 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005623 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005624 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005625 if (msg->err_pos >= 0)
5626 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005627 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005628 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005629 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005630 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005631 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005632 }
5633 }
5634
Willy Tarreaud98cf932009-12-27 22:54:55 +01005635 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005636 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005637 if (res->flags & BF_SHUTR) {
5638 if (!(s->flags & SN_ERR_MASK))
5639 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005640 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005641 if (target_srv(&s->target))
5642 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005643 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005644 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005645
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005646 if (res->flags & BF_SHUTW)
5647 goto aborted_xfer;
5648
Willy Tarreau40dba092010-03-04 18:14:51 +01005649 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005650 if (!s->req->analysers)
5651 goto return_bad_res;
5652
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005653 /* forward any pending data */
5654 bytes = msg->sov - msg->som;
5655 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005656 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005657 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5658 bytes += res->size;
5659 msg->chunk_len += (unsigned int)bytes;
5660 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005661 }
5662
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005663 /* When TE: chunked is used, we need to get there again to parse remaining
5664 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5665 * Similarly, with keep-alive on the client side, we don't want to forward a
5666 * close.
5667 */
5668 if ((txn->flags & TX_RES_TE_CHNK) ||
5669 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5670 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5671 buffer_dont_close(res);
5672
Willy Tarreau5c620922011-05-11 19:56:11 +02005673 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005674 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5675 * system knows it must not set a PUSH on this first part. Interactive
5676 * modes are already handled by the stream sock layer.
Willy Tarreau5c620922011-05-11 19:56:11 +02005677 */
Willy Tarreau07293032011-05-30 18:29:28 +02005678 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005679
Willy Tarreaud98cf932009-12-27 22:54:55 +01005680 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005681 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005682 return 0;
5683
Willy Tarreau40dba092010-03-04 18:14:51 +01005684 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005685 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005686 if (target_srv(&s->target))
5687 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005688
5689 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005690 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005691 /* don't send any error message as we're in the body */
5692 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005693 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005694 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005695 if (target_srv(&s->target))
5696 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005697
5698 if (!(s->flags & SN_ERR_MASK))
5699 s->flags |= SN_ERR_PRXCOND;
5700 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005701 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005702 return 0;
5703
5704 aborted_xfer:
5705 txn->rsp.msg_state = HTTP_MSG_ERROR;
5706 /* don't send any error message as we're in the body */
5707 stream_int_retnclose(res->cons, NULL);
5708 res->analysers = 0;
5709 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5710
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005711 s->fe->fe_counters.cli_aborts++;
5712 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005713 if (target_srv(&s->target))
5714 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005715
5716 if (!(s->flags & SN_ERR_MASK))
5717 s->flags |= SN_ERR_CLICL;
5718 if (!(s->flags & SN_FINST_MASK))
5719 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005720 return 0;
5721}
5722
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723/* Iterate the same filter through all request headers.
5724 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005725 * Since it can manage the switch to another backend, it updates the per-proxy
5726 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005727 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005728int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005729{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005730 char term;
5731 char *cur_ptr, *cur_end, *cur_next;
5732 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005733 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005734 struct hdr_idx_elem *cur_hdr;
5735 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005736
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005737 last_hdr = 0;
5738
Willy Tarreau962c3f42010-01-10 00:15:35 +01005739 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005740 old_idx = 0;
5741
5742 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005743 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005744 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005745 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005746 (exp->action == ACT_ALLOW ||
5747 exp->action == ACT_DENY ||
5748 exp->action == ACT_TARPIT))
5749 return 0;
5750
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005751 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005752 if (!cur_idx)
5753 break;
5754
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005755 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005756 cur_ptr = cur_next;
5757 cur_end = cur_ptr + cur_hdr->len;
5758 cur_next = cur_end + cur_hdr->cr + 1;
5759
5760 /* Now we have one header between cur_ptr and cur_end,
5761 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005762 */
5763
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005764 /* The annoying part is that pattern matching needs
5765 * that we modify the contents to null-terminate all
5766 * strings before testing them.
5767 */
5768
5769 term = *cur_end;
5770 *cur_end = '\0';
5771
5772 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5773 switch (exp->action) {
5774 case ACT_SETBE:
5775 /* It is not possible to jump a second time.
5776 * FIXME: should we return an HTTP/500 here so that
5777 * the admin knows there's a problem ?
5778 */
5779 if (t->be != t->fe)
5780 break;
5781
5782 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005783 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005784 last_hdr = 1;
5785 break;
5786
5787 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005788 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005789 last_hdr = 1;
5790 break;
5791
5792 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005793 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005794 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005795
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005796 t->fe->fe_counters.denied_req++;
5797 if (t->fe != t->be)
5798 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005799 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005800 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005801
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005802 break;
5803
5804 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005805 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005806 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005807
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005808 t->fe->fe_counters.denied_req++;
5809 if (t->fe != t->be)
5810 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005811 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005812 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005813
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005814 break;
5815
5816 case ACT_REPLACE:
5817 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5818 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5819 /* FIXME: if the user adds a newline in the replacement, the
5820 * index will not be recalculated for now, and the new line
5821 * will not be counted as a new header.
5822 */
5823
5824 cur_end += delta;
5825 cur_next += delta;
5826 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005827 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005828 break;
5829
5830 case ACT_REMOVE:
5831 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5832 cur_next += delta;
5833
Willy Tarreaufa355d42009-11-29 18:12:29 +01005834 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005835 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5836 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005837 cur_hdr->len = 0;
5838 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005839 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005840 break;
5841
5842 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005843 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005844 if (cur_end)
5845 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005846
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005847 /* keep the link from this header to next one in case of later
5848 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005849 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005850 old_idx = cur_idx;
5851 }
5852 return 0;
5853}
5854
5855
5856/* Apply the filter to the request line.
5857 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5858 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005859 * Since it can manage the switch to another backend, it updates the per-proxy
5860 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005861 */
5862int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5863{
5864 char term;
5865 char *cur_ptr, *cur_end;
5866 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005867 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005868 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005869
Willy Tarreau58f10d72006-12-04 02:26:12 +01005870
Willy Tarreau3d300592007-03-18 18:34:41 +01005871 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005872 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005873 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005874 (exp->action == ACT_ALLOW ||
5875 exp->action == ACT_DENY ||
5876 exp->action == ACT_TARPIT))
5877 return 0;
5878 else if (exp->action == ACT_REMOVE)
5879 return 0;
5880
5881 done = 0;
5882
Willy Tarreau962c3f42010-01-10 00:15:35 +01005883 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005884 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005885
5886 /* Now we have the request line between cur_ptr and cur_end */
5887
5888 /* The annoying part is that pattern matching needs
5889 * that we modify the contents to null-terminate all
5890 * strings before testing them.
5891 */
5892
5893 term = *cur_end;
5894 *cur_end = '\0';
5895
5896 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5897 switch (exp->action) {
5898 case ACT_SETBE:
5899 /* It is not possible to jump a second time.
5900 * FIXME: should we return an HTTP/500 here so that
5901 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005902 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005903 if (t->be != t->fe)
5904 break;
5905
5906 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005907 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005908 done = 1;
5909 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005910
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005911 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005912 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005913 done = 1;
5914 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005915
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005916 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005917 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005918
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005919 t->fe->fe_counters.denied_req++;
5920 if (t->fe != t->be)
5921 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005922 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005923 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005924
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005925 done = 1;
5926 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005927
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005928 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005929 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005930
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005931 t->fe->fe_counters.denied_req++;
5932 if (t->fe != t->be)
5933 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005934 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005935 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005936
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005937 done = 1;
5938 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005939
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005940 case ACT_REPLACE:
5941 *cur_end = term; /* restore the string terminator */
5942 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5943 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5944 /* FIXME: if the user adds a newline in the replacement, the
5945 * index will not be recalculated for now, and the new line
5946 * will not be counted as a new header.
5947 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005948
Willy Tarreaufa355d42009-11-29 18:12:29 +01005949 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005951 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005952 HTTP_MSG_RQMETH,
5953 cur_ptr, cur_end + 1,
5954 NULL, NULL);
5955 if (unlikely(!cur_end))
5956 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005957
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005958 /* we have a full request and we know that we have either a CR
5959 * or an LF at <ptr>.
5960 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005961 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5962 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005963 /* there is no point trying this regex on headers */
5964 return 1;
5965 }
5966 }
5967 *cur_end = term; /* restore the string terminator */
5968 return done;
5969}
Willy Tarreau97de6242006-12-27 17:18:38 +01005970
Willy Tarreau58f10d72006-12-04 02:26:12 +01005971
Willy Tarreau58f10d72006-12-04 02:26:12 +01005972
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005973/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005974 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005975 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005976 * unparsable request. Since it can manage the switch to another backend, it
5977 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005978 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005979int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005980{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005981 struct http_txn *txn = &s->txn;
5982 struct hdr_exp *exp;
5983
5984 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005985 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005986
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005987 /*
5988 * The interleaving of transformations and verdicts
5989 * makes it difficult to decide to continue or stop
5990 * the evaluation.
5991 */
5992
Willy Tarreau6c123b12010-01-28 20:22:06 +01005993 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5994 break;
5995
Willy Tarreau3d300592007-03-18 18:34:41 +01005996 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005997 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005998 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005999 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006000
6001 /* if this filter had a condition, evaluate it now and skip to
6002 * next filter if the condition does not match.
6003 */
6004 if (exp->cond) {
6005 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
6006 ret = acl_pass(ret);
6007 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6008 ret = !ret;
6009
6010 if (!ret)
6011 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006012 }
6013
6014 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006015 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006016 if (unlikely(ret < 0))
6017 return -1;
6018
6019 if (likely(ret == 0)) {
6020 /* The filter did not match the request, it can be
6021 * iterated through all headers.
6022 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006023 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006024 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006025 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006026 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006027}
6028
6029
Willy Tarreaua15645d2007-03-18 16:22:39 +01006030
Willy Tarreau58f10d72006-12-04 02:26:12 +01006031/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006032 * Try to retrieve the server associated to the appsession.
6033 * If the server is found, it's assigned to the session.
6034 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006035void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006036 struct http_txn *txn = &t->txn;
6037 appsess *asession = NULL;
6038 char *sessid_temp = NULL;
6039
Cyril Bontéb21570a2009-11-29 20:04:48 +01006040 if (len > t->be->appsession_len) {
6041 len = t->be->appsession_len;
6042 }
6043
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006044 if (t->be->options2 & PR_O2_AS_REQL) {
6045 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006046 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006047 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006048 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006049 }
6050
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006051 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006052 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6053 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6054 return;
6055 }
6056
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006057 memcpy(txn->sessid, buf, len);
6058 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006059 }
6060
6061 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6062 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6063 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6064 return;
6065 }
6066
Cyril Bontéb21570a2009-11-29 20:04:48 +01006067 memcpy(sessid_temp, buf, len);
6068 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006069
6070 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6071 /* free previously allocated memory */
6072 pool_free2(apools.sessid, sessid_temp);
6073
6074 if (asession != NULL) {
6075 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6076 if (!(t->be->options2 & PR_O2_AS_REQL))
6077 asession->request_count++;
6078
6079 if (asession->serverid != NULL) {
6080 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006081
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006082 while (srv) {
6083 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006084 if ((srv->state & SRV_RUNNING) ||
6085 (t->be->options & PR_O_PERSIST) ||
6086 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006087 /* we found the server and it's usable */
6088 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006089 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006090 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006091 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006092
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006093 break;
6094 } else {
6095 txn->flags &= ~TX_CK_MASK;
6096 txn->flags |= TX_CK_DOWN;
6097 }
6098 }
6099 srv = srv->next;
6100 }
6101 }
6102 }
6103}
6104
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006105/* Find the end of a cookie value contained between <s> and <e>. It works the
6106 * same way as with headers above except that the semi-colon also ends a token.
6107 * See RFC2965 for more information. Note that it requires a valid header to
6108 * return a valid result.
6109 */
6110char *find_cookie_value_end(char *s, const char *e)
6111{
6112 int quoted, qdpair;
6113
6114 quoted = qdpair = 0;
6115 for (; s < e; s++) {
6116 if (qdpair) qdpair = 0;
6117 else if (quoted) {
6118 if (*s == '\\') qdpair = 1;
6119 else if (*s == '"') quoted = 0;
6120 }
6121 else if (*s == '"') quoted = 1;
6122 else if (*s == ',' || *s == ';') return s;
6123 }
6124 return s;
6125}
6126
6127/* Delete a value in a header between delimiters <from> and <next> in buffer
6128 * <buf>. The number of characters displaced is returned, and the pointer to
6129 * the first delimiter is updated if required. The function tries as much as
6130 * possible to respect the following principles :
6131 * - replace <from> delimiter by the <next> one unless <from> points to a
6132 * colon, in which case <next> is simply removed
6133 * - set exactly one space character after the new first delimiter, unless
6134 * there are not enough characters in the block being moved to do so.
6135 * - remove unneeded spaces before the previous delimiter and after the new
6136 * one.
6137 *
6138 * It is the caller's responsibility to ensure that :
6139 * - <from> points to a valid delimiter or the colon ;
6140 * - <next> points to a valid delimiter or the final CR/LF ;
6141 * - there are non-space chars before <from> ;
6142 * - there is a CR/LF at or after <next>.
6143 */
6144int del_hdr_value(struct buffer *buf, char **from, char *next)
6145{
6146 char *prev = *from;
6147
6148 if (*prev == ':') {
6149 /* We're removing the first value, preserve the colon and add a
6150 * space if possible.
6151 */
6152 if (!http_is_crlf[(unsigned char)*next])
6153 next++;
6154 prev++;
6155 if (prev < next)
6156 *prev++ = ' ';
6157
6158 while (http_is_spht[(unsigned char)*next])
6159 next++;
6160 } else {
6161 /* Remove useless spaces before the old delimiter. */
6162 while (http_is_spht[(unsigned char)*(prev-1)])
6163 prev--;
6164 *from = prev;
6165
6166 /* copy the delimiter and if possible a space if we're
6167 * not at the end of the line.
6168 */
6169 if (!http_is_crlf[(unsigned char)*next]) {
6170 *prev++ = *next++;
6171 if (prev + 1 < next)
6172 *prev++ = ' ';
6173 while (http_is_spht[(unsigned char)*next])
6174 next++;
6175 }
6176 }
6177 return buffer_replace2(buf, prev, next, NULL, 0);
6178}
6179
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006180/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006181 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006182 * desirable to call it only when needed. This code is quite complex because
6183 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6184 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006185 */
6186void manage_client_side_cookies(struct session *t, struct buffer *req)
6187{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006188 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006189 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006190 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006191 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6192 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006193
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006194 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006195 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006196 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006197
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006198 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006199 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006200 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006201
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006202 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006203 hdr_beg = hdr_next;
6204 hdr_end = hdr_beg + cur_hdr->len;
6205 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006206
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006207 /* We have one full header between hdr_beg and hdr_end, and the
6208 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006209 * "Cookie:" headers.
6210 */
6211
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006212 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006213 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214 old_idx = cur_idx;
6215 continue;
6216 }
6217
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006218 del_from = NULL; /* nothing to be deleted */
6219 preserve_hdr = 0; /* assume we may kill the whole header */
6220
Willy Tarreau58f10d72006-12-04 02:26:12 +01006221 /* Now look for cookies. Conforming to RFC2109, we have to support
6222 * attributes whose name begin with a '$', and associate them with
6223 * the right cookie, if we want to delete this cookie.
6224 * So there are 3 cases for each cookie read :
6225 * 1) it's a special attribute, beginning with a '$' : ignore it.
6226 * 2) it's a server id cookie that we *MAY* want to delete : save
6227 * some pointers on it (last semi-colon, beginning of cookie...)
6228 * 3) it's an application cookie : we *MAY* have to delete a previous
6229 * "special" cookie.
6230 * At the end of loop, if a "special" cookie remains, we may have to
6231 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006232 * *MUST* delete it.
6233 *
6234 * Note: RFC2965 is unclear about the processing of spaces around
6235 * the equal sign in the ATTR=VALUE form. A careful inspection of
6236 * the RFC explicitly allows spaces before it, and not within the
6237 * tokens (attrs or values). An inspection of RFC2109 allows that
6238 * too but section 10.1.3 lets one think that spaces may be allowed
6239 * after the equal sign too, resulting in some (rare) buggy
6240 * implementations trying to do that. So let's do what servers do.
6241 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6242 * allowed quoted strings in values, with any possible character
6243 * after a backslash, including control chars and delimitors, which
6244 * causes parsing to become ambiguous. Browsers also allow spaces
6245 * within values even without quotes.
6246 *
6247 * We have to keep multiple pointers in order to support cookie
6248 * removal at the beginning, middle or end of header without
6249 * corrupting the header. All of these headers are valid :
6250 *
6251 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6252 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6253 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6254 * | | | | | | | | |
6255 * | | | | | | | | hdr_end <--+
6256 * | | | | | | | +--> next
6257 * | | | | | | +----> val_end
6258 * | | | | | +-----------> val_beg
6259 * | | | | +--------------> equal
6260 * | | | +----------------> att_end
6261 * | | +---------------------> att_beg
6262 * | +--------------------------> prev
6263 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006264 */
6265
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006266 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6267 /* Iterate through all cookies on this line */
6268
6269 /* find att_beg */
6270 att_beg = prev + 1;
6271 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6272 att_beg++;
6273
6274 /* find att_end : this is the first character after the last non
6275 * space before the equal. It may be equal to hdr_end.
6276 */
6277 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006279 while (equal < hdr_end) {
6280 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006281 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006282 if (http_is_spht[(unsigned char)*equal++])
6283 continue;
6284 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006285 }
6286
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006287 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6288 * is between <att_beg> and <equal>, both may be identical.
6289 */
6290
6291 /* look for end of cookie if there is an equal sign */
6292 if (equal < hdr_end && *equal == '=') {
6293 /* look for the beginning of the value */
6294 val_beg = equal + 1;
6295 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6296 val_beg++;
6297
6298 /* find the end of the value, respecting quotes */
6299 next = find_cookie_value_end(val_beg, hdr_end);
6300
6301 /* make val_end point to the first white space or delimitor after the value */
6302 val_end = next;
6303 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6304 val_end--;
6305 } else {
6306 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006307 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006308
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 /* We have nothing to do with attributes beginning with '$'. However,
6310 * they will automatically be removed if a header before them is removed,
6311 * since they're supposed to be linked together.
6312 */
6313 if (*att_beg == '$')
6314 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006315
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006316 /* Ignore cookies with no equal sign */
6317 if (equal == next) {
6318 /* This is not our cookie, so we must preserve it. But if we already
6319 * scheduled another cookie for removal, we cannot remove the
6320 * complete header, but we can remove the previous block itself.
6321 */
6322 preserve_hdr = 1;
6323 if (del_from != NULL) {
6324 int delta = del_hdr_value(req, &del_from, prev);
6325 val_end += delta;
6326 next += delta;
6327 hdr_end += delta;
6328 hdr_next += delta;
6329 cur_hdr->len += delta;
6330 http_msg_move_end(&txn->req, delta);
6331 prev = del_from;
6332 del_from = NULL;
6333 }
6334 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006335 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006337 /* if there are spaces around the equal sign, we need to
6338 * strip them otherwise we'll get trouble for cookie captures,
6339 * or even for rewrites. Since this happens extremely rarely,
6340 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006341 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6343 int stripped_before = 0;
6344 int stripped_after = 0;
6345
6346 if (att_end != equal) {
6347 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6348 equal += stripped_before;
6349 val_beg += stripped_before;
6350 }
6351
6352 if (val_beg > equal + 1) {
6353 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6354 val_beg += stripped_after;
6355 stripped_before += stripped_after;
6356 }
6357
6358 val_end += stripped_before;
6359 next += stripped_before;
6360 hdr_end += stripped_before;
6361 hdr_next += stripped_before;
6362 cur_hdr->len += stripped_before;
6363 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006364 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006365 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006366
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006367 /* First, let's see if we want to capture this cookie. We check
6368 * that we don't already have a client side cookie, because we
6369 * can only capture one. Also as an optimisation, we ignore
6370 * cookies shorter than the declared name.
6371 */
6372 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6373 (val_end - att_beg >= t->fe->capture_namelen) &&
6374 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6375 int log_len = val_end - att_beg;
6376
6377 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6378 Alert("HTTP logging : out of memory.\n");
6379 } else {
6380 if (log_len > t->fe->capture_len)
6381 log_len = t->fe->capture_len;
6382 memcpy(txn->cli_cookie, att_beg, log_len);
6383 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006384 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006385 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006386
Willy Tarreaubca99692010-10-06 19:25:55 +02006387 /* Persistence cookies in passive, rewrite or insert mode have the
6388 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006389 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006390 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006391 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006392 * For cookies in prefix mode, the form is :
6393 *
6394 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006395 */
6396 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6397 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6398 struct server *srv = t->be->srv;
6399 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006400
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006401 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6402 * have the server ID between val_beg and delim, and the original cookie between
6403 * delim+1 and val_end. Otherwise, delim==val_end :
6404 *
6405 * Cookie: NAME=SRV; # in all but prefix modes
6406 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6407 * | || || | |+-> next
6408 * | || || | +--> val_end
6409 * | || || +---------> delim
6410 * | || |+------------> val_beg
6411 * | || +-------------> att_end = equal
6412 * | |+-----------------> att_beg
6413 * | +------------------> prev
6414 * +-------------------------> hdr_beg
6415 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006416
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006417 if (t->be->options & PR_O_COOK_PFX) {
6418 for (delim = val_beg; delim < val_end; delim++)
6419 if (*delim == COOKIE_DELIM)
6420 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006421 } else {
6422 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006423 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006424 /* Now check if the cookie contains a date field, which would
6425 * appear after a vertical bar ('|') just after the server name
6426 * and before the delimiter.
6427 */
6428 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6429 if (vbar1) {
6430 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006431 * right is the last seen date. It is a base64 encoded
6432 * 30-bit value representing the UNIX date since the
6433 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006434 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006435 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006436 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006437 if (val_end - vbar1 >= 5) {
6438 val = b64tos30(vbar1);
6439 if (val > 0)
6440 txn->cookie_last_date = val << 2;
6441 }
6442 /* look for a second vertical bar */
6443 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6444 if (vbar1 && (val_end - vbar1 > 5)) {
6445 val = b64tos30(vbar1 + 1);
6446 if (val > 0)
6447 txn->cookie_first_date = val << 2;
6448 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006449 }
6450 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006451
Willy Tarreauf64d1412010-10-07 20:06:11 +02006452 /* if the cookie has an expiration date and the proxy wants to check
6453 * it, then we do that now. We first check if the cookie is too old,
6454 * then only if it has expired. We detect strict overflow because the
6455 * time resolution here is not great (4 seconds). Cookies with dates
6456 * in the future are ignored if their offset is beyond one day. This
6457 * allows an admin to fix timezone issues without expiring everyone
6458 * and at the same time avoids keeping unwanted side effects for too
6459 * long.
6460 */
6461 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006462 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6463 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006464 txn->flags &= ~TX_CK_MASK;
6465 txn->flags |= TX_CK_OLD;
6466 delim = val_beg; // let's pretend we have not found the cookie
6467 txn->cookie_first_date = 0;
6468 txn->cookie_last_date = 0;
6469 }
6470 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006471 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6472 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006473 txn->flags &= ~TX_CK_MASK;
6474 txn->flags |= TX_CK_EXPIRED;
6475 delim = val_beg; // let's pretend we have not found the cookie
6476 txn->cookie_first_date = 0;
6477 txn->cookie_last_date = 0;
6478 }
6479
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006480 /* Here, we'll look for the first running server which supports the cookie.
6481 * This allows to share a same cookie between several servers, for example
6482 * to dedicate backup servers to specific servers only.
6483 * However, to prevent clients from sticking to cookie-less backup server
6484 * when they have incidentely learned an empty cookie, we simply ignore
6485 * empty cookies and mark them as invalid.
6486 * The same behaviour is applied when persistence must be ignored.
6487 */
6488 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6489 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006490
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006491 while (srv) {
6492 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6493 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6494 if ((srv->state & SRV_RUNNING) ||
6495 (t->be->options & PR_O_PERSIST) ||
6496 (t->flags & SN_FORCE_PRST)) {
6497 /* we found the server and we can use it */
6498 txn->flags &= ~TX_CK_MASK;
6499 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6500 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006501 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006502 break;
6503 } else {
6504 /* we found a server, but it's down,
6505 * mark it as such and go on in case
6506 * another one is available.
6507 */
6508 txn->flags &= ~TX_CK_MASK;
6509 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006510 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006511 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006512 srv = srv->next;
6513 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006514
Willy Tarreauf64d1412010-10-07 20:06:11 +02006515 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006516 /* no server matched this cookie */
6517 txn->flags &= ~TX_CK_MASK;
6518 txn->flags |= TX_CK_INVALID;
6519 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006520
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006521 /* depending on the cookie mode, we may have to either :
6522 * - delete the complete cookie if we're in insert+indirect mode, so that
6523 * the server never sees it ;
6524 * - remove the server id from the cookie value, and tag the cookie as an
6525 * application cookie so that it does not get accidentely removed later,
6526 * if we're in cookie prefix mode
6527 */
6528 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6529 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006530
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006531 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6532 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);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006538
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006539 del_from = NULL;
6540 preserve_hdr = 1; /* we want to keep this cookie */
6541 }
6542 else if (del_from == NULL &&
6543 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6544 del_from = prev;
6545 }
6546 } else {
6547 /* This is not our cookie, so we must preserve it. But if we already
6548 * scheduled another cookie for removal, we cannot remove the
6549 * complete header, but we can remove the previous block itself.
6550 */
6551 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006552
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006553 if (del_from != NULL) {
6554 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006555 if (att_beg >= del_from)
6556 att_beg += delta;
6557 if (att_end >= del_from)
6558 att_end += delta;
6559 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006560 val_end += delta;
6561 next += delta;
6562 hdr_end += delta;
6563 hdr_next += delta;
6564 cur_hdr->len += delta;
6565 http_msg_move_end(&txn->req, delta);
6566 prev = del_from;
6567 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006568 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006569 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006570
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006571 /* Look for the appsession cookie unless persistence must be ignored */
6572 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6573 int cmp_len, value_len;
6574 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006575
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006576 if (t->be->options2 & PR_O2_AS_PFX) {
6577 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6578 value_begin = att_beg + t->be->appsession_name_len;
6579 value_len = val_end - att_beg - t->be->appsession_name_len;
6580 } else {
6581 cmp_len = att_end - att_beg;
6582 value_begin = val_beg;
6583 value_len = val_end - val_beg;
6584 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006585
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586 /* let's see if the cookie is our appcookie */
6587 if (cmp_len == t->be->appsession_name_len &&
6588 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6589 manage_client_side_appsession(t, value_begin, value_len);
6590 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006591 }
6592
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006593 /* continue with next cookie on this header line */
6594 att_beg = next;
6595 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006596
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006597 /* There are no more cookies on this line.
6598 * We may still have one (or several) marked for deletion at the
6599 * end of the line. We must do this now in two ways :
6600 * - if some cookies must be preserved, we only delete from the
6601 * mark to the end of line ;
6602 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006603 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006604 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006605 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006606 if (preserve_hdr) {
6607 delta = del_hdr_value(req, &del_from, hdr_end);
6608 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006609 cur_hdr->len += delta;
6610 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006611 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006612
6613 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006614 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6615 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006616 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006617 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006618 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006619 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006620 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006621 }
6622
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006623 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006624 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006625 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006626}
6627
6628
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629/* Iterate the same filter through all response headers contained in <rtr>.
6630 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6631 */
6632int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6633{
6634 char term;
6635 char *cur_ptr, *cur_end, *cur_next;
6636 int cur_idx, old_idx, last_hdr;
6637 struct http_txn *txn = &t->txn;
6638 struct hdr_idx_elem *cur_hdr;
6639 int len, delta;
6640
6641 last_hdr = 0;
6642
Willy Tarreau962c3f42010-01-10 00:15:35 +01006643 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006644 old_idx = 0;
6645
6646 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006647 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006649 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006650 (exp->action == ACT_ALLOW ||
6651 exp->action == ACT_DENY))
6652 return 0;
6653
6654 cur_idx = txn->hdr_idx.v[old_idx].next;
6655 if (!cur_idx)
6656 break;
6657
6658 cur_hdr = &txn->hdr_idx.v[cur_idx];
6659 cur_ptr = cur_next;
6660 cur_end = cur_ptr + cur_hdr->len;
6661 cur_next = cur_end + cur_hdr->cr + 1;
6662
6663 /* Now we have one header between cur_ptr and cur_end,
6664 * and the next header starts at cur_next.
6665 */
6666
6667 /* The annoying part is that pattern matching needs
6668 * that we modify the contents to null-terminate all
6669 * strings before testing them.
6670 */
6671
6672 term = *cur_end;
6673 *cur_end = '\0';
6674
6675 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6676 switch (exp->action) {
6677 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006678 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006679 last_hdr = 1;
6680 break;
6681
6682 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006683 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006684 last_hdr = 1;
6685 break;
6686
6687 case ACT_REPLACE:
6688 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6689 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6690 /* FIXME: if the user adds a newline in the replacement, the
6691 * index will not be recalculated for now, and the new line
6692 * will not be counted as a new header.
6693 */
6694
6695 cur_end += delta;
6696 cur_next += delta;
6697 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006698 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006699 break;
6700
6701 case ACT_REMOVE:
6702 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6703 cur_next += delta;
6704
Willy Tarreaufa355d42009-11-29 18:12:29 +01006705 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6707 txn->hdr_idx.used--;
6708 cur_hdr->len = 0;
6709 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006710 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006711 break;
6712
6713 }
6714 }
6715 if (cur_end)
6716 *cur_end = term; /* restore the string terminator */
6717
6718 /* keep the link from this header to next one in case of later
6719 * removal of next header.
6720 */
6721 old_idx = cur_idx;
6722 }
6723 return 0;
6724}
6725
6726
6727/* Apply the filter to the status line in the response buffer <rtr>.
6728 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6729 * or -1 if a replacement resulted in an invalid status line.
6730 */
6731int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6732{
6733 char term;
6734 char *cur_ptr, *cur_end;
6735 int done;
6736 struct http_txn *txn = &t->txn;
6737 int len, delta;
6738
6739
Willy Tarreau3d300592007-03-18 18:34:41 +01006740 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006741 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006742 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006743 (exp->action == ACT_ALLOW ||
6744 exp->action == ACT_DENY))
6745 return 0;
6746 else if (exp->action == ACT_REMOVE)
6747 return 0;
6748
6749 done = 0;
6750
Willy Tarreau962c3f42010-01-10 00:15:35 +01006751 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006752 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006753
6754 /* Now we have the status line between cur_ptr and cur_end */
6755
6756 /* The annoying part is that pattern matching needs
6757 * that we modify the contents to null-terminate all
6758 * strings before testing them.
6759 */
6760
6761 term = *cur_end;
6762 *cur_end = '\0';
6763
6764 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6765 switch (exp->action) {
6766 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006767 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006768 done = 1;
6769 break;
6770
6771 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006772 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006773 done = 1;
6774 break;
6775
6776 case ACT_REPLACE:
6777 *cur_end = term; /* restore the string terminator */
6778 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6779 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6780 /* FIXME: if the user adds a newline in the replacement, the
6781 * index will not be recalculated for now, and the new line
6782 * will not be counted as a new header.
6783 */
6784
Willy Tarreaufa355d42009-11-29 18:12:29 +01006785 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006786 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006787 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006788 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006789 cur_ptr, cur_end + 1,
6790 NULL, NULL);
6791 if (unlikely(!cur_end))
6792 return -1;
6793
6794 /* we have a full respnse and we know that we have either a CR
6795 * or an LF at <ptr>.
6796 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006797 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006798 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799 /* there is no point trying this regex on headers */
6800 return 1;
6801 }
6802 }
6803 *cur_end = term; /* restore the string terminator */
6804 return done;
6805}
6806
6807
6808
6809/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006810 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6812 * unparsable response.
6813 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006814int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006815{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006816 struct http_txn *txn = &s->txn;
6817 struct hdr_exp *exp;
6818
6819 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006820 int ret;
6821
6822 /*
6823 * The interleaving of transformations and verdicts
6824 * makes it difficult to decide to continue or stop
6825 * the evaluation.
6826 */
6827
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006828 if (txn->flags & TX_SVDENY)
6829 break;
6830
Willy Tarreau3d300592007-03-18 18:34:41 +01006831 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6833 exp->action == ACT_PASS)) {
6834 exp = exp->next;
6835 continue;
6836 }
6837
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006838 /* if this filter had a condition, evaluate it now and skip to
6839 * next filter if the condition does not match.
6840 */
6841 if (exp->cond) {
6842 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6843 ret = acl_pass(ret);
6844 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6845 ret = !ret;
6846 if (!ret)
6847 continue;
6848 }
6849
Willy Tarreaua15645d2007-03-18 16:22:39 +01006850 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006851 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006852 if (unlikely(ret < 0))
6853 return -1;
6854
6855 if (likely(ret == 0)) {
6856 /* The filter did not match the response, it can be
6857 * iterated through all headers.
6858 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006859 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006861 }
6862 return 0;
6863}
6864
6865
Willy Tarreaua15645d2007-03-18 16:22:39 +01006866/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006867 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006868 * desirable to call it only when needed. This function is also used when we
6869 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006870 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006871void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006872{
6873 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006874 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006875 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006876 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006877 char *hdr_beg, *hdr_end, *hdr_next;
6878 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006879
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880 /* Iterate through the headers.
6881 * we start with the start line.
6882 */
6883 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006884 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006885
6886 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6887 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006888 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006889
6890 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 hdr_beg = hdr_next;
6892 hdr_end = hdr_beg + cur_hdr->len;
6893 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006894
Willy Tarreau24581ba2010-08-31 22:39:35 +02006895 /* We have one full header between hdr_beg and hdr_end, and the
6896 * next header starts at hdr_next. We're only interested in
6897 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006898 */
6899
Willy Tarreau24581ba2010-08-31 22:39:35 +02006900 is_cookie2 = 0;
6901 prev = hdr_beg + 10;
6902 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006903 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006904 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6905 if (!val) {
6906 old_idx = cur_idx;
6907 continue;
6908 }
6909 is_cookie2 = 1;
6910 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006911 }
6912
Willy Tarreau24581ba2010-08-31 22:39:35 +02006913 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6914 * <prev> points to the colon.
6915 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006916 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006917
Willy Tarreau24581ba2010-08-31 22:39:35 +02006918 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6919 * check-cache is enabled) and we are not interested in checking
6920 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006921 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006922 if (t->be->cookie_name == NULL &&
6923 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006924 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006925 return;
6926
Willy Tarreau24581ba2010-08-31 22:39:35 +02006927 /* OK so now we know we have to process this response cookie.
6928 * The format of the Set-Cookie header is slightly different
6929 * from the format of the Cookie header in that it does not
6930 * support the comma as a cookie delimiter (thus the header
6931 * cannot be folded) because the Expires attribute described in
6932 * the original Netscape's spec may contain an unquoted date
6933 * with a comma inside. We have to live with this because
6934 * many browsers don't support Max-Age and some browsers don't
6935 * support quoted strings. However the Set-Cookie2 header is
6936 * clean.
6937 *
6938 * We have to keep multiple pointers in order to support cookie
6939 * removal at the beginning, middle or end of header without
6940 * corrupting the header (in case of set-cookie2). A special
6941 * pointer, <scav> points to the beginning of the set-cookie-av
6942 * fields after the first semi-colon. The <next> pointer points
6943 * either to the end of line (set-cookie) or next unquoted comma
6944 * (set-cookie2). All of these headers are valid :
6945 *
6946 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6947 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6948 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6949 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6950 * | | | | | | | | | |
6951 * | | | | | | | | +-> next hdr_end <--+
6952 * | | | | | | | +------------> scav
6953 * | | | | | | +--------------> val_end
6954 * | | | | | +--------------------> val_beg
6955 * | | | | +----------------------> equal
6956 * | | | +------------------------> att_end
6957 * | | +----------------------------> att_beg
6958 * | +------------------------------> prev
6959 * +-----------------------------------------> hdr_beg
6960 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006961
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 for (; prev < hdr_end; prev = next) {
6963 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006964
Willy Tarreau24581ba2010-08-31 22:39:35 +02006965 /* find att_beg */
6966 att_beg = prev + 1;
6967 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6968 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006969
Willy Tarreau24581ba2010-08-31 22:39:35 +02006970 /* find att_end : this is the first character after the last non
6971 * space before the equal. It may be equal to hdr_end.
6972 */
6973 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974
Willy Tarreau24581ba2010-08-31 22:39:35 +02006975 while (equal < hdr_end) {
6976 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6977 break;
6978 if (http_is_spht[(unsigned char)*equal++])
6979 continue;
6980 att_end = equal;
6981 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006982
Willy Tarreau24581ba2010-08-31 22:39:35 +02006983 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6984 * is between <att_beg> and <equal>, both may be identical.
6985 */
6986
6987 /* look for end of cookie if there is an equal sign */
6988 if (equal < hdr_end && *equal == '=') {
6989 /* look for the beginning of the value */
6990 val_beg = equal + 1;
6991 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6992 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993
Willy Tarreau24581ba2010-08-31 22:39:35 +02006994 /* find the end of the value, respecting quotes */
6995 next = find_cookie_value_end(val_beg, hdr_end);
6996
6997 /* make val_end point to the first white space or delimitor after the value */
6998 val_end = next;
6999 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7000 val_end--;
7001 } else {
7002 /* <equal> points to next comma, semi-colon or EOL */
7003 val_beg = val_end = next = equal;
7004 }
7005
7006 if (next < hdr_end) {
7007 /* Set-Cookie2 supports multiple cookies, and <next> points to
7008 * a colon or semi-colon before the end. So skip all attr-value
7009 * pairs and look for the next comma. For Set-Cookie, since
7010 * commas are permitted in values, skip to the end.
7011 */
7012 if (is_cookie2)
7013 next = find_hdr_value_end(next, hdr_end);
7014 else
7015 next = hdr_end;
7016 }
7017
7018 /* Now everything is as on the diagram above */
7019
7020 /* Ignore cookies with no equal sign */
7021 if (equal == val_end)
7022 continue;
7023
7024 /* If there are spaces around the equal sign, we need to
7025 * strip them otherwise we'll get trouble for cookie captures,
7026 * or even for rewrites. Since this happens extremely rarely,
7027 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007028 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007029 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7030 int stripped_before = 0;
7031 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007032
Willy Tarreau24581ba2010-08-31 22:39:35 +02007033 if (att_end != equal) {
7034 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
7035 equal += stripped_before;
7036 val_beg += stripped_before;
7037 }
7038
7039 if (val_beg > equal + 1) {
7040 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
7041 val_beg += stripped_after;
7042 stripped_before += stripped_after;
7043 }
7044
7045 val_end += stripped_before;
7046 next += stripped_before;
7047 hdr_end += stripped_before;
7048 hdr_next += stripped_before;
7049 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007050 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007051 }
7052
7053 /* First, let's see if we want to capture this cookie. We check
7054 * that we don't already have a server side cookie, because we
7055 * can only capture one. Also as an optimisation, we ignore
7056 * cookies shorter than the declared name.
7057 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007058 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007059 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007060 (val_end - att_beg >= t->fe->capture_namelen) &&
7061 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7062 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007063 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007064 Alert("HTTP logging : out of memory.\n");
7065 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007066 else {
7067 if (log_len > t->fe->capture_len)
7068 log_len = t->fe->capture_len;
7069 memcpy(txn->srv_cookie, att_beg, log_len);
7070 txn->srv_cookie[log_len] = 0;
7071 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007072 }
7073
Willy Tarreau827aee92011-03-10 16:55:02 +01007074 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007075 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007076 if (!(t->flags & SN_IGNORE_PRST) &&
7077 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7078 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007079 /* assume passive cookie by default */
7080 txn->flags &= ~TX_SCK_MASK;
7081 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007082
7083 /* If the cookie is in insert mode on a known server, we'll delete
7084 * this occurrence because we'll insert another one later.
7085 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007086 * a direct access.
7087 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007088 if (t->be->options2 & PR_O2_COOK_PSV) {
7089 /* The "preserve" flag was set, we don't want to touch the
7090 * server's cookie.
7091 */
7092 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007093 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007094 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007095 /* this cookie must be deleted */
7096 if (*prev == ':' && next == hdr_end) {
7097 /* whole header */
7098 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
7099 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7100 txn->hdr_idx.used--;
7101 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007102 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007103 hdr_next += delta;
7104 http_msg_move_end(&txn->rsp, delta);
7105 /* note: while both invalid now, <next> and <hdr_end>
7106 * are still equal, so the for() will stop as expected.
7107 */
7108 } else {
7109 /* just remove the value */
7110 int delta = del_hdr_value(res, &prev, next);
7111 next = prev;
7112 hdr_end += delta;
7113 hdr_next += delta;
7114 cur_hdr->len += delta;
7115 http_msg_move_end(&txn->rsp, delta);
7116 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007117 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007118 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007119 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007120 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007121 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007122 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007123 * with this server since we know it.
7124 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007125 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007126 next += delta;
7127 hdr_end += delta;
7128 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007129 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007130 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007131
Willy Tarreauf1348312010-10-07 15:54:11 +02007132 txn->flags &= ~TX_SCK_MASK;
7133 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134 }
Willy Tarreau827aee92011-03-10 16:55:02 +01007135 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007136 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007137 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007138 */
Willy Tarreau827aee92011-03-10 16:55:02 +01007139 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007140 next += delta;
7141 hdr_end += delta;
7142 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007144 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007145
Willy Tarreau827aee92011-03-10 16:55:02 +01007146 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007147 txn->flags &= ~TX_SCK_MASK;
7148 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007149 }
7150 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007151 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7152 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007153 int cmp_len, value_len;
7154 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007155
Cyril Bontéb21570a2009-11-29 20:04:48 +01007156 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007157 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7158 value_begin = att_beg + t->be->appsession_name_len;
7159 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007160 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007161 cmp_len = att_end - att_beg;
7162 value_begin = val_beg;
7163 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007164 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007165
Cyril Bonté17530c32010-04-06 21:11:10 +02007166 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007167 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7168 /* free a possibly previously allocated memory */
7169 pool_free2(apools.sessid, txn->sessid);
7170
Cyril Bontéb21570a2009-11-29 20:04:48 +01007171 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007172 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007173 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7174 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7175 return;
7176 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007177 memcpy(txn->sessid, value_begin, value_len);
7178 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007180 }
7181 /* that's done for this cookie, check the next one on the same
7182 * line when next != hdr_end (only if is_cookie2).
7183 */
7184 }
7185 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007186 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007187 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007188
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007189 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007190 appsess *asession = NULL;
7191 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007192 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007193 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007194 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007195 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7196 Alert("Not enough Memory process_srv():asession:calloc().\n");
7197 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7198 return;
7199 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007200 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7201
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007202 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7203 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7204 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007205 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007206 return;
7207 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007208 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007209 asession->sessid[t->be->appsession_len] = 0;
7210
Willy Tarreau827aee92011-03-10 16:55:02 +01007211 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007212 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007213 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007214 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007215 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007216 return;
7217 }
7218 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007219 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007220
7221 asession->request_count = 0;
7222 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7223 }
7224
7225 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7226 asession->request_count++;
7227 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007228}
7229
7230
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231/*
7232 * Check if response is cacheable or not. Updates t->flags.
7233 */
7234void check_response_for_cacheability(struct session *t, struct buffer *rtr)
7235{
7236 struct http_txn *txn = &t->txn;
7237 char *p1, *p2;
7238
7239 char *cur_ptr, *cur_end, *cur_next;
7240 int cur_idx;
7241
Willy Tarreau5df51872007-11-25 16:20:08 +01007242 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 return;
7244
7245 /* Iterate through the headers.
7246 * we start with the start line.
7247 */
7248 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007249 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007250
7251 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7252 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007253 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254
7255 cur_hdr = &txn->hdr_idx.v[cur_idx];
7256 cur_ptr = cur_next;
7257 cur_end = cur_ptr + cur_hdr->len;
7258 cur_next = cur_end + cur_hdr->cr + 1;
7259
7260 /* We have one full header between cur_ptr and cur_end, and the
7261 * next header starts at cur_next. We're only interested in
7262 * "Cookie:" headers.
7263 */
7264
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007265 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7266 if (val) {
7267 if ((cur_end - (cur_ptr + val) >= 8) &&
7268 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7269 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7270 return;
7271 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007272 }
7273
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007274 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7275 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007276 continue;
7277
7278 /* OK, right now we know we have a cache-control header at cur_ptr */
7279
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007280 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281
7282 if (p1 >= cur_end) /* no more info */
7283 continue;
7284
7285 /* p1 is at the beginning of the value */
7286 p2 = p1;
7287
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007288 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289 p2++;
7290
7291 /* we have a complete value between p1 and p2 */
7292 if (p2 < cur_end && *p2 == '=') {
7293 /* we have something of the form no-cache="set-cookie" */
7294 if ((cur_end - p1 >= 21) &&
7295 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7296 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007297 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007298 continue;
7299 }
7300
7301 /* OK, so we know that either p2 points to the end of string or to a comma */
7302 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7303 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7304 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7305 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007306 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007307 return;
7308 }
7309
7310 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007311 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312 continue;
7313 }
7314 }
7315}
7316
7317
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318/*
7319 * Try to retrieve a known appsession in the URI, then the associated server.
7320 * If the server is found, it's assigned to the session.
7321 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007322void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007323{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007324 char *end_params, *first_param, *cur_param, *next_param;
7325 char separator;
7326 int value_len;
7327
7328 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007329
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007330 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007331 (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 +01007332 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007333 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007334
Cyril Bontéb21570a2009-11-29 20:04:48 +01007335 first_param = NULL;
7336 switch (mode) {
7337 case PR_O2_AS_M_PP:
7338 first_param = memchr(begin, ';', len);
7339 break;
7340 case PR_O2_AS_M_QS:
7341 first_param = memchr(begin, '?', len);
7342 break;
7343 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007344
Cyril Bontéb21570a2009-11-29 20:04:48 +01007345 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007346 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007347 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007348
Cyril Bontéb21570a2009-11-29 20:04:48 +01007349 switch (mode) {
7350 case PR_O2_AS_M_PP:
7351 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7352 end_params = (char *) begin + len;
7353 }
7354 separator = ';';
7355 break;
7356 case PR_O2_AS_M_QS:
7357 end_params = (char *) begin + len;
7358 separator = '&';
7359 break;
7360 default:
7361 /* unknown mode, shouldn't happen */
7362 return;
7363 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007364
Cyril Bontéb21570a2009-11-29 20:04:48 +01007365 cur_param = next_param = end_params;
7366 while (cur_param > first_param) {
7367 cur_param--;
7368 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7369 /* let's see if this is the appsession parameter */
7370 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7371 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7372 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7373 /* Cool... it's the right one */
7374 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7375 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7376 if (value_len > 0) {
7377 manage_client_side_appsession(t, cur_param, value_len);
7378 }
7379 break;
7380 }
7381 next_param = cur_param;
7382 }
7383 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007384#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007385 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007386 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007387#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007388}
7389
Willy Tarreaub2513902006-12-17 14:52:38 +01007390/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007391 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007392 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007393 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007394 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007395 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007396 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007397 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007398 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007399int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007400{
7401 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007402 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007403
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007404 if (!uri_auth)
7405 return 0;
7406
Cyril Bonté70be45d2010-10-12 00:14:35 +02007407 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007408 return 0;
7409
Willy Tarreau295a8372011-03-10 11:25:07 +01007410 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007412 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007413 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007414 return 0;
7415
Willy Tarreau962c3f42010-01-10 00:15:35 +01007416 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007417
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007418 /* the URI is in h */
7419 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007420 return 0;
7421
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007422 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007423 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007424 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007425 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007426 break;
7427 }
7428 h++;
7429 }
7430
7431 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007432 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7433 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007434 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007435 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007436 break;
7437 }
7438 h++;
7439 }
7440 }
7441
Willy Tarreau962c3f42010-01-10 00:15:35 +01007442 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7443 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007444 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007445 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007446 break;
7447 }
7448 h++;
7449 }
7450
Cyril Bonté70be45d2010-10-12 00:14:35 +02007451 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7452 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7453 if (memcmp(h, ";st=", 4) == 0) {
7454 h += 4;
7455
7456 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007457 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007458 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007459 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007460 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007461 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007462 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007463 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007464 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007465 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007466 break;
7467 }
7468 h++;
7469 }
7470
Willy Tarreau295a8372011-03-10 11:25:07 +01007471 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007472
Willy Tarreaub2513902006-12-17 14:52:38 +01007473 return 1;
7474}
7475
Willy Tarreau4076a152009-04-02 15:18:36 +02007476/*
7477 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007478 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7479 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007480 */
7481void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7482 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007483 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007484{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007485 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7486 int len1 = buf->size - msg->som;
7487 es->len = buf->r - (buf->data + msg->som) + buf->size;
7488 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7489 if (es->len > len1 && len1 < sizeof(es->buf))
7490 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7491 }
7492 else {
7493 es->len = buf->r - (buf->data + msg->som);
7494 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7495 }
7496
Willy Tarreau4076a152009-04-02 15:18:36 +02007497 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007498 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007499 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007500 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007501 else
7502 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7503
Willy Tarreau4076a152009-04-02 15:18:36 +02007504 es->when = date; // user-visible date
7505 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007506 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007507 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007508 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007509 es->state = state;
7510 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007511 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007512}
Willy Tarreaub2513902006-12-17 14:52:38 +01007513
Willy Tarreau294c4732011-12-16 21:35:50 +01007514/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7515 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7516 * performed over the whole headers. Otherwise it must contain a valid header
7517 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7518 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7519 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7520 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7521 * -1.
7522 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007523 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007524unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7525 struct hdr_idx *idx, int occ,
7526 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007527{
Willy Tarreau294c4732011-12-16 21:35:50 +01007528 struct hdr_ctx local_ctx;
7529 char *ptr_hist[MAX_HDR_HISTORY];
7530 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007531 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007532 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007533
Willy Tarreau294c4732011-12-16 21:35:50 +01007534 if (!ctx) {
7535 local_ctx.idx = 0;
7536 ctx = &local_ctx;
7537 }
7538
Willy Tarreaubce70882009-09-07 11:51:47 +02007539 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007540 /* search from the beginning */
7541 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007542 occ--;
7543 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007544 *vptr = ctx->line + ctx->val;
7545 *vlen = ctx->vlen;
7546 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007547 }
7548 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007549 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007550 }
7551
7552 /* negative occurrence, we scan all the list then walk back */
7553 if (-occ > MAX_HDR_HISTORY)
7554 return 0;
7555
Willy Tarreau294c4732011-12-16 21:35:50 +01007556 found = hist_ptr = 0;
7557 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
7558 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7559 len_hist[hist_ptr] = ctx->vlen;
7560 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007561 hist_ptr = 0;
7562 found++;
7563 }
7564 if (-occ > found)
7565 return 0;
7566 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7567 * find occurrence -occ, so we have to check [hist_ptr+occ].
7568 */
7569 hist_ptr += occ;
7570 if (hist_ptr >= MAX_HDR_HISTORY)
7571 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007572 *vptr = ptr_hist[hist_ptr];
7573 *vlen = len_hist[hist_ptr];
7574 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007575}
7576
Willy Tarreaubaaee002006-06-26 02:48:02 +02007577/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007578 * Print a debug line with a header
7579 */
7580void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7581{
7582 int len, max;
7583 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007584 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007585 max = end - start;
7586 UBOUND(max, sizeof(trash) - len - 1);
7587 len += strlcpy2(trash + len, start, max + 1);
7588 trash[len++] = '\n';
7589 write(1, trash, len);
7590}
7591
Willy Tarreau0937bc42009-12-22 15:03:09 +01007592/*
7593 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7594 * the required fields are properly allocated and that we only need to (re)init
7595 * them. This should be used before processing any new request.
7596 */
7597void http_init_txn(struct session *s)
7598{
7599 struct http_txn *txn = &s->txn;
7600 struct proxy *fe = s->fe;
7601
7602 txn->flags = 0;
7603 txn->status = -1;
7604
Willy Tarreauf64d1412010-10-07 20:06:11 +02007605 txn->cookie_first_date = 0;
7606 txn->cookie_last_date = 0;
7607
Willy Tarreau0937bc42009-12-22 15:03:09 +01007608 txn->req.sol = txn->req.eol = NULL;
7609 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7610 txn->rsp.sol = txn->rsp.eol = NULL;
7611 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007612 txn->req.chunk_len = 0LL;
7613 txn->req.body_len = 0LL;
7614 txn->rsp.chunk_len = 0LL;
7615 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007616 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7617 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007618
7619 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007620
7621 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7622 if (fe->options2 & PR_O2_REQBUG_OK)
7623 txn->req.err_pos = -1; /* let buggy requests pass */
7624
Willy Tarreau46023632010-01-07 22:51:47 +01007625 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007626 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7627
Willy Tarreau46023632010-01-07 22:51:47 +01007628 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007629 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7630
7631 if (txn->hdr_idx.v)
7632 hdr_idx_init(&txn->hdr_idx);
7633}
7634
7635/* to be used at the end of a transaction */
7636void http_end_txn(struct session *s)
7637{
7638 struct http_txn *txn = &s->txn;
7639
7640 /* these ones will have been dynamically allocated */
7641 pool_free2(pool2_requri, txn->uri);
7642 pool_free2(pool2_capture, txn->cli_cookie);
7643 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007644 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007645
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007646 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007647 txn->uri = NULL;
7648 txn->srv_cookie = NULL;
7649 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007650
7651 if (txn->req.cap) {
7652 struct cap_hdr *h;
7653 for (h = s->fe->req_cap; h; h = h->next)
7654 pool_free2(h->pool, txn->req.cap[h->index]);
7655 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7656 }
7657
7658 if (txn->rsp.cap) {
7659 struct cap_hdr *h;
7660 for (h = s->fe->rsp_cap; h; h = h->next)
7661 pool_free2(h->pool, txn->rsp.cap[h->index]);
7662 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7663 }
7664
Willy Tarreau0937bc42009-12-22 15:03:09 +01007665}
7666
7667/* to be used at the end of a transaction to prepare a new one */
7668void http_reset_txn(struct session *s)
7669{
7670 http_end_txn(s);
7671 http_init_txn(s);
7672
7673 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007674 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007675 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007676 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007677 /* re-init store persistence */
7678 s->store_count = 0;
7679
Willy Tarreau0937bc42009-12-22 15:03:09 +01007680 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007681
7682 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7683
Willy Tarreau739cfba2010-01-25 23:11:14 +01007684 /* We must trim any excess data from the response buffer, because we
7685 * may have blocked an invalid response from a server that we don't
7686 * want to accidentely forward once we disable the analysers, nor do
7687 * we want those data to come along with next response. A typical
7688 * example of such data would be from a buggy server responding to
7689 * a HEAD with some data, or sending more than the advertised
7690 * content-length.
7691 */
7692 if (unlikely(s->rep->l > s->rep->send_max)) {
7693 s->rep->l = s->rep->send_max;
7694 s->rep->r = s->rep->w + s->rep->l;
7695 if (s->rep->r >= s->rep->data + s->rep->size)
7696 s->rep->r -= s->rep->size;
7697 }
7698
Willy Tarreau0937bc42009-12-22 15:03:09 +01007699 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007700 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007701
Willy Tarreaud04e8582010-05-31 12:31:35 +02007702 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007703 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007704
7705 s->req->rex = TICK_ETERNITY;
7706 s->req->wex = TICK_ETERNITY;
7707 s->req->analyse_exp = TICK_ETERNITY;
7708 s->rep->rex = TICK_ETERNITY;
7709 s->rep->wex = TICK_ETERNITY;
7710 s->rep->analyse_exp = TICK_ETERNITY;
7711}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007712
Willy Tarreauff011f22011-01-06 17:51:27 +01007713void free_http_req_rules(struct list *r) {
7714 struct http_req_rule *tr, *pr;
7715
7716 list_for_each_entry_safe(pr, tr, r, list) {
7717 LIST_DEL(&pr->list);
7718 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7719 free(pr->http_auth.realm);
7720
7721 free(pr);
7722 }
7723}
7724
7725struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7726{
7727 struct http_req_rule *rule;
7728 int cur_arg;
7729
7730 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7731 if (!rule) {
7732 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7733 return NULL;
7734 }
7735
7736 if (!*args[0]) {
7737 goto req_error_parsing;
7738 } else if (!strcmp(args[0], "allow")) {
7739 rule->action = HTTP_REQ_ACT_ALLOW;
7740 cur_arg = 1;
7741 } else if (!strcmp(args[0], "deny")) {
7742 rule->action = HTTP_REQ_ACT_DENY;
7743 cur_arg = 1;
7744 } else if (!strcmp(args[0], "auth")) {
7745 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7746 cur_arg = 1;
7747
7748 while(*args[cur_arg]) {
7749 if (!strcmp(args[cur_arg], "realm")) {
7750 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7751 cur_arg+=2;
7752 continue;
7753 } else
7754 break;
7755 }
7756 } else {
7757req_error_parsing:
7758 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7759 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7760 return NULL;
7761 }
7762
7763 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7764 struct acl_cond *cond;
7765
7766 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7767 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7768 file, linenum, args[0]);
7769 return NULL;
7770 }
7771 rule->cond = cond;
7772 }
7773 else if (*args[cur_arg]) {
7774 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7775 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7776 file, linenum, args[0], args[cur_arg]);
7777 return NULL;
7778 }
7779
7780 return rule;
7781}
7782
Willy Tarreau8797c062007-05-07 00:55:35 +02007783/************************************************************************/
7784/* The code below is dedicated to ACL parsing and matching */
7785/************************************************************************/
7786
7787
7788
7789
7790/* 1. Check on METHOD
7791 * We use the pre-parsed method if it is known, and store its number as an
7792 * integer. If it is unknown, we use the pointer and the length.
7793 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007794static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007795{
7796 int len, meth;
7797
Willy Tarreauae8b7962007-06-09 23:10:04 +02007798 len = strlen(*text);
7799 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007800
7801 pattern->val.i = meth;
7802 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007803 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007804 if (!pattern->ptr.str)
7805 return 0;
7806 pattern->len = len;
7807 }
7808 return 1;
7809}
7810
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007811/* This function fetches the method of current HTTP request and stores
7812 * it in the global pattern struct as a chunk. There are two possibilities :
7813 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7814 * in <len> and <ptr> is NULL ;
7815 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7816 * <len> to its length.
7817 * This is intended to be used with acl_match_meth() only.
7818 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007819static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007820acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7821 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007822{
7823 int meth;
7824 struct http_txn *txn = l7;
7825
Willy Tarreaub6866442008-07-14 23:54:42 +02007826 if (!txn)
7827 return 0;
7828
Willy Tarreau655dce92009-11-08 13:10:58 +01007829 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007830 return 0;
7831
Willy Tarreau8797c062007-05-07 00:55:35 +02007832 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007833 temp_pattern.data.str.len = meth;
7834 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007835 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007836 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7837 /* ensure the indexes are not affected */
7838 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007839 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
7840 temp_pattern.data.str.str = txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007841 }
7842 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7843 return 1;
7844}
7845
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007846/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007847static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7848{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007849 int icase;
7850
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007851
7852 if (temp_pattern.data.str.str == NULL) {
7853 /* well-known method */
7854 if (temp_pattern.data.str.len == pattern->val.i)
7855 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007856 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007857 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007858
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007859 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7860 if (pattern->val.i != HTTP_METH_OTHER)
7861 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007862
7863 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007864 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007865 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007866
7867 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007868 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7869 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007870 return ACL_PAT_FAIL;
7871 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007872}
7873
7874/* 2. Check on Request/Status Version
7875 * We simply compare strings here.
7876 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007877static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007878{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007879 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007880 if (!pattern->ptr.str)
7881 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007882 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007883 return 1;
7884}
7885
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007886static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007887acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7888 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007889{
7890 struct http_txn *txn = l7;
7891 char *ptr;
7892 int len;
7893
Willy Tarreaub6866442008-07-14 23:54:42 +02007894 if (!txn)
7895 return 0;
7896
Willy Tarreau655dce92009-11-08 13:10:58 +01007897 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007898 return 0;
7899
Willy Tarreau8797c062007-05-07 00:55:35 +02007900 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007901 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007902
7903 while ((len-- > 0) && (*ptr++ != '/'));
7904 if (len <= 0)
7905 return 0;
7906
Willy Tarreau664092c2011-12-16 19:11:42 +01007907 temp_pattern.data.str.str = ptr;
7908 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007909
7910 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7911 return 1;
7912}
7913
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007914static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007915acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7916 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007917{
7918 struct http_txn *txn = l7;
7919 char *ptr;
7920 int len;
7921
Willy Tarreaub6866442008-07-14 23:54:42 +02007922 if (!txn)
7923 return 0;
7924
Willy Tarreau655dce92009-11-08 13:10:58 +01007925 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007926 return 0;
7927
Willy Tarreau8797c062007-05-07 00:55:35 +02007928 len = txn->rsp.sl.st.v_l;
7929 ptr = txn->rsp.sol;
7930
7931 while ((len-- > 0) && (*ptr++ != '/'));
7932 if (len <= 0)
7933 return 0;
7934
Willy Tarreau664092c2011-12-16 19:11:42 +01007935 temp_pattern.data.str.str = ptr;
7936 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007937
7938 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7939 return 1;
7940}
7941
7942/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007943static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007944acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7945 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007946{
7947 struct http_txn *txn = l7;
7948 char *ptr;
7949 int len;
7950
Willy Tarreaub6866442008-07-14 23:54:42 +02007951 if (!txn)
7952 return 0;
7953
Willy Tarreau655dce92009-11-08 13:10:58 +01007954 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007955 return 0;
7956
Willy Tarreau8797c062007-05-07 00:55:35 +02007957 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007958 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007959
Willy Tarreaua5e37562011-12-16 17:06:15 +01007960 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007961 test->flags = ACL_TEST_F_VOL_1ST;
7962 return 1;
7963}
7964
7965/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007966static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007967acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7968 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007969{
7970 struct http_txn *txn = l7;
7971
Willy Tarreaub6866442008-07-14 23:54:42 +02007972 if (!txn)
7973 return 0;
7974
Willy Tarreau655dce92009-11-08 13:10:58 +01007975 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007976 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007977
Willy Tarreauc11416f2007-06-17 16:58:38 +02007978 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7979 /* ensure the indexes are not affected */
7980 return 0;
7981
Willy Tarreau664092c2011-12-16 19:11:42 +01007982 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
7983 temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007984
Willy Tarreauf3d25982007-05-08 22:45:09 +02007985 /* we do not need to set READ_ONLY because the data is in a buffer */
7986 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007987 return 1;
7988}
7989
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007990static int
7991acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7992 struct acl_expr *expr, struct acl_test *test)
7993{
7994 struct http_txn *txn = l7;
7995
Willy Tarreaub6866442008-07-14 23:54:42 +02007996 if (!txn)
7997 return 0;
7998
Willy Tarreau655dce92009-11-08 13:10:58 +01007999 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008000 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008001
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008002 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8003 /* ensure the indexes are not affected */
8004 return 0;
8005
8006 /* Parse HTTP request */
Willy Tarreau6471afb2011-09-23 10:54:59 +02008007 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreauf4362b32011-12-16 17:49:52 +01008008 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
8009 return 0;
8010 temp_pattern.type = PATTERN_TYPE_IP;
8011 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008012
8013 /*
8014 * If we are parsing url in frontend space, we prepare backend stage
8015 * to not parse again the same url ! optimization lazyness...
8016 */
8017 if (px->options & PR_O_HTTP_PROXY)
8018 l4->flags |= SN_ADDR_SET;
8019
Willy Tarreauf4362b32011-12-16 17:49:52 +01008020 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008021 return 1;
8022}
8023
8024static int
8025acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
8026 struct acl_expr *expr, struct acl_test *test)
8027{
8028 struct http_txn *txn = l7;
8029
Willy Tarreaub6866442008-07-14 23:54:42 +02008030 if (!txn)
8031 return 0;
8032
Willy Tarreau655dce92009-11-08 13:10:58 +01008033 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008034 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008035
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008036 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8037 /* ensure the indexes are not affected */
8038 return 0;
8039
8040 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02008041 url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.to);
Willy Tarreaua5e37562011-12-16 17:06:15 +01008042 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008043
8044 if (px->options & PR_O_HTTP_PROXY)
8045 l4->flags |= SN_ADDR_SET;
8046
8047 test->flags = ACL_TEST_F_READ_ONLY;
8048 return 1;
8049}
8050
Willy Tarreauc11416f2007-06-17 16:58:38 +02008051/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
8052 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
8053 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008054static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008055acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008056 struct acl_expr *expr, struct acl_test *test)
8057{
8058 struct http_txn *txn = l7;
8059 struct hdr_idx *idx = &txn->hdr_idx;
8060 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008061
Willy Tarreaub6866442008-07-14 23:54:42 +02008062 if (!txn)
8063 return 0;
8064
Willy Tarreau33a7e692007-06-10 19:45:56 +02008065 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8066 /* search for header from the beginning */
8067 ctx->idx = 0;
8068
Willy Tarreau33a7e692007-06-10 19:45:56 +02008069 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8070 test->flags |= ACL_TEST_F_FETCH_MORE;
8071 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01008072 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
8073 temp_pattern.data.str.len = ctx->vlen;
8074
Willy Tarreau33a7e692007-06-10 19:45:56 +02008075 return 1;
8076 }
8077
8078 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8079 test->flags |= ACL_TEST_F_VOL_HDR;
8080 return 0;
8081}
8082
Willy Tarreau33a7e692007-06-10 19:45:56 +02008083static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008084acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
8085 struct acl_expr *expr, struct acl_test *test)
8086{
8087 struct http_txn *txn = l7;
8088
Willy Tarreaub6866442008-07-14 23:54:42 +02008089 if (!txn)
8090 return 0;
8091
Willy Tarreau655dce92009-11-08 13:10:58 +01008092 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008093 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008094
Willy Tarreauc11416f2007-06-17 16:58:38 +02008095 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8096 /* ensure the indexes are not affected */
8097 return 0;
8098
8099 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
8100}
8101
8102static int
8103acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
8104 struct acl_expr *expr, struct acl_test *test)
8105{
8106 struct http_txn *txn = l7;
8107
Willy Tarreaub6866442008-07-14 23:54:42 +02008108 if (!txn)
8109 return 0;
8110
Willy Tarreau655dce92009-11-08 13:10:58 +01008111 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008112 return 0;
8113
8114 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
8115}
8116
8117/* 6. Check on HTTP header count. The number of occurrences is returned.
8118 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8119 */
8120static int
8121acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008122 struct acl_expr *expr, struct acl_test *test)
8123{
8124 struct http_txn *txn = l7;
8125 struct hdr_idx *idx = &txn->hdr_idx;
8126 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008127 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008128
Willy Tarreaub6866442008-07-14 23:54:42 +02008129 if (!txn)
8130 return 0;
8131
Willy Tarreau33a7e692007-06-10 19:45:56 +02008132 ctx.idx = 0;
8133 cnt = 0;
8134 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
8135 cnt++;
8136
Willy Tarreaua5e37562011-12-16 17:06:15 +01008137 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008138 test->flags = ACL_TEST_F_VOL_HDR;
8139 return 1;
8140}
8141
Willy Tarreauc11416f2007-06-17 16:58:38 +02008142static int
8143acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8144 struct acl_expr *expr, struct acl_test *test)
8145{
8146 struct http_txn *txn = l7;
8147
Willy Tarreaub6866442008-07-14 23:54:42 +02008148 if (!txn)
8149 return 0;
8150
Willy Tarreau655dce92009-11-08 13:10:58 +01008151 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008152 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008153
Willy Tarreauc11416f2007-06-17 16:58:38 +02008154 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8155 /* ensure the indexes are not affected */
8156 return 0;
8157
8158 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
8159}
8160
8161static int
8162acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8163 struct acl_expr *expr, struct acl_test *test)
8164{
8165 struct http_txn *txn = l7;
8166
Willy Tarreaub6866442008-07-14 23:54:42 +02008167 if (!txn)
8168 return 0;
8169
Willy Tarreau655dce92009-11-08 13:10:58 +01008170 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008171 return 0;
8172
8173 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
8174}
8175
Willy Tarreau33a7e692007-06-10 19:45:56 +02008176/* 7. Check on HTTP header's integer value. The integer value is returned.
8177 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008178 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008179 */
8180static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02008181acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02008182 struct acl_expr *expr, struct acl_test *test)
8183{
8184 struct http_txn *txn = l7;
8185 struct hdr_idx *idx = &txn->hdr_idx;
8186 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008187
Willy Tarreaub6866442008-07-14 23:54:42 +02008188 if (!txn)
8189 return 0;
8190
Willy Tarreau33a7e692007-06-10 19:45:56 +02008191 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8192 /* search for header from the beginning */
8193 ctx->idx = 0;
8194
Willy Tarreau33a7e692007-06-10 19:45:56 +02008195 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
8196 test->flags |= ACL_TEST_F_FETCH_MORE;
8197 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01008198 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02008199 return 1;
8200 }
8201
8202 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8203 test->flags |= ACL_TEST_F_VOL_HDR;
8204 return 0;
8205}
8206
Willy Tarreauc11416f2007-06-17 16:58:38 +02008207static int
8208acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8209 struct acl_expr *expr, struct acl_test *test)
8210{
8211 struct http_txn *txn = l7;
8212
Willy Tarreaub6866442008-07-14 23:54:42 +02008213 if (!txn)
8214 return 0;
8215
Willy Tarreau655dce92009-11-08 13:10:58 +01008216 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008217 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008218
Willy Tarreauc11416f2007-06-17 16:58:38 +02008219 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8220 /* ensure the indexes are not affected */
8221 return 0;
8222
8223 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
8224}
8225
8226static int
8227acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
8228 struct acl_expr *expr, struct acl_test *test)
8229{
8230 struct http_txn *txn = l7;
8231
Willy Tarreaub6866442008-07-14 23:54:42 +02008232 if (!txn)
8233 return 0;
8234
Willy Tarreau655dce92009-11-08 13:10:58 +01008235 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008236 return 0;
8237
8238 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
8239}
8240
Willy Tarreau106f9792009-09-19 07:54:16 +02008241/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
8242 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
8243 */
8244static int
8245acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
8246 struct acl_expr *expr, struct acl_test *test)
8247{
8248 struct http_txn *txn = l7;
8249 struct hdr_idx *idx = &txn->hdr_idx;
8250 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
8251
8252 if (!txn)
8253 return 0;
8254
8255 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
8256 /* search for header from the beginning */
8257 ctx->idx = 0;
8258
Willy Tarreauf4362b32011-12-16 17:49:52 +01008259 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02008260 test->flags |= ACL_TEST_F_FETCH_MORE;
8261 test->flags |= ACL_TEST_F_VOL_HDR;
8262 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01008263 temp_pattern.type = PATTERN_TYPE_IP;
8264 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
8265 return 1;
8266 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02008267 }
8268
8269 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8270 test->flags |= ACL_TEST_F_VOL_HDR;
8271 return 0;
8272}
8273
8274static int
8275acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8276 struct acl_expr *expr, struct acl_test *test)
8277{
8278 struct http_txn *txn = l7;
8279
8280 if (!txn)
8281 return 0;
8282
Willy Tarreau655dce92009-11-08 13:10:58 +01008283 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008284 return 0;
8285
8286 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8287 /* ensure the indexes are not affected */
8288 return 0;
8289
8290 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8291}
8292
8293static int
8294acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8295 struct acl_expr *expr, struct acl_test *test)
8296{
8297 struct http_txn *txn = l7;
8298
8299 if (!txn)
8300 return 0;
8301
Willy Tarreau655dce92009-11-08 13:10:58 +01008302 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008303 return 0;
8304
8305 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8306}
8307
Willy Tarreau737b0c12007-06-10 21:28:46 +02008308/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8309 * the first '/' after the possible hostname, and ends before the possible '?'.
8310 */
8311static int
8312acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8313 struct acl_expr *expr, struct acl_test *test)
8314{
8315 struct http_txn *txn = l7;
8316 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008317
Willy Tarreaub6866442008-07-14 23:54:42 +02008318 if (!txn)
8319 return 0;
8320
Willy Tarreau655dce92009-11-08 13:10:58 +01008321 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008322 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008323
Willy Tarreauc11416f2007-06-17 16:58:38 +02008324 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8325 /* ensure the indexes are not affected */
8326 return 0;
8327
Willy Tarreau962c3f42010-01-10 00:15:35 +01008328 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008329 ptr = http_get_path(txn);
8330 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008331 return 0;
8332
8333 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008334 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008335
8336 while (ptr < end && *ptr != '?')
8337 ptr++;
8338
Willy Tarreau664092c2011-12-16 19:11:42 +01008339 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008340
8341 /* we do not need to set READ_ONLY because the data is in a buffer */
8342 test->flags = ACL_TEST_F_VOL_1ST;
8343 return 1;
8344}
8345
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008346static int
8347acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8348 struct acl_expr *expr, struct acl_test *test)
8349{
8350 struct buffer *req = s->req;
8351 struct http_txn *txn = &s->txn;
8352 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008353
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008354 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8355 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8356 */
8357
8358 if (!s || !req)
8359 return 0;
8360
Willy Tarreau655dce92009-11-08 13:10:58 +01008361 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008362 /* Already decoded as OK */
8363 test->flags |= ACL_TEST_F_SET_RES_PASS;
8364 return 1;
8365 }
8366
8367 /* Try to decode HTTP request */
8368 if (likely(req->lr < req->r))
8369 http_msg_analyzer(req, msg, &txn->hdr_idx);
8370
Willy Tarreau655dce92009-11-08 13:10:58 +01008371 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008372 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8373 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8374 return 1;
8375 }
8376 /* wait for final state */
8377 test->flags |= ACL_TEST_F_MAY_CHANGE;
8378 return 0;
8379 }
8380
8381 /* OK we got a valid HTTP request. We have some minor preparation to
8382 * perform so that further checks can rely on HTTP tests.
8383 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008384 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008385 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8386 s->flags |= SN_REDIRECTABLE;
8387
8388 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8389 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8390 return 1;
8391 }
8392
8393 test->flags |= ACL_TEST_F_SET_RES_PASS;
8394 return 1;
8395}
8396
Willy Tarreau7f18e522010-10-22 20:04:13 +02008397/* return a valid test if the current request is the first one on the connection */
8398static int
8399acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8400 struct acl_expr *expr, struct acl_test *test)
8401{
8402 if (!s)
8403 return 0;
8404
8405 if (s->txn.flags & TX_NOT_FIRST)
8406 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8407 else
8408 test->flags |= ACL_TEST_F_SET_RES_PASS;
8409
8410 return 1;
8411}
8412
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008413static int
8414acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8415 struct acl_expr *expr, struct acl_test *test)
8416{
8417
8418 if (!s)
8419 return 0;
8420
8421 if (!get_http_auth(s))
8422 return 0;
8423
8424 test->ctx.a[0] = expr->arg.ul;
8425 test->ctx.a[1] = s->txn.auth.user;
8426 test->ctx.a[2] = s->txn.auth.pass;
8427
8428 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8429
8430 return 1;
8431}
Willy Tarreau8797c062007-05-07 00:55:35 +02008432
8433/************************************************************************/
8434/* All supported keywords must be declared here. */
8435/************************************************************************/
8436
8437/* Note: must not be declared <const> as its list will be overwritten */
8438static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008439 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8440
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008441 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008442 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8443 { "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 +02008444 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008445
Willy Tarreauc4262962010-05-10 23:42:40 +02008446 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008447 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8448 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8449 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8450 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8451 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8452 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008453 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008454 { "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 +02008455 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008456
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008457 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008458 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008459 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8460 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8461 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8462 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8463 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8464 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8465 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008466 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008467 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008468 { "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 +02008469
Willy Tarreauc4262962010-05-10 23:42:40 +02008470 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008471 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8472 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8473 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8474 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8475 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8476 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8477 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008478 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008479 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008480 { "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 +02008481
Willy Tarreauc4262962010-05-10 23:42:40 +02008482 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008483 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8484 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8485 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8486 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8487 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8488 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008489 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008490
Willy Tarreauf3d25982007-05-08 22:45:09 +02008491#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008492 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8493 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8494 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8495 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8496 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8497 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8498 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8499
Willy Tarreau8797c062007-05-07 00:55:35 +02008500 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8501 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8502 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8503 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8504 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8505 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8506 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8507 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008508#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008509
Willy Tarreau7f18e522010-10-22 20:04:13 +02008510 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8511 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8512 { "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 +02008513 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008514}};
8515
Willy Tarreau4a568972010-05-12 08:08:50 +02008516/************************************************************************/
8517/* The code below is dedicated to pattern fetching and matching */
8518/************************************************************************/
8519
Willy Tarreaue428fb72011-12-16 21:50:30 +01008520/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008521static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008522pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8523 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008524{
8525 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008526
Willy Tarreaue428fb72011-12-16 21:50:30 +01008527 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8528 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008529}
8530
David Cournapeau16023ee2010-12-23 20:55:41 +09008531/*
8532 * Given a path string and its length, find the position of beginning of the
8533 * query string. Returns NULL if no query string is found in the path.
8534 *
8535 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8536 *
8537 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8538 */
8539static inline char *find_query_string(char *path, size_t path_l)
8540{
8541 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008542
David Cournapeau16023ee2010-12-23 20:55:41 +09008543 p = memchr(path, '?', path_l);
8544 return p ? p + 1 : NULL;
8545}
8546
8547static inline int is_param_delimiter(char c)
8548{
8549 return c == '&' || c == ';';
8550}
8551
8552/*
8553 * Given a url parameter, find the starting position of the first occurence,
8554 * or NULL if the parameter is not found.
8555 *
8556 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8557 * the function will return query_string+8.
8558 */
8559static char*
8560find_url_param_pos(char* query_string, size_t query_string_l,
8561 char* url_param_name, size_t url_param_name_l)
8562{
8563 char *pos, *last;
8564
8565 pos = query_string;
8566 last = query_string + query_string_l - url_param_name_l - 1;
8567
8568 while (pos <= last) {
8569 if (pos[url_param_name_l] == '=') {
8570 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8571 return pos;
8572 pos += url_param_name_l + 1;
8573 }
8574 while (pos <= last && !is_param_delimiter(*pos))
8575 pos++;
8576 pos++;
8577 }
8578 return NULL;
8579}
8580
8581/*
8582 * Given a url parameter name, returns its value and size into *value and
8583 * *value_l respectively, and returns non-zero. If the parameter is not found,
8584 * zero is returned and value/value_l are not touched.
8585 */
8586static int
8587find_url_param_value(char* path, size_t path_l,
8588 char* url_param_name, size_t url_param_name_l,
8589 char** value, size_t* value_l)
8590{
8591 char *query_string, *qs_end;
8592 char *arg_start;
8593 char *value_start, *value_end;
8594
8595 query_string = find_query_string(path, path_l);
8596 if (!query_string)
8597 return 0;
8598
8599 qs_end = path + path_l;
8600 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8601 url_param_name, url_param_name_l);
8602 if (!arg_start)
8603 return 0;
8604
8605 value_start = arg_start + url_param_name_l + 1;
8606 value_end = value_start;
8607
8608 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8609 value_end++;
8610
8611 *value = value_start;
8612 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008613 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008614}
8615
8616static int
8617pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8618 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8619{
8620 struct http_txn *txn = l7;
8621 struct http_msg *msg = &txn->req;
8622 char *url_param_value;
8623 size_t url_param_value_l;
8624
8625 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8626 arg_p->data.str.str, arg_p->data.str.len,
8627 &url_param_value, &url_param_value_l))
8628 return 0;
8629
8630 data->str.str = url_param_value;
8631 data->str.len = url_param_value_l;
8632 return 1;
8633}
8634
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008635/* Try to find the last occurrence of a cookie name in a cookie header value.
8636 * The pointer and size of the last occurrence of the cookie value is returned
8637 * into *value and *value_l, and the function returns non-zero if the value was
8638 * found. Otherwise if the cookie was not found, zero is returned and neither
8639 * value nor value_l are touched. The input hdr string should begin at the
8640 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8641 * value may represent a list of values (cookie headers).
8642 */
8643static int
8644extract_cookie_value(char *hdr, size_t hdr_l,
8645 char *cookie_name, size_t cookie_name_l, int list,
8646 char **value, size_t *value_l)
8647{
8648 int found = 0;
8649 char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end;
8650 char *next;
8651
8652 /* Note that multiple cookies may be delimited with semi-colons, so we
8653 * also have to loop on this.
8654 */
8655
8656 /* we search at least a cookie name followed by an equal, and more
8657 * generally something like this :
8658 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8659 */
8660 if (hdr_l < cookie_name_l + 1)
8661 return 0;
8662
8663 hdr_end = hdr + hdr_l;
8664
8665 for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) {
8666 /* Iterate through all cookies on this line */
8667
8668 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8669 att_beg++;
8670
8671 /* find att_end : this is the first character after the last non
8672 * space before the equal. It may be equal to hdr_end.
8673 */
8674 equal = att_end = att_beg;
8675
8676 while (equal < hdr_end) {
8677 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8678 break;
8679 if (http_is_spht[(unsigned char)*equal++])
8680 continue;
8681 att_end = equal;
8682 }
8683
8684 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8685 * is between <att_beg> and <equal>, both may be identical.
8686 */
8687
8688 /* look for end of cookie if there is an equal sign */
8689 if (equal < hdr_end && *equal == '=') {
8690 /* look for the beginning of the value */
8691 val_beg = equal + 1;
8692 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8693 val_beg++;
8694
8695 /* find the end of the value, respecting quotes */
8696 next = find_cookie_value_end(val_beg, hdr_end);
8697
8698 /* make val_end point to the first white space or delimitor after the value */
8699 val_end = next;
8700 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8701 val_end--;
8702 } else {
8703 val_beg = val_end = next = equal;
8704 }
8705
8706 /* We have nothing to do with attributes beginning with '$'. However,
8707 * they will automatically be removed if a header before them is removed,
8708 * since they're supposed to be linked together.
8709 */
8710 if (*att_beg == '$')
8711 continue;
8712
8713 /* Ignore cookies with no equal sign */
8714 if (equal == next)
8715 continue;
8716
8717 /* Now we have the cookie name between att_beg and att_end, and
8718 * its value between val_beg and val_end.
8719 */
8720
8721 if (att_end - att_beg == cookie_name_l &&
8722 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8723 found = 1;
8724 *value = val_beg;
8725 *value_l = val_end - val_beg;
8726 /* right now we want to catch the last occurrence
8727 * of the cookie, so let's go on searching.
8728 */
8729 }
8730
8731 /* Set-Cookie headers only have the name in the first attr=value part */
8732 if (!list)
8733 break;
8734 }
8735
8736 return found;
8737}
8738
8739/* Try to find in request or response message is in <msg> and whose transaction
8740 * is in <txn> the last occurrence of a cookie name in all cookie header values
8741 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8742 * pointer and size of the last occurrence of the cookie value is returned into
8743 * <value> and <value_l>, and the function returns non-zero if the value was
8744 * found. Otherwise if the cookie was not found, zero is returned and neither
8745 * value nor value_l are touched. The input hdr string should begin at the
8746 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8747 * value may represent a list of values (cookie headers).
8748 */
8749
8750static int
8751find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8752 const char *hdr_name, int hdr_name_len,
8753 char *cookie_name, size_t cookie_name_l, int list,
8754 char **value, size_t *value_l)
8755{
8756 struct hdr_ctx ctx;
8757 int found = 0;
8758
8759 ctx.idx = 0;
8760 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
8761 if (ctx.vlen < cookie_name_l + 1)
8762 continue;
8763
8764 found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen,
8765 cookie_name, cookie_name_l, 1,
8766 value, value_l);
8767 }
8768 return found;
8769}
8770
8771static int
8772pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8773 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8774{
8775 struct http_txn *txn = l7;
8776 struct http_msg *msg = &txn->req;
8777 char *cookie_value;
8778 size_t cookie_value_l;
8779 int found = 0;
8780
8781 found = find_cookie_value(msg, txn, "Cookie", 6,
8782 arg_p->data.str.str, arg_p->data.str.len, 1,
8783 &cookie_value, &cookie_value_l);
8784 if (found) {
8785 data->str.str = cookie_value;
8786 data->str.len = cookie_value_l;
8787 }
8788
8789 return found;
8790}
8791
8792
8793static int
8794pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8795 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8796{
8797 struct http_txn *txn = l7;
8798 struct http_msg *msg = &txn->rsp;
8799 char *cookie_value;
8800 size_t cookie_value_l;
8801 int found = 0;
8802
8803 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8804 arg_p->data.str.str, arg_p->data.str.len, 1,
8805 &cookie_value, &cookie_value_l);
8806 if (found) {
8807 data->str.str = cookie_value;
8808 data->str.len = cookie_value_l;
8809 }
8810
8811 return found;
8812}
8813
Emeric Brun485479d2010-09-23 18:02:19 +02008814
Willy Tarreau4a568972010-05-12 08:08:50 +02008815/************************************************************************/
8816/* All supported keywords must be declared here. */
8817/************************************************************************/
8818/* Note: must not be declared <const> as its list will be overwritten */
8819static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008820 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008821 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008822 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8823 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008824 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008825}};
8826
Willy Tarreau8797c062007-05-07 00:55:35 +02008827
8828__attribute__((constructor))
8829static void __http_protocol_init(void)
8830{
8831 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008832 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008833}
8834
8835
Willy Tarreau58f10d72006-12-04 02:26:12 +01008836/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008837 * Local variables:
8838 * c-indent-level: 8
8839 * c-basic-offset: 8
8840 * End:
8841 */