blob: bf91328fe08db902fa3e28d533061ec0664d7846 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/compat.h>
31#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010032#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020033#include <common/memory.h>
34#include <common/mini-clist.h>
35#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020036#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020037#include <common/time.h>
38#include <common/uri_auth.h>
39#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
41#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020042#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
Willy Tarreau8797c062007-05-07 00:55:35 +020044#include <proto/acl.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010045#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020046#include <proto/backend.h>
47#include <proto/buffers.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010048#include <proto/checks.h>
Willy Tarreau91861262007-10-17 17:06:05 +020049#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020051#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010053#include <proto/hdr_idx.h>
Willy Tarreau4a568972010-05-12 08:08:50 +020054#include <proto/pattern.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020055#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010057#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010061#include <proto/stream_interface.h>
Willy Tarreau2d212792008-08-27 21:41:35 +020062#include <proto/stream_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/task.h>
64
Willy Tarreau522d6c02009-12-06 18:49:18 +010065const char HTTP_100[] =
66 "HTTP/1.1 100 Continue\r\n\r\n";
67
68const struct chunk http_100_chunk = {
69 .str = (char *)&HTTP_100,
70 .len = sizeof(HTTP_100)-1
71};
72
Willy Tarreaua9679ac2010-01-03 17:32:57 +010073/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020074const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010075 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Location: "; /* not terminated since it will be concatenated with the URL */
79
Willy Tarreau0f772532006-12-23 20:51:41 +010080const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010081 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010082 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Location: "; /* not terminated since it will be concatenated with the URL */
85
86/* same as 302 except that the browser MUST retry with the GET method */
87const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010088 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010089 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Location: "; /* not terminated since it will be concatenated with the URL */
92
Willy Tarreaubaaee002006-06-26 02:48:02 +020093/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
94const char *HTTP_401_fmt =
95 "HTTP/1.0 401 Unauthorized\r\n"
96 "Cache-Control: no-cache\r\n"
97 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +020098 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +020099 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
100 "\r\n"
101 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
102
Willy Tarreau844a7e72010-01-31 21:46:18 +0100103const char *HTTP_407_fmt =
104 "HTTP/1.0 407 Unauthorized\r\n"
105 "Cache-Control: no-cache\r\n"
106 "Connection: close\r\n"
107 "Content-Type: text/html\r\n"
108 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
109 "\r\n"
110 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
111
Willy Tarreau0f772532006-12-23 20:51:41 +0100112
113const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200114 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100115 [HTTP_ERR_400] = 400,
116 [HTTP_ERR_403] = 403,
117 [HTTP_ERR_408] = 408,
118 [HTTP_ERR_500] = 500,
119 [HTTP_ERR_502] = 502,
120 [HTTP_ERR_503] = 503,
121 [HTTP_ERR_504] = 504,
122};
123
Willy Tarreau80587432006-12-24 17:47:20 +0100124static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200125 [HTTP_ERR_200] =
126 "HTTP/1.0 200 OK\r\n"
127 "Cache-Control: no-cache\r\n"
128 "Connection: close\r\n"
129 "Content-Type: text/html\r\n"
130 "\r\n"
131 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
132
Willy Tarreau0f772532006-12-23 20:51:41 +0100133 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100134 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 "Cache-Control: no-cache\r\n"
136 "Connection: close\r\n"
137 "Content-Type: text/html\r\n"
138 "\r\n"
139 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
140
141 [HTTP_ERR_403] =
142 "HTTP/1.0 403 Forbidden\r\n"
143 "Cache-Control: no-cache\r\n"
144 "Connection: close\r\n"
145 "Content-Type: text/html\r\n"
146 "\r\n"
147 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
148
149 [HTTP_ERR_408] =
150 "HTTP/1.0 408 Request Time-out\r\n"
151 "Cache-Control: no-cache\r\n"
152 "Connection: close\r\n"
153 "Content-Type: text/html\r\n"
154 "\r\n"
155 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
156
157 [HTTP_ERR_500] =
158 "HTTP/1.0 500 Server Error\r\n"
159 "Cache-Control: no-cache\r\n"
160 "Connection: close\r\n"
161 "Content-Type: text/html\r\n"
162 "\r\n"
163 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
164
165 [HTTP_ERR_502] =
166 "HTTP/1.0 502 Bad Gateway\r\n"
167 "Cache-Control: no-cache\r\n"
168 "Connection: close\r\n"
169 "Content-Type: text/html\r\n"
170 "\r\n"
171 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
172
173 [HTTP_ERR_503] =
174 "HTTP/1.0 503 Service Unavailable\r\n"
175 "Cache-Control: no-cache\r\n"
176 "Connection: close\r\n"
177 "Content-Type: text/html\r\n"
178 "\r\n"
179 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
180
181 [HTTP_ERR_504] =
182 "HTTP/1.0 504 Gateway Time-out\r\n"
183 "Cache-Control: no-cache\r\n"
184 "Connection: close\r\n"
185 "Content-Type: text/html\r\n"
186 "\r\n"
187 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
188
189};
190
Willy Tarreau80587432006-12-24 17:47:20 +0100191/* We must put the messages here since GCC cannot initialize consts depending
192 * on strlen().
193 */
194struct chunk http_err_chunks[HTTP_ERR_SIZE];
195
Willy Tarreau42250582007-04-01 01:30:43 +0200196#define FD_SETS_ARE_BITFIELDS
197#ifdef FD_SETS_ARE_BITFIELDS
198/*
199 * This map is used with all the FD_* macros to check whether a particular bit
200 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
201 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
202 * byte should be encoded. Be careful to always pass bytes from 0 to 255
203 * exclusively to the macros.
204 */
205fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
206fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
207
208#else
209#error "Check if your OS uses bitfields for fd_sets"
210#endif
211
Willy Tarreau80587432006-12-24 17:47:20 +0100212void init_proto_http()
213{
Willy Tarreau42250582007-04-01 01:30:43 +0200214 int i;
215 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100216 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200217
Willy Tarreau80587432006-12-24 17:47:20 +0100218 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
219 if (!http_err_msgs[msg]) {
220 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
221 abort();
222 }
223
224 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
225 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
226 }
Willy Tarreau42250582007-04-01 01:30:43 +0200227
228 /* initialize the log header encoding map : '{|}"#' should be encoded with
229 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
230 * URL encoding only requires '"', '#' to be encoded as well as non-
231 * printable characters above.
232 */
233 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
234 memset(url_encode_map, 0, sizeof(url_encode_map));
235 for (i = 0; i < 32; i++) {
236 FD_SET(i, hdr_encode_map);
237 FD_SET(i, url_encode_map);
238 }
239 for (i = 127; i < 256; i++) {
240 FD_SET(i, hdr_encode_map);
241 FD_SET(i, url_encode_map);
242 }
243
244 tmp = "\"#{|}";
245 while (*tmp) {
246 FD_SET(*tmp, hdr_encode_map);
247 tmp++;
248 }
249
250 tmp = "\"#";
251 while (*tmp) {
252 FD_SET(*tmp, url_encode_map);
253 tmp++;
254 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200255
256 /* memory allocations */
257 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200258 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100259}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200260
Willy Tarreau53b6c742006-12-17 13:37:46 +0100261/*
262 * We have 26 list of methods (1 per first letter), each of which can have
263 * up to 3 entries (2 valid, 1 null).
264 */
265struct http_method_desc {
266 http_meth_t meth;
267 int len;
268 const char text[8];
269};
270
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100271const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100272 ['C' - 'A'] = {
273 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
274 },
275 ['D' - 'A'] = {
276 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
277 },
278 ['G' - 'A'] = {
279 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
280 },
281 ['H' - 'A'] = {
282 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
283 },
284 ['P' - 'A'] = {
285 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
286 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
287 },
288 ['T' - 'A'] = {
289 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
290 },
291 /* rest is empty like this :
292 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
293 */
294};
295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100296/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200297 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100298 * RFC2616 for those chars.
299 */
300
301const char http_is_spht[256] = {
302 [' '] = 1, ['\t'] = 1,
303};
304
305const char http_is_crlf[256] = {
306 ['\r'] = 1, ['\n'] = 1,
307};
308
309const char http_is_lws[256] = {
310 [' '] = 1, ['\t'] = 1,
311 ['\r'] = 1, ['\n'] = 1,
312};
313
314const char http_is_sep[256] = {
315 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
316 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
317 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
318 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
319 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
320};
321
322const char http_is_ctl[256] = {
323 [0 ... 31] = 1,
324 [127] = 1,
325};
326
327/*
328 * A token is any ASCII char that is neither a separator nor a CTL char.
329 * Do not overwrite values in assignment since gcc-2.95 will not handle
330 * them correctly. Instead, define every non-CTL char's status.
331 */
332const char http_is_token[256] = {
333 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
334 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
335 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
336 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
337 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
338 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
339 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
340 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
341 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
342 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
343 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
344 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
345 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
346 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
347 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
348 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
349 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
350 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
351 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
352 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
353 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
354 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
355 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
356 ['|'] = 1, ['}'] = 0, ['~'] = 1,
357};
358
359
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100360/*
361 * An http ver_token is any ASCII which can be found in an HTTP version,
362 * which includes 'H', 'T', 'P', '/', '.' and any digit.
363 */
364const char http_is_ver_token[256] = {
365 ['.'] = 1, ['/'] = 1,
366 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
367 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
368 ['H'] = 1, ['P'] = 1, ['T'] = 1,
369};
370
371
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100372/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100373 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
374 */
375#if defined(DEBUG_FSM)
376static void http_silent_debug(int line, struct session *s)
377{
378 int size = 0;
379 size += snprintf(trash + size, sizeof(trash) - size,
380 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n",
381 line,
382 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
383 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags);
384 write(-1, trash, size);
385 size = 0;
386 size += snprintf(trash + size, sizeof(trash) - size,
387 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n",
388 line,
389 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
390 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward);
391
392 write(-1, trash, size);
393}
394#else
395#define http_silent_debug(l,s) do { } while (0)
396#endif
397
398/*
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100399 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
400 * CRLF. Text length is measured first, so it cannot be NULL.
401 * The header is also automatically added to the index <hdr_idx>, and the end
402 * of headers is automatically adjusted. The number of bytes added is returned
403 * on success, otherwise <0 is returned indicating an error.
404 */
405int http_header_add_tail(struct buffer *b, struct http_msg *msg,
406 struct hdr_idx *hdr_idx, const char *text)
407{
408 int bytes, len;
409
410 len = strlen(text);
411 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
412 if (!bytes)
413 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100414 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100415 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
416}
417
418/*
419 * Adds a header and its CRLF at the tail of buffer <b>, just before the last
420 * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
421 * the buffer is only opened and the space reserved, but nothing is copied.
422 * The header is also automatically added to the index <hdr_idx>, and the end
423 * of headers is automatically adjusted. The number of bytes added is returned
424 * on success, otherwise <0 is returned indicating an error.
425 */
426int http_header_add_tail2(struct buffer *b, struct http_msg *msg,
427 struct hdr_idx *hdr_idx, const char *text, int len)
428{
429 int bytes;
430
431 bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len);
432 if (!bytes)
433 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100434 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100435 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
436}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200437
438/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100439 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
440 * If so, returns the position of the first non-space character relative to
441 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
442 * to return a pointer to the place after the first space. Returns 0 if the
443 * header name does not match. Checks are case-insensitive.
444 */
445int http_header_match2(const char *hdr, const char *end,
446 const char *name, int len)
447{
448 const char *val;
449
450 if (hdr + len >= end)
451 return 0;
452 if (hdr[len] != ':')
453 return 0;
454 if (strncasecmp(hdr, name, len) != 0)
455 return 0;
456 val = hdr + len + 1;
457 while (val < end && HTTP_IS_SPHT(*val))
458 val++;
459 if ((val >= end) && (len + 2 <= end - hdr))
460 return len + 2; /* we may replace starting from second space */
461 return val - hdr;
462}
463
Willy Tarreau68085d82010-01-18 14:54:04 +0100464/* Find the end of the header value contained between <s> and <e>. See RFC2616,
465 * par 2.2 for more information. Note that it requires a valid header to return
466 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200467 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100468char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200469{
470 int quoted, qdpair;
471
472 quoted = qdpair = 0;
473 for (; s < e; s++) {
474 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200475 else if (quoted) {
476 if (*s == '\\') qdpair = 1;
477 else if (*s == '"') quoted = 0;
478 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200479 else if (*s == '"') quoted = 1;
480 else if (*s == ',') return s;
481 }
482 return s;
483}
484
485/* Find the first or next occurrence of header <name> in message buffer <sol>
486 * using headers index <idx>, and return it in the <ctx> structure. This
487 * structure holds everything necessary to use the header and find next
488 * occurrence. If its <idx> member is 0, the header is searched from the
489 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100490 * 1 when it finds a value, and 0 when there is no more. It is designed to work
491 * with headers defined as comma-separated lists. As a special case, if ctx->val
492 * is NULL when searching for a new values of a header, the current header is
493 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200494 */
495int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100496 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200497 struct hdr_ctx *ctx)
498{
Willy Tarreau68085d82010-01-18 14:54:04 +0100499 char *eol, *sov;
500 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200501
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 cur_idx = ctx->idx;
503 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200504 /* We have previously returned a value, let's search
505 * another one on the same line.
506 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200507 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200508 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100509 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200510 eol = sol + idx->v[cur_idx].len;
511
512 if (sov >= eol)
513 /* no more values in this header */
514 goto next_hdr;
515
Willy Tarreau68085d82010-01-18 14:54:04 +0100516 /* values remaining for this header, skip the comma but save it
517 * for later use (eg: for header deletion).
518 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 sov++;
520 while (sov < eol && http_is_lws[(unsigned char)*sov])
521 sov++;
522
523 goto return_hdr;
524 }
525
526 /* first request for this header */
527 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100528 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200529 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 while (cur_idx) {
531 eol = sol + idx->v[cur_idx].len;
532
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200533 if (len == 0) {
534 /* No argument was passed, we want any header.
535 * To achieve this, we simply build a fake request. */
536 while (sol + len < eol && sol[len] != ':')
537 len++;
538 name = sol;
539 }
540
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 if ((len < eol - sol) &&
542 (sol[len] == ':') &&
543 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100544 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200545 sov = sol + len + 1;
546 while (sov < eol && http_is_lws[(unsigned char)*sov])
547 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100548
Willy Tarreau33a7e692007-06-10 19:45:56 +0200549 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100550 ctx->prev = old_idx;
551 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 ctx->idx = cur_idx;
553 ctx->val = sov - sol;
554
555 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200556 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200557 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200558 eol--;
559 ctx->tws++;
560 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200561 ctx->vlen = eol - sov;
562 return 1;
563 }
564 next_hdr:
565 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100566 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200567 cur_idx = idx->v[cur_idx].next;
568 }
569 return 0;
570}
571
572int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100573 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200574 struct hdr_ctx *ctx)
575{
576 return http_find_header2(name, strlen(name), sol, idx, ctx);
577}
578
Willy Tarreau68085d82010-01-18 14:54:04 +0100579/* Remove one value of a header. This only works on a <ctx> returned by one of
580 * the http_find_header functions. The value is removed, as well as surrounding
581 * commas if any. If the removed value was alone, the whole header is removed.
582 * The ctx is always updated accordingly, as well as buffer <buf> and HTTP
583 * message <msg>. The new index is returned. If it is zero, it means there is
584 * no more header, so any processing may stop. The ctx is always left in a form
585 * that can be handled by http_find_header2() to find next occurrence.
586 */
587int http_remove_header2(struct http_msg *msg, struct buffer *buf,
588 struct hdr_idx *idx, struct hdr_ctx *ctx)
589{
590 int cur_idx = ctx->idx;
591 char *sol = ctx->line;
592 struct hdr_idx_elem *hdr;
593 int delta, skip_comma;
594
595 if (!cur_idx)
596 return 0;
597
598 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200599 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100600 /* This was the only value of the header, we must now remove it entirely. */
601 delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
602 http_msg_move_end(msg, delta);
603 idx->used--;
604 hdr->len = 0; /* unused entry */
605 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100606 if (idx->tail == ctx->idx)
607 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100608 ctx->idx = ctx->prev; /* walk back to the end of previous header */
609 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
610 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200611 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 return ctx->idx;
613 }
614
615 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200616 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
617 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100618 */
619
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200620 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100621 delta = buffer_replace2(buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200622 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100623 NULL, 0);
624 hdr->len += delta;
625 http_msg_move_end(msg, delta);
626 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200627 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 return ctx->idx;
629}
630
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100631/* This function handles a server error at the stream interface level. The
632 * stream interface is assumed to be already in a closed state. An optional
633 * message is copied into the input buffer, and an HTTP status code stored.
634 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100635 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100637static void http_server_error(struct session *t, struct stream_interface *si,
638 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200639{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100640 buffer_auto_read(si->ob);
641 buffer_abort(si->ob);
642 buffer_auto_close(si->ob);
643 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200644 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100645 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100646 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100647 t->txn.status = status;
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100648 buffer_write(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649 }
650 if (!(t->flags & SN_ERR_MASK))
651 t->flags |= err;
652 if (!(t->flags & SN_FINST_MASK))
653 t->flags |= finst;
654}
655
Willy Tarreau80587432006-12-24 17:47:20 +0100656/* This function returns the appropriate error location for the given session
657 * and message.
658 */
659
660struct chunk *error_message(struct session *s, int msgnum)
661{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200662 if (s->be->errmsg[msgnum].str)
663 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100664 else if (s->fe->errmsg[msgnum].str)
665 return &s->fe->errmsg[msgnum];
666 else
667 return &http_err_chunks[msgnum];
668}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200669
Willy Tarreau53b6c742006-12-17 13:37:46 +0100670/*
671 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
672 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
673 */
674static http_meth_t find_http_meth(const char *str, const int len)
675{
676 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100677 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100678
679 m = ((unsigned)*str - 'A');
680
681 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100682 for (h = http_methods[m]; h->len > 0; h++) {
683 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100684 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100685 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100686 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100687 };
688 return HTTP_METH_OTHER;
689 }
690 return HTTP_METH_NONE;
691
692}
693
Willy Tarreau21d2af32008-02-14 20:25:24 +0100694/* Parse the URI from the given transaction (which is assumed to be in request
695 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
696 * It is returned otherwise.
697 */
698static char *
699http_get_path(struct http_txn *txn)
700{
701 char *ptr, *end;
702
Willy Tarreau962c3f42010-01-10 00:15:35 +0100703 ptr = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100704 end = ptr + txn->req.sl.rq.u_l;
705
706 if (ptr >= end)
707 return NULL;
708
709 /* RFC2616, par. 5.1.2 :
710 * Request-URI = "*" | absuri | abspath | authority
711 */
712
713 if (*ptr == '*')
714 return NULL;
715
716 if (isalpha((unsigned char)*ptr)) {
717 /* this is a scheme as described by RFC3986, par. 3.1 */
718 ptr++;
719 while (ptr < end &&
720 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
721 ptr++;
722 /* skip '://' */
723 if (ptr == end || *ptr++ != ':')
724 return NULL;
725 if (ptr == end || *ptr++ != '/')
726 return NULL;
727 if (ptr == end || *ptr++ != '/')
728 return NULL;
729 }
730 /* skip [user[:passwd]@]host[:[port]] */
731
732 while (ptr < end && *ptr != '/')
733 ptr++;
734
735 if (ptr == end)
736 return NULL;
737
738 /* OK, we got the '/' ! */
739 return ptr;
740}
741
Willy Tarreauefb453c2008-10-26 20:49:47 +0100742/* Returns a 302 for a redirectable request. This may only be called just after
743 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
744 * left unchanged and will follow normal proxy processing.
745 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100746void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100747{
748 struct http_txn *txn;
749 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100750 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100751 char *path;
752 int len;
753
754 /* 1: create the response header */
755 rdr.len = strlen(HTTP_302);
756 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100757 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100758 memcpy(rdr.str, HTTP_302, rdr.len);
759
Willy Tarreau827aee92011-03-10 16:55:02 +0100760 srv = target_srv(&s->target);
761
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100763 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764 return;
765
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100766 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100767 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
768 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
769 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100770 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771
772 /* 3: add the request URI */
773 txn = &s->txn;
774 path = http_get_path(txn);
775 if (!path)
776 return;
777
Willy Tarreau962c3f42010-01-10 00:15:35 +0100778 len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200779 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780 return;
781
782 memcpy(rdr.str + rdr.len, path, len);
783 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100784
785 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
786 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
787 rdr.len += 29;
788 } else {
789 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
790 rdr.len += 23;
791 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792
793 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100794 si->shutr(si);
795 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796 si->err_type = SI_ET_NONE;
797 si->err_loc = NULL;
798 si->state = SI_ST_CLO;
799
800 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100801 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802
803 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100804 if (srv)
805 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100806}
807
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100808/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100809 * that the server side is closed. Note that err_type is actually a
810 * bitmask, where almost only aborts may be cumulated with other
811 * values. We consider that aborted operations are more important
812 * than timeouts or errors due to the fact that nobody else in the
813 * logs might explain incomplete retries. All others should avoid
814 * being cumulated. It should normally not be possible to have multiple
815 * aborts at once, but just in case, the first one in sequence is reported.
816 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100817void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100819 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820
821 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100822 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
823 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100824 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100825 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
826 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100827 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100828 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
829 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100830 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100831 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
832 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100833 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100834 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
835 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100836 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100837 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
838 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100839 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100840 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
841 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100842}
843
Willy Tarreau42250582007-04-01 01:30:43 +0200844extern const char sess_term_cond[8];
845extern const char sess_fin_state[8];
846extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200847struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200848struct pool_head *pool2_capture;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100849
Willy Tarreau117f59e2007-03-04 18:17:17 +0100850/*
851 * Capture headers from message starting at <som> according to header list
852 * <cap_hdr>, and fill the <idx> structure appropriately.
853 */
854void capture_headers(char *som, struct hdr_idx *idx,
855 char **cap, struct cap_hdr *cap_hdr)
856{
857 char *eol, *sol, *col, *sov;
858 int cur_idx;
859 struct cap_hdr *h;
860 int len;
861
862 sol = som + hdr_idx_first_pos(idx);
863 cur_idx = hdr_idx_first_idx(idx);
864
865 while (cur_idx) {
866 eol = sol + idx->v[cur_idx].len;
867
868 col = sol;
869 while (col < eol && *col != ':')
870 col++;
871
872 sov = col + 1;
873 while (sov < eol && http_is_lws[(unsigned char)*sov])
874 sov++;
875
876 for (h = cap_hdr; h; h = h->next) {
877 if ((h->namelen == col - sol) &&
878 (strncasecmp(sol, h->name, h->namelen) == 0)) {
879 if (cap[h->index] == NULL)
880 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200881 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100882
883 if (cap[h->index] == NULL) {
884 Alert("HTTP capture : out of memory.\n");
885 continue;
886 }
887
888 len = eol - sov;
889 if (len > h->len)
890 len = h->len;
891
892 memcpy(cap[h->index], sov, len);
893 cap[h->index][len]=0;
894 }
895 }
896 sol = eol + idx->v[cur_idx].cr + 1;
897 cur_idx = idx->v[cur_idx].next;
898 }
899}
900
901
Willy Tarreau42250582007-04-01 01:30:43 +0200902/* either we find an LF at <ptr> or we jump to <bad>.
903 */
904#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
905
906/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
907 * otherwise to <http_msg_ood> with <state> set to <st>.
908 */
909#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
910 ptr++; \
911 if (likely(ptr < end)) \
912 goto good; \
913 else { \
914 state = (st); \
915 goto http_msg_ood; \
916 } \
917 } while (0)
918
919
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100921 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100922 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
923 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
924 * will give undefined results.
925 * Note that it is upon the caller's responsibility to ensure that ptr < end,
926 * and that msg->sol points to the beginning of the response.
927 * If a complete line is found (which implies that at least one CR or LF is
928 * found before <end>, the updated <ptr> is returned, otherwise NULL is
929 * returned indicating an incomplete line (which does not mean that parts have
930 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
931 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
932 * upon next call.
933 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200934 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100935 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
936 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200937 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100938 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100939const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
940 unsigned int state, const char *ptr, const char *end,
941 char **ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100942{
Willy Tarreau8973c702007-01-21 23:58:29 +0100943 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100944 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200945 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100946 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100947 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
948
949 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +0100950 msg->sl.st.v_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100951 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
952 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100953 state = HTTP_MSG_ERROR;
954 break;
955
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200957 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100959 msg->sl.st.c = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100960 goto http_msg_rpcode;
961 }
962 if (likely(HTTP_IS_SPHT(*ptr)))
963 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
964 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100965 state = HTTP_MSG_ERROR;
966 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100967
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200969 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 if (likely(!HTTP_IS_LWS(*ptr)))
971 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
972
973 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100974 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
976 }
977
978 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreau962c3f42010-01-10 00:15:35 +0100979 msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100980 http_msg_rsp_reason:
981 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreau962c3f42010-01-10 00:15:35 +0100982 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100983 msg->sl.st.r_l = 0;
984 goto http_msg_rpline_eol;
985
Willy Tarreau8973c702007-01-21 23:58:29 +0100986 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200987 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100988 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +0100989 msg->sl.st.r = (ptr - msg_buf) - msg->som;
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 goto http_msg_rpreason;
991 }
992 if (likely(HTTP_IS_SPHT(*ptr)))
993 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
994 /* so it's a CR/LF, so there is no reason phrase */
995 goto http_msg_rsp_reason;
996
Willy Tarreau8973c702007-01-21 23:58:29 +0100997 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200998 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +0100999 if (likely(!HTTP_IS_CRLF(*ptr)))
1000 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreau962c3f42010-01-10 00:15:35 +01001001 msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 http_msg_rpline_eol:
1003 /* We have seen the end of line. Note that we do not
1004 * necessarily have the \n yet, but at least we know that we
1005 * have EITHER \r OR \n, otherwise the response would not be
1006 * complete. We can then record the response length and return
1007 * to the caller which will be able to register it.
1008 */
1009 msg->sl.st.l = ptr - msg->sol;
1010 return ptr;
1011
1012#ifdef DEBUG_FULL
1013 default:
1014 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1015 exit(1);
1016#endif
1017 }
1018
1019 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001020 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001021 if (ret_state)
1022 *ret_state = state;
1023 if (ret_ptr)
1024 *ret_ptr = (char *)ptr;
1025 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001026}
1027
Willy Tarreau8973c702007-01-21 23:58:29 +01001028/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001029 * This function parses a request line between <ptr> and <end>, starting with
1030 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1031 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1032 * will give undefined results.
1033 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1034 * and that msg->sol points to the beginning of the request.
1035 * If a complete line is found (which implies that at least one CR or LF is
1036 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1037 * returned indicating an incomplete line (which does not mean that parts have
1038 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1039 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1040 * upon next call.
1041 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001042 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1044 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001045 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001046 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001047const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1048 unsigned int state, const char *ptr, const char *end,
1049 char **ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001050{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001051 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001052 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001053 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001054 if (likely(HTTP_IS_TOKEN(*ptr)))
1055 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001056
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001057 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001058 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001059 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1060 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001061
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001062 if (likely(HTTP_IS_CRLF(*ptr))) {
1063 /* HTTP 0.9 request */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001064 msg->sl.rq.m_l = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001065 http_msg_req09_uri:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001066 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 http_msg_req09_uri_e:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001068 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001069 http_msg_req09_ver:
Willy Tarreau962c3f42010-01-10 00:15:35 +01001070 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001071 msg->sl.rq.v_l = 0;
1072 goto http_msg_rqline_eol;
1073 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001074 state = HTTP_MSG_ERROR;
1075 break;
1076
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001078 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001080 msg->sl.rq.u = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 goto http_msg_rquri;
1082 }
1083 if (likely(HTTP_IS_SPHT(*ptr)))
1084 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1085 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1086 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001087
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001089 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001090 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001094 msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1096 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001097
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001098 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001099 /* non-ASCII chars are forbidden unless option
1100 * accept-invalid-http-request is enabled in the frontend.
1101 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001102 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001103 if (msg->err_pos < -1)
1104 goto invalid_char;
1105 if (msg->err_pos == -1)
1106 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001107 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1108 }
1109
1110 if (likely(HTTP_IS_CRLF(*ptr))) {
1111 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1112 goto http_msg_req09_uri_e;
1113 }
1114
1115 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001116 invalid_char:
1117 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001118 state = HTTP_MSG_ERROR;
1119 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001120
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001121 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001122 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001123 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001124 msg->sl.rq.v = (ptr - msg_buf) - msg->som;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001125 goto http_msg_rqver;
1126 }
1127 if (likely(HTTP_IS_SPHT(*ptr)))
1128 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1129 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1130 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001132 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001133 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001134 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001135 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001136
1137 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01001138 msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001139 http_msg_rqline_eol:
1140 /* We have seen the end of line. Note that we do not
1141 * necessarily have the \n yet, but at least we know that we
1142 * have EITHER \r OR \n, otherwise the request would not be
1143 * complete. We can then record the request length and return
1144 * to the caller which will be able to register it.
1145 */
1146 msg->sl.rq.l = ptr - msg->sol;
1147 return ptr;
1148 }
1149
1150 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001151 state = HTTP_MSG_ERROR;
1152 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001153
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001154#ifdef DEBUG_FULL
1155 default:
1156 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1157 exit(1);
1158#endif
1159 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001160
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001162 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001163 if (ret_state)
1164 *ret_state = state;
1165 if (ret_ptr)
1166 *ret_ptr = (char *)ptr;
1167 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001169
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001170/*
1171 * Returns the data from Authorization header. Function may be called more
1172 * than once so data is stored in txn->auth_data. When no header is found
1173 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1174 * searching again for something we are unable to find anyway.
1175 */
1176
1177char get_http_auth_buff[BUFSIZE];
1178
1179int
1180get_http_auth(struct session *s)
1181{
1182
1183 struct http_txn *txn = &s->txn;
1184 struct chunk auth_method;
1185 struct hdr_ctx ctx;
1186 char *h, *p;
1187 int len;
1188
1189#ifdef DEBUG_AUTH
1190 printf("Auth for session %p: %d\n", s, txn->auth.method);
1191#endif
1192
1193 if (txn->auth.method == HTTP_AUTH_WRONG)
1194 return 0;
1195
1196 if (txn->auth.method)
1197 return 1;
1198
1199 txn->auth.method = HTTP_AUTH_WRONG;
1200
1201 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001202
1203 if (txn->flags & TX_USE_PX_CONN) {
1204 h = "Proxy-Authorization";
1205 len = strlen(h);
1206 } else {
1207 h = "Authorization";
1208 len = strlen(h);
1209 }
1210
1211 if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001212 return 0;
1213
1214 h = ctx.line + ctx.val;
1215
1216 p = memchr(h, ' ', ctx.vlen);
1217 if (!p || p == h)
1218 return 0;
1219
1220 chunk_initlen(&auth_method, h, 0, p-h);
1221 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1222
1223 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1224
1225 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1226 get_http_auth_buff, BUFSIZE - 1);
1227
1228 if (len < 0)
1229 return 0;
1230
1231
1232 get_http_auth_buff[len] = '\0';
1233
1234 p = strchr(get_http_auth_buff, ':');
1235
1236 if (!p)
1237 return 0;
1238
1239 txn->auth.user = get_http_auth_buff;
1240 *p = '\0';
1241 txn->auth.pass = p+1;
1242
1243 txn->auth.method = HTTP_AUTH_BASIC;
1244 return 1;
1245 }
1246
1247 return 0;
1248}
1249
Willy Tarreau58f10d72006-12-04 02:26:12 +01001250
Willy Tarreau8973c702007-01-21 23:58:29 +01001251/*
1252 * This function parses an HTTP message, either a request or a response,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001253 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001254 * when data are missing and recalled at the exact same location with no
1255 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001256 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001257 * fields. Note that msg->som and msg->sol will be initialized after completing
1258 * the first state, so that none of the msg pointers has to be initialized
1259 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001260 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001261void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx)
1262{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001263 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001264 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreau58f10d72006-12-04 02:26:12 +01001265
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001266 state = msg->msg_state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001267 ptr = buf->lr;
1268 end = buf->r;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001269
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001270 if (unlikely(ptr >= end))
1271 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001273 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001274 /*
1275 * First, states that are specific to the response only.
1276 * We check them first so that request and headers are
1277 * closer to each other (accessed more often).
1278 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001279 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001280 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001281 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001282 /* we have a start of message, but we have to check
1283 * first if we need to remove some CRLF. We can only
1284 * do this when send_max=0.
1285 */
1286 char *beg = buf->w + buf->send_max;
1287 if (beg >= buf->data + buf->size)
1288 beg -= buf->size;
1289 if (unlikely(ptr != beg)) {
1290 if (buf->send_max)
1291 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001292 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001293 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001294 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001295 msg->som = ptr - buf->data;
Willy Tarreau816b9792009-09-15 21:25:21 +02001296 msg->sol = ptr;
Willy Tarreau8973c702007-01-21 23:58:29 +01001297 hdr_idx_init(idx);
1298 state = HTTP_MSG_RPVER;
1299 goto http_msg_rpver;
1300 }
1301
1302 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1303 goto http_msg_invalid;
1304
1305 if (unlikely(*ptr == '\n'))
1306 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1307 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1308 /* stop here */
1309
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001311 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 EXPECT_LF_HERE(ptr, http_msg_invalid);
1313 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1314 /* stop here */
1315
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001317 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001318 case HTTP_MSG_RPVER_SP:
1319 case HTTP_MSG_RPCODE:
1320 case HTTP_MSG_RPCODE_SP:
1321 case HTTP_MSG_RPREASON:
Willy Tarreaua15645d2007-03-18 16:22:39 +01001322 ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001323 &buf->lr, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001324 if (unlikely(!ptr))
1325 return;
1326
1327 /* we have a full response and we know that we have either a CR
1328 * or an LF at <ptr>.
1329 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001330 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr);
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1332
1333 msg->sol = ptr;
1334 if (likely(*ptr == '\r'))
1335 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1336 goto http_msg_rpline_end;
1337
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001339 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 /* msg->sol must point to the first of CR or LF. */
1341 EXPECT_LF_HERE(ptr, http_msg_invalid);
1342 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1343 /* stop here */
1344
1345 /*
1346 * Second, states that are specific to the request only
1347 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001348 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001349 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001350 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001351 /* we have a start of message, but we have to check
1352 * first if we need to remove some CRLF. We can only
1353 * do this when send_max=0.
1354 */
1355 char *beg = buf->w + buf->send_max;
1356 if (beg >= buf->data + buf->size)
1357 beg -= buf->size;
1358 if (likely(ptr != beg)) {
1359 if (buf->send_max)
1360 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001361 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001362 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 }
Willy Tarreau15de77e2010-01-02 21:59:16 +01001364 msg->som = ptr - buf->data;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001365 msg->sol = ptr;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001366 /* we will need this when keep-alive will be supported
1367 hdr_idx_init(idx);
1368 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001369 state = HTTP_MSG_RQMETH;
1370 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001372
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1374 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001375
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 if (unlikely(*ptr == '\n'))
1377 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1378 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001379 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001380
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001381 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001382 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 EXPECT_LF_HERE(ptr, http_msg_invalid);
1384 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001386
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001388 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 case HTTP_MSG_RQMETH_SP:
1390 case HTTP_MSG_RQURI:
1391 case HTTP_MSG_RQURI_SP:
1392 case HTTP_MSG_RQVER:
1393 ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end,
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001394 &buf->lr, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 if (unlikely(!ptr))
1396 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 /* we have a full request and we know that we have either a CR
1399 * or an LF at <ptr>.
1400 */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001401 //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 msg->sol = ptr;
1405 if (likely(*ptr == '\r'))
1406 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001410 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 /* check for HTTP/0.9 request : no version information available.
1412 * msg->sol must point to the first of CR or LF.
1413 */
1414 if (unlikely(msg->sl.rq.v_l == 0))
1415 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001416
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 EXPECT_LF_HERE(ptr, http_msg_invalid);
1418 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001419 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8973c702007-01-21 23:58:29 +01001421 /*
1422 * Common states below
1423 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001425 http_msg_hdr_first:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 msg->sol = ptr;
1427 if (likely(!HTTP_IS_CRLF(*ptr))) {
1428 goto http_msg_hdr_name;
1429 }
1430
1431 if (likely(*ptr == '\r'))
1432 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1433 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001435 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001436 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 /* assumes msg->sol points to the first char */
1438 if (likely(HTTP_IS_TOKEN(*ptr)))
1439 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001440
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 if (likely(*ptr == ':')) {
1442 msg->col = ptr - buf->data;
1443 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
1444 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001445
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001446 if (likely(msg->err_pos < -1) || *ptr == '\n')
1447 goto http_msg_invalid;
1448
1449 if (msg->err_pos == -1) /* capture error pointer */
1450 msg->err_pos = ptr - buf->data; /* >= 0 now */
1451
1452 /* and we still accept this non-token character */
1453 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_hdr_l1_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 /* assumes msg->sol points to the first char and msg->col to the colon */
1458 if (likely(HTTP_IS_SPHT(*ptr)))
1459 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001461 /* header value can be basically anything except CR/LF */
1462 msg->sov = ptr - buf->data;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001463
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 if (likely(!HTTP_IS_CRLF(*ptr))) {
1465 goto http_msg_hdr_val;
1466 }
1467
1468 if (likely(*ptr == '\r'))
1469 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1470 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001473 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 EXPECT_LF_HERE(ptr, http_msg_invalid);
1475 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001478 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 if (likely(HTTP_IS_SPHT(*ptr))) {
1480 /* replace HT,CR,LF with spaces */
1481 for (; buf->data+msg->sov < ptr; msg->sov++)
1482 buf->data[msg->sov] = ' ';
1483 goto http_msg_hdr_l1_sp;
1484 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001485 /* we had a header consisting only in spaces ! */
1486 msg->eol = buf->data + msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 goto http_msg_complete_header;
1488
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001490 http_msg_hdr_val:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 /* assumes msg->sol points to the first char, msg->col to the
1492 * colon, and msg->sov points to the first character of the
1493 * value.
1494 */
1495 if (likely(!HTTP_IS_CRLF(*ptr)))
1496 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 msg->eol = ptr;
1499 /* Note: we could also copy eol into ->eoh so that we have the
1500 * real header end in case it ends with lots of LWS, but is this
1501 * really needed ?
1502 */
1503 if (likely(*ptr == '\r'))
1504 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1505 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001506
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001508 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 EXPECT_LF_HERE(ptr, http_msg_invalid);
1510 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001511
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001513 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001514 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1515 /* LWS: replace HT,CR,LF with spaces */
1516 for (; msg->eol < ptr; msg->eol++)
1517 *msg->eol = ' ';
1518 goto http_msg_hdr_val;
1519 }
1520 http_msg_complete_header:
1521 /*
1522 * It was a new header, so the last one is finished.
1523 * Assumes msg->sol points to the first char, msg->col to the
1524 * colon, msg->sov points to the first character of the value
1525 * and msg->eol to the first CR or LF so we know how the line
1526 * ends. We insert last header into the index.
1527 */
1528 /*
1529 fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol);
1530 write(2, msg->sol, msg->eol-msg->sol);
1531 fprintf(stderr,"\n");
1532 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001534 if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r',
1535 idx, idx->tail) < 0))
1536 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001537
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001538 msg->sol = ptr;
1539 if (likely(!HTTP_IS_CRLF(*ptr))) {
1540 goto http_msg_hdr_name;
1541 }
1542
1543 if (likely(*ptr == '\r'))
1544 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1545 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001546
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001548 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001549 /* Assumes msg->sol points to the first of either CR or LF */
1550 EXPECT_LF_HERE(ptr, http_msg_invalid);
1551 ptr++;
1552 buf->lr = ptr;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001553 msg->col = msg->sov = buf->lr - buf->data;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 msg->eoh = msg->sol - buf->data;
Willy Tarreau962c3f42010-01-10 00:15:35 +01001555 msg->sol = buf->data + msg->som;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001556 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 return;
1558#ifdef DEBUG_FULL
1559 default:
1560 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1561 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001562#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 }
1564 http_msg_ood:
1565 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001566 msg->msg_state = state;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001567 buf->lr = ptr;
1568 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570 http_msg_invalid:
1571 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001572 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreau7552c032009-03-01 11:10:40 +01001573 buf->lr = ptr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 return;
1575}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001576
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001577/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1578 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1579 * nothing is done and 1 is returned.
1580 */
1581static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1582{
1583 int delta;
1584 char *cur_end;
1585
1586 if (msg->sl.rq.v_l != 0)
1587 return 1;
1588
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001589 cur_end = msg->sol + msg->sl.rq.l;
1590 delta = 0;
1591
1592 if (msg->sl.rq.u_l == 0) {
1593 /* if no URI was set, add "/" */
1594 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1595 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001596 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597 }
1598 /* add HTTP version */
1599 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001600 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001601 cur_end += delta;
1602 cur_end = (char *)http_parse_reqline(msg, req->data,
1603 HTTP_MSG_RQMETH,
1604 msg->sol, cur_end + 1,
1605 NULL, NULL);
1606 if (unlikely(!cur_end))
1607 return 0;
1608
1609 /* we have a full HTTP/1.0 request now and we know that
1610 * we have either a CR or an LF at <ptr>.
1611 */
1612 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1613 return 1;
1614}
1615
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001616/* Parse the Connection: header of an HTTP request, looking for both "close"
1617 * and "keep-alive" values. If a buffer is provided and we already know that
1618 * some headers may safely be removed, we remove them now. The <to_del> flags
1619 * are used for that :
1620 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1621 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1622 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1623 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1624 * harmless combinations may be removed. Do not call that after changes have
1625 * been processed. If unused, the buffer can be NULL, and no data will be
1626 * changed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001627 */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001629{
Willy Tarreau5b154472009-12-21 20:11:07 +01001630 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001631 const char *hdr_val = "Connection";
1632 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001633
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001634 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001635 return;
1636
Willy Tarreau88d349d2010-01-25 12:15:43 +01001637 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1638 hdr_val = "Proxy-Connection";
1639 hdr_len = 16;
1640 }
1641
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001643 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001644 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001645 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1646 txn->flags |= TX_HDR_CONN_KAL;
1647 if ((to_del & 2) && buf)
1648 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1649 else
1650 txn->flags |= TX_CON_KAL_SET;
1651 }
1652 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1653 txn->flags |= TX_HDR_CONN_CLO;
1654 if ((to_del & 1) && buf)
1655 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
1656 else
1657 txn->flags |= TX_CON_CLO_SET;
1658 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001659 }
1660
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001661 txn->flags |= TX_HDR_CONN_PRS;
1662 return;
1663}
Willy Tarreau5b154472009-12-21 20:11:07 +01001664
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001665/* Apply desired changes on the Connection: header. Values may be removed and/or
1666 * added depending on the <wanted> flags, which are exclusively composed of
1667 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1668 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1669 */
1670void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted)
1671{
1672 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001673 const char *hdr_val = "Connection";
1674 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675
1676 ctx.idx = 0;
1677
Willy Tarreau88d349d2010-01-25 12:15:43 +01001678
1679 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1680 hdr_val = "Proxy-Connection";
1681 hdr_len = 16;
1682 }
1683
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001684 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau88d349d2010-01-25 12:15:43 +01001685 while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001686 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1687 if (wanted & TX_CON_KAL_SET)
1688 txn->flags |= TX_CON_KAL_SET;
1689 else
1690 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001691 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001692 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1693 if (wanted & TX_CON_CLO_SET)
1694 txn->flags |= TX_CON_CLO_SET;
1695 else
1696 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001697 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001698 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699
1700 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1701 return;
1702
1703 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1704 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001705 hdr_val = "Connection: close";
1706 hdr_len = 17;
1707 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1708 hdr_val = "Proxy-Connection: close";
1709 hdr_len = 23;
1710 }
1711 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001712 }
1713
1714 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1715 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001716 hdr_val = "Connection: keep-alive";
1717 hdr_len = 22;
1718 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1719 hdr_val = "Proxy-Connection: keep-alive";
1720 hdr_len = 28;
1721 }
1722 http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001723 }
1724 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001725}
1726
Willy Tarreaud98cf932009-12-27 22:54:55 +01001727/* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the
1728 * first byte of body, and increments msg->sov by the number of bytes parsed,
1729 * so that we know we can forward between ->som and ->sov. Note that due to
1730 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1731 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001732 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001733 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001734 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001735int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001736{
Willy Tarreaud98cf932009-12-27 22:54:55 +01001737 char *ptr = buf->lr;
1738 char *end = buf->data + buf->size;
Willy Tarreau115acb92009-12-26 13:56:06 +01001739 unsigned int chunk = 0;
1740
1741 /* The chunk size is in the following form, though we are only
1742 * interested in the size and CRLF :
1743 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1744 */
1745 while (1) {
1746 int c;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001747 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001748 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001749 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001750 if (c < 0) /* not a hex digit anymore */
1751 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001752 if (++ptr >= end)
1753 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001754 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001755 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001756 chunk = (chunk << 4) + c;
1757 }
1758
Willy Tarreaud98cf932009-12-27 22:54:55 +01001759 /* empty size not allowed */
1760 if (ptr == buf->lr)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001761 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001762
1763 while (http_is_spht[(unsigned char)*ptr]) {
1764 if (++ptr >= end)
1765 ptr = buf->data;
1766 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001767 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001768 }
1769
Willy Tarreaud98cf932009-12-27 22:54:55 +01001770 /* Up to there, we know that at least one byte is present at *ptr. Check
1771 * for the end of chunk size.
1772 */
1773 while (1) {
1774 if (likely(HTTP_IS_CRLF(*ptr))) {
1775 /* we now have a CR or an LF at ptr */
1776 if (likely(*ptr == '\r')) {
1777 if (++ptr >= end)
1778 ptr = buf->data;
1779 if (ptr == buf->r)
1780 return 0;
1781 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001782
Willy Tarreaud98cf932009-12-27 22:54:55 +01001783 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001784 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001785 if (++ptr >= end)
1786 ptr = buf->data;
1787 /* done */
1788 break;
1789 }
1790 else if (*ptr == ';') {
1791 /* chunk extension, ends at next CRLF */
1792 if (++ptr >= end)
1793 ptr = buf->data;
1794 if (ptr == buf->r)
Willy Tarreau115acb92009-12-26 13:56:06 +01001795 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001796
1797 while (!HTTP_IS_CRLF(*ptr)) {
1798 if (++ptr >= end)
1799 ptr = buf->data;
1800 if (ptr == buf->r)
1801 return 0;
1802 }
1803 /* we have a CRLF now, loop above */
1804 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001805 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001806 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001807 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001808 }
1809
Willy Tarreaud98cf932009-12-27 22:54:55 +01001810 /* OK we found our CRLF and now <ptr> points to the next byte,
1811 * which may or may not be present. We save that into ->lr and
1812 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001813 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001814 msg->sov += ptr - buf->lr;
1815 buf->lr = ptr;
Willy Tarreau124d9912011-03-01 20:30:48 +01001816 msg->chunk_len = chunk;
1817 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001818 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001819 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001820 error:
1821 msg->err_pos = ptr - buf->data;
1822 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001823}
1824
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825/* This function skips trailers in the buffer <buf> associated with HTTP
1826 * message <msg>. The first visited position is buf->lr. If the end of
1827 * the trailers is found, it is automatically scheduled to be forwarded,
1828 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1829 * If not enough data are available, the function does not change anything
Willy Tarreau638cd022010-01-03 07:42:04 +01001830 * except maybe buf->lr and msg->sov if it could parse some lines, and returns
1831 * zero. If a parse error is encountered, the function returns < 0 and does not
1832 * change anything except maybe buf->lr and msg->sov. Note that the message
1833 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1834 * which implies that all non-trailers data have already been scheduled for
1835 * forwarding, and that the difference between msg->som and msg->sov exactly
1836 * matches the length of trailers already parsed and not forwarded. It is also
1837 * important to note that this function is designed to be able to parse wrapped
1838 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001839 */
1840int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1841{
1842 /* we have buf->lr which points to next line. Look for CRLF. */
1843 while (1) {
1844 char *p1 = NULL, *p2 = NULL;
1845 char *ptr = buf->lr;
Willy Tarreau638cd022010-01-03 07:42:04 +01001846 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001847
1848 /* scan current line and stop at LF or CRLF */
1849 while (1) {
1850 if (ptr == buf->r)
1851 return 0;
1852
1853 if (*ptr == '\n') {
1854 if (!p1)
1855 p1 = ptr;
1856 p2 = ptr;
1857 break;
1858 }
1859
1860 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001861 if (p1) {
1862 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001864 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001865 p1 = ptr;
1866 }
1867
1868 ptr++;
1869 if (ptr >= buf->data + buf->size)
1870 ptr = buf->data;
1871 }
1872
1873 /* after LF; point to beginning of next line */
1874 p2++;
1875 if (p2 >= buf->data + buf->size)
1876 p2 = buf->data;
1877
Willy Tarreau638cd022010-01-03 07:42:04 +01001878 bytes = p2 - buf->lr;
1879 if (bytes < 0)
1880 bytes += buf->size;
1881
1882 /* schedule this line for forwarding */
1883 msg->sov += bytes;
1884 if (msg->sov >= buf->size)
1885 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886
Willy Tarreau638cd022010-01-03 07:42:04 +01001887 if (p1 == buf->lr) {
1888 /* LF/CRLF at beginning of line => end of trailers at p2.
1889 * Everything was scheduled for forwarding, there's nothing
1890 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001891 */
Willy Tarreau638cd022010-01-03 07:42:04 +01001892 buf->lr = p2;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001893 msg->msg_state = HTTP_MSG_DONE;
1894 return 1;
1895 }
1896 /* OK, next line then */
1897 buf->lr = p2;
1898 }
1899}
1900
1901/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1902 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
1903 * ->som, buf->lr in order to include this part into the next forwarding phase.
1904 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1905 * not enough data are available, the function does not change anything and
1906 * returns zero. If a parse error is encountered, the function returns < 0 and
1907 * does not change anything. Note: this function is designed to parse wrapped
1908 * CRLF at the end of the buffer.
1909 */
1910int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1911{
1912 char *ptr;
1913 int bytes;
1914
1915 /* NB: we'll check data availabilty at the end. It's not a
1916 * problem because whatever we match first will be checked
1917 * against the correct length.
1918 */
1919 bytes = 1;
1920 ptr = buf->lr;
1921 if (*ptr == '\r') {
1922 bytes++;
1923 ptr++;
1924 if (ptr >= buf->data + buf->size)
1925 ptr = buf->data;
1926 }
1927
Willy Tarreaubf3f1de2010-03-17 15:54:24 +01001928 if (bytes > buf->l - buf->send_max)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001929 return 0;
1930
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001931 if (*ptr != '\n') {
1932 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001934 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001935
1936 ptr++;
1937 if (ptr >= buf->data + buf->size)
1938 ptr = buf->data;
1939 buf->lr = ptr;
1940 /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */
1941 msg->sov = ptr - buf->data;
1942 msg->som = msg->sov - bytes;
1943 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1944 return 1;
1945}
Willy Tarreau5b154472009-12-21 20:11:07 +01001946
Willy Tarreau83e3af02009-12-28 17:39:57 +01001947void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1948{
1949 char *end = buf->data + buf->size;
1950 int off = buf->data + buf->size - buf->w;
1951
1952 /* two possible cases :
1953 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001954 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001955 */
1956 if (buf->l) {
Willy Tarreau8096de92010-02-26 11:12:27 +01001957 int block1 = buf->l;
1958 int block2 = 0;
1959 if (buf->r <= buf->w) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001960 /* non-contiguous block */
Willy Tarreau8096de92010-02-26 11:12:27 +01001961 block1 = buf->data + buf->size - buf->w;
1962 block2 = buf->r - buf->data;
1963 }
1964 if (block2)
1965 memcpy(swap_buffer, buf->data, block2);
1966 memmove(buf->data, buf->w, block1);
1967 if (block2)
1968 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001969 }
1970
1971 /* adjust all known pointers */
1972 buf->w = buf->data;
1973 buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size;
1974 buf->r += off; if (buf->r >= end) buf->r -= buf->size;
1975 msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size;
1976 msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size;
1977
1978 /* adjust relative pointers */
1979 msg->som = 0;
1980 msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size;
1981 msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size;
1982 msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size;
1983
Willy Tarreau83e3af02009-12-28 17:39:57 +01001984 if (msg->err_pos >= 0) {
1985 msg->err_pos += off;
1986 if (msg->err_pos >= buf->size)
1987 msg->err_pos -= buf->size;
1988 }
1989
1990 buf->flags &= ~BF_FULL;
1991 if (buf->l >= buffer_max_len(buf))
1992 buf->flags |= BF_FULL;
1993}
1994
Willy Tarreaud787e662009-07-07 10:14:51 +02001995/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1996 * processing can continue on next analysers, or zero if it either needs more
1997 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1998 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1999 * when it has nothing left to do, and may remove any analyser when it wants to
2000 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002001 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002002int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002003{
Willy Tarreau59234e92008-11-30 23:51:27 +01002004 /*
2005 * We will parse the partial (or complete) lines.
2006 * We will check the request syntax, and also join multi-line
2007 * headers. An index of all the lines will be elaborated while
2008 * parsing.
2009 *
2010 * For the parsing, we use a 28 states FSM.
2011 *
2012 * Here is the information we currently have :
Willy Tarreauf073a832009-03-01 23:21:47 +01002013 * req->data + msg->som = beginning of request
Willy Tarreau83e3af02009-12-28 17:39:57 +01002014 * req->data + msg->eoh = end of processed headers / start of current one
2015 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreau59234e92008-11-30 23:51:27 +01002016 * req->lr = first non-visited byte
2017 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002018 *
2019 * At end of parsing, we may perform a capture of the error (if any), and
2020 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2021 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2022 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002023 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002024
Willy Tarreau59234e92008-11-30 23:51:27 +01002025 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002026 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002027 struct http_txn *txn = &s->txn;
2028 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002029 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002030
Willy Tarreau6bf17362009-02-24 10:48:35 +01002031 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2032 now_ms, __FUNCTION__,
2033 s,
2034 req,
2035 req->rex, req->wex,
2036 req->flags,
2037 req->l,
2038 req->analysers);
2039
Willy Tarreau52a0c602009-08-16 22:45:38 +02002040 /* we're speaking HTTP here, so let's speak HTTP to the client */
2041 s->srv_error = http_return_srv_error;
2042
Willy Tarreau83e3af02009-12-28 17:39:57 +01002043 /* There's a protected area at the end of the buffer for rewriting
2044 * purposes. We don't want to start to parse the request if the
2045 * protected area is affected, because we may have to move processed
2046 * data later, which is much more complicated.
2047 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002048 if (req->l && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002049 if ((txn->flags & TX_NOT_FIRST) &&
2050 unlikely((req->flags & BF_FULL) ||
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002051 req->r < req->lr ||
2052 req->r > req->data + req->size - global.tune.maxrewrite)) {
2053 if (req->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002054 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2055 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002056 /* some data has still not left the buffer, wake us once that's done */
2057 buffer_dont_connect(req);
2058 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2059 return 0;
2060 }
Willy Tarreau0499e352010-12-17 07:13:42 +01002061 if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002062 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002063 }
2064
Willy Tarreau065e8332010-01-08 00:30:20 +01002065 /* Note that we have the same problem with the response ; we
2066 * may want to send a redirect, error or anything which requires
2067 * some spare space. So we'll ensure that we have at least
2068 * maxrewrite bytes available in the response buffer before
2069 * processing that one. This will only affect pipelined
2070 * keep-alive requests.
2071 */
2072 if ((txn->flags & TX_NOT_FIRST) &&
2073 unlikely((s->rep->flags & BF_FULL) ||
2074 s->rep->r < s->rep->lr ||
2075 s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
2076 if (s->rep->send_max) {
Willy Tarreau64648412010-03-05 10:41:54 +01002077 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2078 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002079 /* don't let a connection request be initiated */
2080 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002081 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002082 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002083 return 0;
2084 }
2085 }
2086
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002087 if (likely(req->lr < req->r))
2088 http_msg_analyzer(req, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002089 }
2090
Willy Tarreau59234e92008-11-30 23:51:27 +01002091 /* 1: we might have to print this header in debug mode */
2092 if (unlikely((global.mode & MODE_DEBUG) &&
2093 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002094 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002095 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002096 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002097
Willy Tarreau663308b2010-06-07 14:06:08 +02002098 sol = req->data + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002099 eol = sol + msg->sl.rq.l;
2100 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002101
Willy Tarreau59234e92008-11-30 23:51:27 +01002102 sol += hdr_idx_first_pos(&txn->hdr_idx);
2103 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002104
Willy Tarreau59234e92008-11-30 23:51:27 +01002105 while (cur_idx) {
2106 eol = sol + txn->hdr_idx.v[cur_idx].len;
2107 debug_hdr("clihdr", s, sol, eol);
2108 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2109 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002110 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002111 }
2112
Willy Tarreau58f10d72006-12-04 02:26:12 +01002113
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 /*
2115 * Now we quickly check if we have found a full valid request.
2116 * If not so, we check the FD and buffer states before leaving.
2117 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002118 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002119 * requests are checked first. When waiting for a second request
2120 * on a keep-alive session, if we encounter and error, close, t/o,
2121 * we note the error in the session flags but don't set any state.
2122 * Since the error will be noted there, it will not be counted by
2123 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002124 * Last, we may increase some tracked counters' http request errors on
2125 * the cases that are deliberately the client's fault. For instance,
2126 * a timeout or connection reset is not counted as an error. However
2127 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002128 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002129
Willy Tarreau655dce92009-11-08 13:10:58 +01002130 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002131 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002132 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002133 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002134 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002135 session_inc_http_req_ctr(s);
2136 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002137 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002138 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002139 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002140
Willy Tarreau59234e92008-11-30 23:51:27 +01002141 /* 1: Since we are in header mode, if there's no space
2142 * left for headers, we won't be able to free more
2143 * later, so the session will never terminate. We
2144 * must terminate it now.
2145 */
2146 if (unlikely(req->flags & BF_FULL)) {
2147 /* FIXME: check if URI is set and return Status
2148 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002149 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002150 session_inc_http_req_ctr(s);
2151 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002152 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002153 if (msg->err_pos < 0)
2154 msg->err_pos = req->l;
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 goto return_bad_req;
2156 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002157
Willy Tarreau59234e92008-11-30 23:51:27 +01002158 /* 2: have we encountered a read error ? */
2159 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002160 if (!(s->flags & SN_ERR_MASK))
2161 s->flags |= SN_ERR_CLICL;
2162
Willy Tarreaufcffa692010-01-10 14:21:19 +01002163 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002164 goto failed_keep_alive;
2165
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002167 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002168 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002169 session_inc_http_err_ctr(s);
2170 }
2171
Willy Tarreau59234e92008-11-30 23:51:27 +01002172 msg->msg_state = HTTP_MSG_ERROR;
2173 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002174
Willy Tarreauda7ff642010-06-23 11:44:09 +02002175 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002176 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002177 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002178 if (s->listener->counters)
2179 s->listener->counters->failed_req++;
2180
Willy Tarreau59234e92008-11-30 23:51:27 +01002181 if (!(s->flags & SN_FINST_MASK))
2182 s->flags |= SN_FINST_R;
2183 return 0;
2184 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 /* 3: has the read timeout expired ? */
2187 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002188 if (!(s->flags & SN_ERR_MASK))
2189 s->flags |= SN_ERR_CLITO;
2190
Willy Tarreaufcffa692010-01-10 14:21:19 +01002191 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002192 goto failed_keep_alive;
2193
Willy Tarreau59234e92008-11-30 23:51:27 +01002194 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002195 if (msg->err_pos >= 0) {
Willy Tarreau078272e2010-12-12 12:46:33 +01002196 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002197 session_inc_http_err_ctr(s);
2198 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002199 txn->status = 408;
2200 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2201 msg->msg_state = HTTP_MSG_ERROR;
2202 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002203
Willy Tarreauda7ff642010-06-23 11:44:09 +02002204 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002205 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002206 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002207 if (s->listener->counters)
2208 s->listener->counters->failed_req++;
2209
Willy Tarreau59234e92008-11-30 23:51:27 +01002210 if (!(s->flags & SN_FINST_MASK))
2211 s->flags |= SN_FINST_R;
2212 return 0;
2213 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002214
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 /* 4: have we encountered a close ? */
2216 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002217 if (!(s->flags & SN_ERR_MASK))
2218 s->flags |= SN_ERR_CLICL;
2219
Willy Tarreaufcffa692010-01-10 14:21:19 +01002220 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002221 goto failed_keep_alive;
2222
Willy Tarreau4076a152009-04-02 15:18:36 +02002223 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01002224 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002225 txn->status = 400;
2226 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2227 msg->msg_state = HTTP_MSG_ERROR;
2228 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002229
Willy Tarreauda7ff642010-06-23 11:44:09 +02002230 session_inc_http_err_ctr(s);
2231 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002232 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002233 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002234 if (s->listener->counters)
2235 s->listener->counters->failed_req++;
2236
Willy Tarreau59234e92008-11-30 23:51:27 +01002237 if (!(s->flags & SN_FINST_MASK))
2238 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002239 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002240 }
2241
Willy Tarreau520d95e2009-09-19 21:04:57 +02002242 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002243 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002244 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002245#ifdef TCP_QUICKACK
2246 if (s->listener->options & LI_O_NOQUICKACK) {
2247 /* We need more data, we have to re-enable quick-ack in case we
2248 * previously disabled it, otherwise we might cause the client
2249 * to delay next data.
2250 */
2251 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2252 }
2253#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002254
Willy Tarreaufcffa692010-01-10 14:21:19 +01002255 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2256 /* If the client starts to talk, let's fall back to
2257 * request timeout processing.
2258 */
2259 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002260 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002261 }
2262
Willy Tarreau59234e92008-11-30 23:51:27 +01002263 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002264 if (!tick_isset(req->analyse_exp)) {
2265 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2266 (txn->flags & TX_WAIT_NEXT_RQ) &&
2267 tick_isset(s->be->timeout.httpka))
2268 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2269 else
2270 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2271 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002272
Willy Tarreau59234e92008-11-30 23:51:27 +01002273 /* we're not ready yet */
2274 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002275
2276 failed_keep_alive:
2277 /* Here we process low-level errors for keep-alive requests. In
2278 * short, if the request is not the first one and it experiences
2279 * a timeout, read error or shutdown, we just silently close so
2280 * that the client can try again.
2281 */
2282 txn->status = 0;
2283 msg->msg_state = HTTP_MSG_RQBEFORE;
2284 req->analysers = 0;
2285 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002286 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002287 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002288 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002289 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002290
Willy Tarreaud787e662009-07-07 10:14:51 +02002291 /* OK now we have a complete HTTP request with indexed headers. Let's
2292 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002293 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2294 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
2295 * points to the CRLF of the request line. req->lr points to the first
2296 * byte after the last LF. msg->col and msg->sov point to the first
2297 * byte of data. msg->eol cannot be trusted because it may have been
2298 * left uninitialized (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002299 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002300
Willy Tarreauda7ff642010-06-23 11:44:09 +02002301 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002302 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2303
Willy Tarreaub16a5742010-01-10 14:46:16 +01002304 if (txn->flags & TX_WAIT_NEXT_RQ) {
2305 /* kill the pending keep-alive timeout */
2306 txn->flags &= ~TX_WAIT_NEXT_RQ;
2307 req->analyse_exp = TICK_ETERNITY;
2308 }
2309
2310
Willy Tarreaud787e662009-07-07 10:14:51 +02002311 /* Maybe we found in invalid header name while we were configured not
2312 * to block on that, so we have to capture it now.
2313 */
2314 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01002315 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002316
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 /*
2318 * 1: identify the method
2319 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01002320 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002321
2322 /* we can make use of server redirect on GET and HEAD */
2323 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2324 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002325
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 /*
2327 * 2: check if the URI matches the monitor_uri.
2328 * We have to do this for every request which gets in, because
2329 * the monitor-uri is defined by the frontend.
2330 */
2331 if (unlikely((s->fe->monitor_uri_len != 0) &&
2332 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002333 !memcmp(msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002334 s->fe->monitor_uri,
2335 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002336 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002338 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002340
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002342 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002345 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2346 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 ret = acl_pass(ret);
2349 if (cond->pol == ACL_COND_UNLESS)
2350 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002351
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 if (ret) {
2353 /* we fail this request, let's return 503 service unavail */
2354 txn->status = 503;
2355 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2356 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002357 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002358 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 /* nothing to fail, let's reply normaly */
2361 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002362 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 goto return_prx_cond;
2364 }
2365
2366 /*
2367 * 3: Maybe we have to copy the original REQURI for the logs ?
2368 * Note: we cannot log anymore if the request has been
2369 * classified as invalid.
2370 */
2371 if (unlikely(s->logs.logwait & LW_REQ)) {
2372 /* we have a complete HTTP request that we must log */
2373 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2374 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002375
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 if (urilen >= REQURI_LEN)
2377 urilen = REQURI_LEN - 1;
2378 memcpy(txn->uri, &req->data[msg->som], urilen);
2379 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 if (!(s->logs.logwait &= ~LW_REQ))
2382 s->do_log(s);
2383 } else {
2384 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002385 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 }
Willy Tarreau06619262006-12-17 08:37:22 +01002387
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002389 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2390 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002391
Willy Tarreau5b154472009-12-21 20:11:07 +01002392 /* ... and check if the request is HTTP/1.1 or above */
2393 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01002394 ((msg->sol[msg->sl.rq.v + 5] > '1') ||
2395 ((msg->sol[msg->sl.rq.v + 5] == '1') &&
2396 (msg->sol[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01002397 txn->flags |= TX_REQ_VER_11;
2398
2399 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002400 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002401
Willy Tarreau88d349d2010-01-25 12:15:43 +01002402 /* if the frontend has "option http-use-proxy-header", we'll check if
2403 * we have what looks like a proxied connection instead of a connection,
2404 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2405 * Note that this is *not* RFC-compliant, however browsers and proxies
2406 * happen to do that despite being non-standard :-(
2407 * We consider that a request not beginning with either '/' or '*' is
2408 * a proxied connection, which covers both "scheme://location" and
2409 * CONNECT ip:port.
2410 */
2411 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
2412 msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*')
2413 txn->flags |= TX_USE_PX_CONN;
2414
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002415 /* transfer length unknown*/
2416 txn->flags &= ~TX_REQ_XFER_LEN;
2417
Willy Tarreau59234e92008-11-30 23:51:27 +01002418 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002419 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01002420 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002421 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002422
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002423 /* 6: determine the transfer-length.
2424 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2425 * the presence of a message-body in a REQUEST and its transfer length
2426 * must be determined that way (in order of precedence) :
2427 * 1. The presence of a message-body in a request is signaled by the
2428 * inclusion of a Content-Length or Transfer-Encoding header field
2429 * in the request's header fields. When a request message contains
2430 * both a message-body of non-zero length and a method that does
2431 * not define any semantics for that request message-body, then an
2432 * origin server SHOULD either ignore the message-body or respond
2433 * with an appropriate error message (e.g., 413). A proxy or
2434 * gateway, when presented the same request, SHOULD either forward
2435 * the request inbound with the message- body or ignore the
2436 * message-body when determining a response.
2437 *
2438 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2439 * and the "chunked" transfer-coding (Section 6.2) is used, the
2440 * transfer-length is defined by the use of this transfer-coding.
2441 * If a Transfer-Encoding header field is present and the "chunked"
2442 * transfer-coding is not present, the transfer-length is defined
2443 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002444 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002445 * 3. If a Content-Length header field is present, its decimal value in
2446 * OCTETs represents both the entity-length and the transfer-length.
2447 * If a message is received with both a Transfer-Encoding header
2448 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002449 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002450 * 4. By the server closing the connection. (Closing the connection
2451 * cannot be used to indicate the end of a request body, since that
2452 * would leave no possibility for the server to send back a response.)
2453 *
2454 * Whenever a transfer-coding is applied to a message-body, the set of
2455 * transfer-codings MUST include "chunked", unless the message indicates
2456 * it is terminated by closing the connection. When the "chunked"
2457 * transfer-coding is used, it MUST be the last transfer-coding applied
2458 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002459 */
2460
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002461 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002463 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01002464 while ((txn->flags & TX_REQ_VER_11) &&
2465 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002466 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
2467 txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2468 else if (txn->flags & TX_REQ_TE_CHNK) {
2469 /* bad transfer-encoding (chunked followed by something else) */
2470 use_close_only = 1;
2471 txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN);
2472 break;
2473 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 }
2475
Willy Tarreau32b47f42009-10-18 20:55:02 +02002476 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002477 while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only &&
Willy Tarreau32b47f42009-10-18 20:55:02 +02002478 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
2479 signed long long cl;
2480
Willy Tarreauad14f752011-09-02 20:33:27 +02002481 if (!ctx.vlen) {
2482 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002483 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002484 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002485
Willy Tarreauad14f752011-09-02 20:33:27 +02002486 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2487 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002488 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002489 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002490
Willy Tarreauad14f752011-09-02 20:33:27 +02002491 if (cl < 0) {
2492 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002493 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002494 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002495
Willy Tarreauad14f752011-09-02 20:33:27 +02002496 if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) {
2497 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002498 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002499 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002500
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002501 txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002502 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002503 }
2504
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002505 /* bodyless requests have a known length */
2506 if (!use_close_only)
2507 txn->flags |= TX_REQ_XFER_LEN;
2508
Willy Tarreaud787e662009-07-07 10:14:51 +02002509 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002510 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002511 req->analyse_exp = TICK_ETERNITY;
2512 return 1;
2513
2514 return_bad_req:
2515 /* We centralize bad requests processing here */
2516 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2517 /* we detected a parsing error. We want to archive this request
2518 * in the dedicated proxy area for later troubleshooting.
2519 */
Willy Tarreau078272e2010-12-12 12:46:33 +01002520 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002521 }
2522
2523 txn->req.msg_state = HTTP_MSG_ERROR;
2524 txn->status = 400;
2525 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002526
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002527 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002528 if (s->listener->counters)
2529 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002530
2531 return_prx_cond:
2532 if (!(s->flags & SN_ERR_MASK))
2533 s->flags |= SN_ERR_PRXCOND;
2534 if (!(s->flags & SN_FINST_MASK))
2535 s->flags |= SN_FINST_R;
2536
2537 req->analysers = 0;
2538 req->analyse_exp = TICK_ETERNITY;
2539 return 0;
2540}
2541
Cyril Bonté70be45d2010-10-12 00:14:35 +02002542/* We reached the stats page through a POST request.
2543 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002544 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002545 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002546int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002547{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002548 struct proxy *px = NULL;
2549 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002550
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002551 char key[LINESIZE];
2552 int action = ST_ADM_ACTION_NONE;
2553 int reprocess = 0;
2554
2555 int total_servers = 0;
2556 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002557
2558 char *first_param, *cur_param, *next_param, *end_params;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002559 char *st_cur_param, *st_next_param;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002560
2561 first_param = req->data + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002562 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002563
2564 cur_param = next_param = end_params;
2565
Cyril Bonté23b39d92011-02-10 22:54:44 +01002566 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002567 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002568 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002569 return 1;
2570 }
2571 else if (end_params > req->data + req->l) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002572 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002573 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002574 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002575 }
2576
2577 *end_params = '\0';
2578
Willy Tarreau295a8372011-03-10 11:25:07 +01002579 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002580
2581 /*
2582 * Parse the parameters in reverse order to only store the last value.
2583 * From the html form, the backend and the action are at the end.
2584 */
2585 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002586 char *value;
2587 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002588
2589 cur_param--;
2590 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002591 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002592 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002593 poffset = (cur_param != first_param ? 1 : 0);
2594 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2595 if ((plen > 0) && (plen <= sizeof(key))) {
2596 strncpy(key, cur_param + poffset, plen);
2597 key[plen - 1] = '\0';
2598 } else {
2599 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2600 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002601 }
2602
2603 /* Parse the value */
2604 value = key;
2605 while (*value != '\0' && *value != '=') {
2606 value++;
2607 }
2608 if (*value == '=') {
2609 /* Ok, a value is found, we can mark the end of the key */
2610 *value++ = '\0';
2611 }
2612
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002613 if (!url_decode(key) || !url_decode(value))
2614 break;
2615
Cyril Bonté70be45d2010-10-12 00:14:35 +02002616 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002617 if (!px && (strcmp(key, "b") == 0)) {
2618 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2619 /* the backend name is unknown or ambiguous (duplicate names) */
2620 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2621 goto out;
2622 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002623 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002624 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002625 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002626 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002627 }
2628 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002629 action = ST_ADM_ACTION_ENABLE;
2630 }
2631 else {
2632 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2633 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002634 }
2635 }
2636 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002637 if (!(px && action)) {
2638 /*
2639 * Indicates that we'll need to reprocess the parameters
2640 * as soon as backend and action are known
2641 */
2642 if (!reprocess) {
2643 st_cur_param = cur_param;
2644 st_next_param = next_param;
2645 }
2646 reprocess = 1;
2647 }
2648 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002649 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002650 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002651 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002652 /* Not already in maintenance, we can change the server state */
2653 sv->state |= SRV_MAINTAIN;
2654 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002655 altered_servers++;
2656 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002657 }
2658 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002659 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002660 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002661 /* Already in maintenance, we can change the server state */
2662 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002663 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002664 altered_servers++;
2665 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002666 }
2667 break;
2668 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002669 } else {
2670 /* the server name is unknown or ambiguous (duplicate names) */
2671 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002672 }
2673 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002674 if (reprocess && px && action) {
2675 /* Now, we know the backend and the action chosen by the user.
2676 * We can safely restart from the first server parameter
2677 * to reprocess them
2678 */
2679 cur_param = st_cur_param;
2680 next_param = st_next_param;
2681 reprocess = 0;
2682 goto reprocess_servers;
2683 }
2684
Cyril Bonté70be45d2010-10-12 00:14:35 +02002685 next_param = cur_param;
2686 }
2687 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002688
2689 if (total_servers == 0) {
2690 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2691 }
2692 else if (altered_servers == 0) {
2693 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2694 }
2695 else if (altered_servers == total_servers) {
2696 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2697 }
2698 else {
2699 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2700 }
2701 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002702 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002703}
2704
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002705/* returns a pointer to the first rule which forbids access (deny or http_auth),
2706 * or NULL if everything's OK.
2707 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002708static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002709http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2710{
Willy Tarreauff011f22011-01-06 17:51:27 +01002711 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002712
Willy Tarreauff011f22011-01-06 17:51:27 +01002713 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002714 int ret = 1;
2715
Willy Tarreauff011f22011-01-06 17:51:27 +01002716 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002717 continue;
2718
2719 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002720 if (rule->cond) {
2721 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002722 ret = acl_pass(ret);
2723
Willy Tarreauff011f22011-01-06 17:51:27 +01002724 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002725 ret = !ret;
2726 }
2727
2728 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002729 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002730 return NULL; /* no problem */
2731 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002732 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002733 }
2734 }
2735 return NULL;
2736}
2737
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002738/* This stream analyser runs all HTTP request processing which is common to
2739 * frontends and backends, which means blocking ACLs, filters, connection-close,
2740 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002741 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002742 * either needs more data or wants to immediately abort the request (eg: deny,
2743 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002744 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002745int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002746{
Willy Tarreaud787e662009-07-07 10:14:51 +02002747 struct http_txn *txn = &s->txn;
2748 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002749 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002750 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002751 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002752 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002753 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002754
Willy Tarreau655dce92009-11-08 13:10:58 +01002755 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002756 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002757 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002758 return 0;
2759 }
2760
Willy Tarreau3a816292009-07-07 10:55:49 +02002761 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002762 req->analyse_exp = TICK_ETERNITY;
2763
2764 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
2765 now_ms, __FUNCTION__,
2766 s,
2767 req,
2768 req->rex, req->wex,
2769 req->flags,
2770 req->l,
2771 req->analysers);
2772
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002773 /* first check whether we have some ACLs set to block this request */
2774 list_for_each_entry(cond, &px->block_cond, list) {
2775 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002776
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002777 ret = acl_pass(ret);
2778 if (cond->pol == ACL_COND_UNLESS)
2779 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002780
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002781 if (ret) {
2782 txn->status = 403;
2783 /* let's log the request time */
2784 s->logs.tv_request = now;
2785 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002786 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002787 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002788 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002789 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002790
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002791 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002792 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002793
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002794 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002795 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002796 do_stats = stats_check_uri(s->rep->prod, txn, px);
2797 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002798 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 +01002799 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002800 else
2801 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002802
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002803 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002804 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002805 txn->status = 403;
2806 s->logs.tv_request = now;
2807 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002808 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002809 s->fe->fe_counters.denied_req++;
2810 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2811 s->be->be_counters.denied_req++;
2812 if (s->listener->counters)
2813 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002814 goto return_prx_cond;
2815 }
2816
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002817 /* try headers filters */
2818 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002819 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002820 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002821
Willy Tarreau59234e92008-11-30 23:51:27 +01002822 /* has the request been denied ? */
2823 if (txn->flags & TX_CLDENY) {
2824 /* no need to go further */
2825 txn->status = 403;
2826 /* let's log the request time */
2827 s->logs.tv_request = now;
2828 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002829 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002830 goto return_prx_cond;
2831 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002832
2833 /* When a connection is tarpitted, we use the tarpit timeout,
2834 * which may be the same as the connect timeout if unspecified.
2835 * If unset, then set it to zero because we really want it to
2836 * eventually expire. We build the tarpit as an analyser.
2837 */
2838 if (txn->flags & TX_CLTARPIT) {
2839 buffer_erase(s->req);
2840 /* wipe the request out so that we can drop the connection early
2841 * if the client closes first.
2842 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002843 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002844 req->analysers = 0; /* remove switching rules etc... */
2845 req->analysers |= AN_REQ_HTTP_TARPIT;
2846 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2847 if (!req->analyse_exp)
2848 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002849 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002850 return 1;
2851 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002852 }
Willy Tarreau06619262006-12-17 08:37:22 +01002853
Willy Tarreau5b154472009-12-21 20:11:07 +01002854 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2855 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002856 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2857 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002858 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2859 * avoid to redo the same work if FE and BE have the same settings (common).
2860 * The method consists in checking if options changed between the two calls
2861 * (implying that either one is non-null, or one of them is non-null and we
2862 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002863 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002864
Willy Tarreaudc008c52010-02-01 16:20:08 +01002865 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2866 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2867 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2868 (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 +01002869 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002870
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002871 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2872 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002873 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002874 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2875 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002876 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002877 tmp = TX_CON_WANT_CLO;
2878
Willy Tarreau5b154472009-12-21 20:11:07 +01002879 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2880 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002881
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002882 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2883 /* parse the Connection header and possibly clean it */
2884 int to_del = 0;
2885 if ((txn->flags & TX_REQ_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002886 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2887 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002888 to_del |= 2; /* remove "keep-alive" */
2889 if (!(txn->flags & TX_REQ_VER_11))
2890 to_del |= 1; /* remove "close" */
2891 http_parse_connection_header(txn, msg, req, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002892 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002893
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002894 /* check if client or config asks for explicit close in KAL/SCL */
2895 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2896 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2897 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
2898 (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 +01002899 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002900 !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */
2901 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002902 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2903 }
Willy Tarreau78599912009-10-17 20:12:21 +02002904
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002905 /* we can be blocked here because the request needs to be authenticated,
2906 * either to pass or to access stats.
2907 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002908 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002909 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002910 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002911
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002912 if (!realm)
2913 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2914
Willy Tarreau844a7e72010-01-31 21:46:18 +01002915 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002916 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2917 txn->status = 401;
2918 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002919 /* on 401 we still count one error, because normal browsing
2920 * won't significantly increase the counter but brute force
2921 * attempts will.
2922 */
2923 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002924 goto return_prx_cond;
2925 }
2926
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002927 /* add request headers from the rule sets in the same order */
2928 list_for_each_entry(wl, &px->req_add, list) {
2929 if (wl->cond) {
2930 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2931 ret = acl_pass(ret);
2932 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2933 ret = !ret;
2934 if (!ret)
2935 continue;
2936 }
2937
2938 if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0))
2939 goto return_bad_req;
2940 }
2941
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002942 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002943 struct stats_admin_rule *stats_admin_rule;
2944
2945 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002946 * FIXME!!! that one is rather dangerous, we want to
2947 * make it follow standard rules (eg: clear req->analysers).
2948 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002949
Cyril Bonté474be412010-10-12 00:14:36 +02002950 /* now check whether we have some admin rules for this request */
2951 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2952 int ret = 1;
2953
2954 if (stats_admin_rule->cond) {
2955 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2956 ret = acl_pass(ret);
2957 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2958 ret = !ret;
2959 }
2960
2961 if (ret) {
2962 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002963 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002964 break;
2965 }
2966 }
2967
Cyril Bonté70be45d2010-10-12 00:14:35 +02002968 /* Was the status page requested with a POST ? */
2969 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002970 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002971 if (msg->msg_state < HTTP_MSG_100_SENT) {
2972 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2973 * send an HTTP/1.1 100 Continue intermediate response.
2974 */
2975 if (txn->flags & TX_REQ_VER_11) {
2976 struct hdr_ctx ctx;
2977 ctx.idx = 0;
2978 /* Expect is allowed in 1.1, look for it */
2979 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
2980 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2981 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
2982 }
2983 }
2984 msg->msg_state = HTTP_MSG_100_SENT;
2985 s->logs.tv_request = now; /* update the request timer to reflect full request */
2986 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002987 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002988 /* we need more data */
2989 req->analysers |= an_bit;
2990 buffer_dont_connect(req);
2991 return 0;
2992 }
Cyril Bonté474be412010-10-12 00:14:36 +02002993 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01002994 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02002995 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002996 }
2997
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002998 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002999 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003000 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003001 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003002 s->rep->prod->applet.private = s;
3003 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003004 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003005 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3006 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003007
3008 return 0;
3009
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003010 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003011
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003012 /* check whether we have some ACLs set to redirect this request */
3013 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003014 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003015
Willy Tarreauf285f542010-01-03 20:03:03 +01003016 if (rule->cond) {
3017 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3018 ret = acl_pass(ret);
3019 if (rule->cond->pol == ACL_COND_UNLESS)
3020 ret = !ret;
3021 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003022
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003023 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003024 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003025 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003026
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003027 /* build redirect message */
3028 switch(rule->code) {
3029 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003030 msg_fmt = HTTP_303;
3031 break;
3032 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003033 msg_fmt = HTTP_301;
3034 break;
3035 case 302:
3036 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 msg_fmt = HTTP_302;
3038 break;
3039 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003040
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003041 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003042 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003043
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003044 switch(rule->type) {
3045 case REDIRECT_TYPE_PREFIX: {
3046 const char *path;
3047 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003048
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 path = http_get_path(txn);
3050 /* build message using path */
3051 if (path) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003052 pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3054 int qs = 0;
3055 while (qs < pathlen) {
3056 if (path[qs] == '?') {
3057 pathlen = qs;
3058 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003059 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003060 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003061 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003062 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 } else {
3064 path = "/";
3065 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003066 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003067
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003068 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003069 goto return_bad_req;
3070
3071 /* add prefix. Note that if prefix == "/", we don't want to
3072 * add anything, otherwise it makes it hard for the user to
3073 * configure a self-redirection.
3074 */
3075 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003076 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3077 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003078 }
3079
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003080 /* add path */
3081 memcpy(rdr.str + rdr.len, path, pathlen);
3082 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003083
3084 /* append a slash at the end of the location is needed and missing */
3085 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3086 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3087 if (rdr.len > rdr.size - 5)
3088 goto return_bad_req;
3089 rdr.str[rdr.len] = '/';
3090 rdr.len++;
3091 }
3092
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003093 break;
3094 }
3095 case REDIRECT_TYPE_LOCATION:
3096 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003097 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003099
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003100 /* add location */
3101 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3102 rdr.len += rule->rdr_len;
3103 break;
3104 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003105
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003106 if (rule->cookie_len) {
3107 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3108 rdr.len += 14;
3109 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3110 rdr.len += rule->cookie_len;
3111 memcpy(rdr.str + rdr.len, "\r\n", 2);
3112 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003113 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003114
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003115 /* add end of headers and the keep-alive/close status.
3116 * We may choose to set keep-alive if the Location begins
3117 * with a slash, because the client will come back to the
3118 * same server.
3119 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003120 txn->status = rule->code;
3121 /* let's log the request time */
3122 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003123
3124 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3125 (txn->flags & TX_REQ_XFER_LEN) &&
Willy Tarreau124d9912011-03-01 20:30:48 +01003126 !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003127 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3128 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3129 /* keep-alive possible */
Willy Tarreau75661452010-01-10 10:35:01 +01003130 if (!(txn->flags & TX_REQ_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003131 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3132 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3133 rdr.len += 30;
3134 } else {
3135 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3136 rdr.len += 24;
3137 }
Willy Tarreau75661452010-01-10 10:35:01 +01003138 }
3139 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3140 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003141 buffer_write(req->prod->ob, rdr.str, rdr.len);
3142 /* "eat" the request */
3143 buffer_ignore(req, msg->sov - msg->som);
3144 msg->som = msg->sov;
3145 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003146 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3147 txn->req.msg_state = HTTP_MSG_CLOSED;
3148 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003149 break;
3150 } else {
3151 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003152 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3153 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3154 rdr.len += 29;
3155 } else {
3156 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3157 rdr.len += 23;
3158 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003159 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003160 goto return_prx_cond;
3161 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003162 }
3163 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003164
Willy Tarreau2be39392010-01-03 17:24:51 +01003165 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3166 * If this happens, then the data will not come immediately, so we must
3167 * send all what we have without waiting. Note that due to the small gain
3168 * in waiting for the body of the request, it's easier to simply put the
3169 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3170 * itself once used.
3171 */
3172 req->flags |= BF_SEND_DONTWAIT;
3173
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003174 /* that's OK for us now, let's move on to next analysers */
3175 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003176
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003177 return_bad_req:
3178 /* We centralize bad requests processing here */
3179 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3180 /* we detected a parsing error. We want to archive this request
3181 * in the dedicated proxy area for later troubleshooting.
3182 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003183 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003184 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003185
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003186 txn->req.msg_state = HTTP_MSG_ERROR;
3187 txn->status = 400;
3188 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003189
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003190 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003191 if (s->listener->counters)
3192 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003193
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003194 return_prx_cond:
3195 if (!(s->flags & SN_ERR_MASK))
3196 s->flags |= SN_ERR_PRXCOND;
3197 if (!(s->flags & SN_FINST_MASK))
3198 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003199
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003200 req->analysers = 0;
3201 req->analyse_exp = TICK_ETERNITY;
3202 return 0;
3203}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003204
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003205/* This function performs all the processing enabled for the current request.
3206 * It returns 1 if the processing can continue on next analysers, or zero if it
3207 * needs more data, encounters an error, or wants to immediately abort the
3208 * request. It relies on buffers flags, and updates s->req->analysers.
3209 */
3210int http_process_request(struct session *s, struct buffer *req, int an_bit)
3211{
3212 struct http_txn *txn = &s->txn;
3213 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003214
Willy Tarreau655dce92009-11-08 13:10:58 +01003215 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003216 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003217 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003218 return 0;
3219 }
3220
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003221 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
3222 now_ms, __FUNCTION__,
3223 s,
3224 req,
3225 req->rex, req->wex,
3226 req->flags,
3227 req->l,
3228 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003229
Willy Tarreau59234e92008-11-30 23:51:27 +01003230 /*
3231 * Right now, we know that we have processed the entire headers
3232 * and that unwanted requests have been filtered out. We can do
3233 * whatever we want with the remaining request. Also, now we
3234 * may have separate values for ->fe, ->be.
3235 */
Willy Tarreau06619262006-12-17 08:37:22 +01003236
Willy Tarreau59234e92008-11-30 23:51:27 +01003237 /*
3238 * If HTTP PROXY is set we simply get remote server address
3239 * parsing incoming request.
3240 */
3241 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau6471afb2011-09-23 10:54:59 +02003242 url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003243 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003244
Willy Tarreau59234e92008-11-30 23:51:27 +01003245 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003246 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003247 * Note that doing so might move headers in the request, but
3248 * the fields will stay coherent and the URI will not move.
3249 * This should only be performed in the backend.
3250 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003251 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003252 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3253 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003254
Willy Tarreau59234e92008-11-30 23:51:27 +01003255 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003256 * 8: the appsession cookie was looked up very early in 1.2,
3257 * so let's do the same now.
3258 */
3259
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003260 /* It needs to look into the URI unless persistence must be ignored */
3261 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01003262 get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003263 }
3264
3265 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003266 * 9: add X-Forwarded-For if either the frontend or the backend
3267 * asks for it.
3268 */
3269 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003270 struct hdr_ctx ctx = { .idx = 0 };
3271
3272 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Sagi Bashari1611e2d2011-10-08 22:48:48 +02003273 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 +02003274 /* The header is set to be added only if none is present
3275 * and we found it, so don't do anything.
3276 */
3277 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003278 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003279 /* Add an X-Forwarded-For header unless the source IP is
3280 * in the 'except' network range.
3281 */
3282 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003283 (((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 +01003284 != s->fe->except_net.s_addr) &&
3285 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003286 (((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 +01003287 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003288 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003289 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003290 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003291
3292 /* Note: we rely on the backend to get the header name to be used for
3293 * x-forwarded-for, because the header is really meant for the backends.
3294 * However, if the backend did not specify any option, we have to rely
3295 * on the frontend's header name.
3296 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003297 if (s->be->fwdfor_hdr_len) {
3298 len = s->be->fwdfor_hdr_len;
3299 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003300 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003301 len = s->fe->fwdfor_hdr_len;
3302 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003303 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003304 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003305
Willy Tarreau4af6f3a2007-03-18 22:36:26 +01003306 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003307 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003308 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003309 }
3310 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003311 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003312 /* FIXME: for the sake of completeness, we should also support
3313 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003314 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003315 int len;
3316 char pn[INET6_ADDRSTRLEN];
3317 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003318 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003319 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003320
Willy Tarreau59234e92008-11-30 23:51:27 +01003321 /* Note: we rely on the backend to get the header name to be used for
3322 * x-forwarded-for, because the header is really meant for the backends.
3323 * However, if the backend did not specify any option, we have to rely
3324 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003325 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003326 if (s->be->fwdfor_hdr_len) {
3327 len = s->be->fwdfor_hdr_len;
3328 memcpy(trash, s->be->fwdfor_hdr_name, len);
3329 } else {
3330 len = s->fe->fwdfor_hdr_len;
3331 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003332 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003333 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003334
Willy Tarreau59234e92008-11-30 23:51:27 +01003335 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003336 &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003337 goto return_bad_req;
3338 }
3339 }
3340
3341 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003342 * 10: add X-Original-To if either the frontend or the backend
3343 * asks for it.
3344 */
3345 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3346
3347 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003348 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003349 /* Add an X-Original-To header unless the destination IP is
3350 * in the 'except' network range.
3351 */
3352 if (!(s->flags & SN_FRT_ADDR_SET))
3353 get_frt_addr(s);
3354
Willy Tarreau6471afb2011-09-23 10:54:59 +02003355 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003356 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003357 (((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 +02003358 != s->fe->except_to.s_addr) &&
3359 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003360 (((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 +02003361 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003362 int len;
3363 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003364 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003365
3366 /* Note: we rely on the backend to get the header name to be used for
3367 * x-original-to, because the header is really meant for the backends.
3368 * However, if the backend did not specify any option, we have to rely
3369 * on the frontend's header name.
3370 */
3371 if (s->be->orgto_hdr_len) {
3372 len = s->be->orgto_hdr_len;
3373 memcpy(trash, s->be->orgto_hdr_name, len);
3374 } else {
3375 len = s->fe->orgto_hdr_len;
3376 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003377 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003378 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3379
3380 if (unlikely(http_header_add_tail2(req, &txn->req,
Willy Tarreau58cc8722009-12-28 06:57:33 +01003381 &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003382 goto return_bad_req;
3383 }
3384 }
3385 }
3386
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003387 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3388 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003389 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003390 unsigned int want_flags = 0;
3391
3392 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003393 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3394 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3395 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003396 want_flags |= TX_CON_CLO_SET;
3397 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003398 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3399 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3400 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003401 want_flags |= TX_CON_KAL_SET;
3402 }
3403
3404 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
3405 http_change_connection_header(txn, msg, req, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003406 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003407
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003408
Willy Tarreau522d6c02009-12-06 18:49:18 +01003409 /* If we have no server assigned yet and we're balancing on url_param
3410 * with a POST request, we may be interested in checking the body for
3411 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003412 */
3413 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3414 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003415 s->be->url_param_post_limit != 0 &&
Willy Tarreau61a21a32011-03-01 20:35:49 +01003416 (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003417 buffer_dont_connect(req);
3418 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003419 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003420
Willy Tarreau5e205522011-12-17 16:34:27 +01003421 if (txn->flags & TX_REQ_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003422 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003423#ifdef TCP_QUICKACK
3424 /* We expect some data from the client. Unless we know for sure
3425 * we already have a full request, we have to re-enable quick-ack
3426 * in case we previously disabled it, otherwise we might cause
3427 * the client to delay further data.
3428 */
3429 if ((s->listener->options & LI_O_NOQUICKACK) &&
3430 ((txn->flags & TX_REQ_TE_CHNK) ||
3431 (msg->body_len > req->l - txn->req.eoh - 2)))
3432 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3433#endif
3434 }
Willy Tarreau03945942009-12-22 16:50:27 +01003435
Willy Tarreau59234e92008-11-30 23:51:27 +01003436 /*************************************************************
3437 * OK, that's finished for the headers. We have done what we *
3438 * could. Let's switch to the DATA state. *
3439 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003440 req->analyse_exp = TICK_ETERNITY;
3441 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003442
Willy Tarreau59234e92008-11-30 23:51:27 +01003443 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003444 /* OK let's go on with the BODY now */
3445 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003446
Willy Tarreau59234e92008-11-30 23:51:27 +01003447 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003448 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003449 /* we detected a parsing error. We want to archive this request
3450 * in the dedicated proxy area for later troubleshooting.
3451 */
Willy Tarreau078272e2010-12-12 12:46:33 +01003452 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003453 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003454
Willy Tarreau59234e92008-11-30 23:51:27 +01003455 txn->req.msg_state = HTTP_MSG_ERROR;
3456 txn->status = 400;
3457 req->analysers = 0;
3458 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003459
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003460 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003461 if (s->listener->counters)
3462 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003463
Willy Tarreau59234e92008-11-30 23:51:27 +01003464 if (!(s->flags & SN_ERR_MASK))
3465 s->flags |= SN_ERR_PRXCOND;
3466 if (!(s->flags & SN_FINST_MASK))
3467 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003468 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003469}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003470
Willy Tarreau60b85b02008-11-30 23:28:40 +01003471/* This function is an analyser which processes the HTTP tarpit. It always
3472 * returns zero, at the beginning because it prevents any other processing
3473 * from occurring, and at the end because it terminates the request.
3474 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003475int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003476{
3477 struct http_txn *txn = &s->txn;
3478
3479 /* This connection is being tarpitted. The CLIENT side has
3480 * already set the connect expiration date to the right
3481 * timeout. We just have to check that the client is still
3482 * there and that the timeout has not expired.
3483 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003484 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003485 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3486 !tick_is_expired(req->analyse_exp, now_ms))
3487 return 0;
3488
3489 /* We will set the queue timer to the time spent, just for
3490 * logging purposes. We fake a 500 server error, so that the
3491 * attacker will not suspect his connection has been tarpitted.
3492 * It will not cause trouble to the logs because we can exclude
3493 * the tarpitted connections by filtering on the 'PT' status flags.
3494 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003495 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3496
3497 txn->status = 500;
3498 if (req->flags != BF_READ_ERROR)
3499 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3500
3501 req->analysers = 0;
3502 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003503
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003504 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003505 if (s->listener->counters)
3506 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003507
Willy Tarreau60b85b02008-11-30 23:28:40 +01003508 if (!(s->flags & SN_ERR_MASK))
3509 s->flags |= SN_ERR_PRXCOND;
3510 if (!(s->flags & SN_FINST_MASK))
3511 s->flags |= SN_FINST_T;
3512 return 0;
3513}
3514
Willy Tarreaud34af782008-11-30 23:36:37 +01003515/* This function is an analyser which processes the HTTP request body. It looks
3516 * for parameters to be used for the load balancing algorithm (url_param). It
3517 * must only be called after the standard HTTP request processing has occurred,
3518 * because it expects the request to be parsed. It returns zero if it needs to
3519 * read more data, or 1 once it has completed its analysis.
3520 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003521int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003522{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003523 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003524 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003525 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003526
3527 /* We have to parse the HTTP request body to find any required data.
3528 * "balance url_param check_post" should have been the only way to get
3529 * into this. We were brought here after HTTP header analysis, so all
3530 * related structures are ready.
3531 */
3532
Willy Tarreau522d6c02009-12-06 18:49:18 +01003533 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3534 goto missing_data;
3535
3536 if (msg->msg_state < HTTP_MSG_100_SENT) {
3537 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3538 * send an HTTP/1.1 100 Continue intermediate response.
3539 */
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01003540 if (txn->flags & TX_REQ_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003541 struct hdr_ctx ctx;
3542 ctx.idx = 0;
3543 /* Expect is allowed in 1.1, look for it */
3544 if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) &&
3545 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3546 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3547 }
3548 }
3549 msg->msg_state = HTTP_MSG_100_SENT;
3550 }
3551
3552 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003553 /* we have msg->col and msg->sov which both point to the first
3554 * byte of message body. msg->som still points to the beginning
3555 * of the message. We must save the body in req->lr because it
3556 * survives buffer re-alignments.
3557 */
3558 req->lr = req->data + msg->sov;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003559 if (txn->flags & TX_REQ_TE_CHNK)
3560 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3561 else
3562 msg->msg_state = HTTP_MSG_DATA;
3563 }
3564
3565 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003566 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01003567 * set ->sov and ->lr to point to the body and switch to DATA or
3568 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003569 */
3570 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003571
Willy Tarreau115acb92009-12-26 13:56:06 +01003572 if (!ret)
3573 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003574 else if (ret < 0) {
3575 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003576 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003577 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003578 }
3579
Willy Tarreaud98cf932009-12-27 22:54:55 +01003580 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreau522d6c02009-12-06 18:49:18 +01003581 * We have the first non-header byte in msg->col, which is either the
3582 * beginning of the chunk size or of the data. The first data byte is in
3583 * msg->sov, which is equal to msg->col when not using transfer-encoding.
3584 * We're waiting for at least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003585 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003586
Willy Tarreau124d9912011-03-01 20:30:48 +01003587 if (msg->body_len < limit)
3588 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003589
Willy Tarreau7c96f672009-12-27 22:47:25 +01003590 if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003591 goto http_end;
3592
3593 missing_data:
3594 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003595 if (req->flags & BF_FULL) {
3596 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003597 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003598 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003599
Willy Tarreau522d6c02009-12-06 18:49:18 +01003600 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3601 txn->status = 408;
3602 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003603
3604 if (!(s->flags & SN_ERR_MASK))
3605 s->flags |= SN_ERR_CLITO;
3606 if (!(s->flags & SN_FINST_MASK))
3607 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003608 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003609 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003610
3611 /* we get here if we need to wait for more data */
3612 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003613 /* Not enough data. We'll re-use the http-request
3614 * timeout here. Ideally, we should set the timeout
3615 * relative to the accept() date. We just set the
3616 * request timeout once at the beginning of the
3617 * request.
3618 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003619 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003620 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003621 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003622 return 0;
3623 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003624
3625 http_end:
3626 /* The situation will not evolve, so let's give up on the analysis. */
3627 s->logs.tv_request = now; /* update the request timer to reflect full request */
3628 req->analysers &= ~an_bit;
3629 req->analyse_exp = TICK_ETERNITY;
3630 return 1;
3631
3632 return_bad_req: /* let's centralize all bad requests */
3633 txn->req.msg_state = HTTP_MSG_ERROR;
3634 txn->status = 400;
3635 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3636
Willy Tarreau79ebac62010-06-07 13:47:49 +02003637 if (!(s->flags & SN_ERR_MASK))
3638 s->flags |= SN_ERR_PRXCOND;
3639 if (!(s->flags & SN_FINST_MASK))
3640 s->flags |= SN_FINST_R;
3641
Willy Tarreau522d6c02009-12-06 18:49:18 +01003642 return_err_msg:
3643 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003644 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003645 if (s->listener->counters)
3646 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003647 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003648}
3649
Mark Lamourinec2247f02012-01-04 13:02:01 -05003650int http_send_name_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, struct proxy* be, const char* srv_name) {
3651
3652 struct hdr_ctx ctx;
3653
Mark Lamourinec2247f02012-01-04 13:02:01 -05003654 char *hdr_name = be->server_id_hdr_name;
3655 int hdr_name_len = be->server_id_hdr_len;
3656
3657 char *hdr_val;
3658
William Lallemandd9e90662012-01-30 17:27:17 +01003659 ctx.idx = 0;
3660
Mark Lamourinec2247f02012-01-04 13:02:01 -05003661 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
3662 /* remove any existing values from the header */
3663 http_remove_header2(msg, buf, &txn->hdr_idx, &ctx);
3664 }
3665
3666 /* Add the new header requested with the server value */
3667 hdr_val = trash;
3668 memcpy(hdr_val, hdr_name, hdr_name_len);
3669 hdr_val += hdr_name_len;
3670 *hdr_val++ = ':';
3671 *hdr_val++ = ' ';
3672 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
3673 http_header_add_tail2(buf, msg, &txn->hdr_idx, trash, hdr_val - trash);
3674
3675 return 0;
3676}
3677
Willy Tarreau610ecce2010-01-04 21:15:02 +01003678/* Terminate current transaction and prepare a new one. This is very tricky
3679 * right now but it works.
3680 */
3681void http_end_txn_clean_session(struct session *s)
3682{
3683 /* FIXME: We need a more portable way of releasing a backend's and a
3684 * server's connections. We need a safer way to reinitialize buffer
3685 * flags. We also need a more accurate method for computing per-request
3686 * data.
3687 */
3688 http_silent_debug(__LINE__, s);
3689
3690 s->req->cons->flags |= SI_FL_NOLINGER;
3691 s->req->cons->shutr(s->req->cons);
3692 s->req->cons->shutw(s->req->cons);
3693
3694 http_silent_debug(__LINE__, s);
3695
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003696 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003697 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003698 if (unlikely(s->srv_conn))
3699 sess_change_server(s, NULL);
3700 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003701
3702 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3703 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003704 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003705
3706 if (s->txn.status) {
3707 int n;
3708
3709 n = s->txn.status / 100;
3710 if (n < 1 || n > 5)
3711 n = 0;
3712
3713 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003714 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003715
Willy Tarreau24657792010-02-26 10:30:28 +01003716 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003717 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003718 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003719 }
3720
3721 /* don't count other requests' data */
3722 s->logs.bytes_in -= s->req->l - s->req->send_max;
3723 s->logs.bytes_out -= s->rep->l - s->rep->send_max;
3724
3725 /* let's do a final log if we need it */
3726 if (s->logs.logwait &&
3727 !(s->flags & SN_MONITOR) &&
3728 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3729 s->do_log(s);
3730 }
3731
3732 s->logs.accept_date = date; /* user-visible date for logging */
3733 s->logs.tv_accept = now; /* corrected date for internal use */
3734 tv_zero(&s->logs.tv_request);
3735 s->logs.t_queue = -1;
3736 s->logs.t_connect = -1;
3737 s->logs.t_data = -1;
3738 s->logs.t_close = 0;
3739 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3740 s->logs.srv_queue_size = 0; /* we will get this number soon */
3741
3742 s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max;
3743 s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max;
3744
3745 if (s->pend_pos)
3746 pendconn_free(s->pend_pos);
3747
Willy Tarreau827aee92011-03-10 16:55:02 +01003748 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003749 if (s->flags & SN_CURR_SESS) {
3750 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003751 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003752 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003753 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3754 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003755 }
3756
Willy Tarreau9e000c62011-03-10 14:03:36 +01003757 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003758
3759 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3760 s->req->cons->fd = -1; /* just to help with debugging */
3761 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003762 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003763 s->req->cons->err_loc = NULL;
3764 s->req->cons->exp = TICK_ETERNITY;
3765 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003766 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3767 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 +02003768 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 +01003769 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3770 s->txn.meth = 0;
3771 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003772 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003773 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003774 s->req->cons->flags |= SI_FL_INDEP_STR;
3775
Willy Tarreau96e31212011-05-30 18:10:30 +02003776 if (s->fe->options2 & PR_O2_NODELAY) {
3777 s->req->flags |= BF_NEVER_WAIT;
3778 s->rep->flags |= BF_NEVER_WAIT;
3779 }
3780
Willy Tarreau610ecce2010-01-04 21:15:02 +01003781 /* if the request buffer is not empty, it means we're
3782 * about to process another request, so send pending
3783 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003784 * Just don't do this if the buffer is close to be full,
3785 * because the request will wait for it to flush a little
3786 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787 */
Willy Tarreau065e8332010-01-08 00:30:20 +01003788 if (s->req->l > s->req->send_max) {
3789 if (s->rep->send_max &&
3790 !(s->rep->flags & BF_FULL) &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003791 s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite)
3792 s->rep->flags |= BF_EXPECT_MORE;
3793 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003794
3795 /* we're removing the analysers, we MUST re-enable events detection */
3796 buffer_auto_read(s->req);
3797 buffer_auto_close(s->req);
3798 buffer_auto_read(s->rep);
3799 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003800
3801 /* make ->lr point to the first non-forwarded byte */
3802 s->req->lr = s->req->w + s->req->send_max;
3803 if (s->req->lr >= s->req->data + s->req->size)
3804 s->req->lr -= s->req->size;
3805 s->rep->lr = s->rep->w + s->rep->send_max;
3806 if (s->rep->lr >= s->rep->data + s->rep->size)
3807 s->rep->lr -= s->req->size;
3808
Willy Tarreau342b11c2010-11-24 16:22:09 +01003809 s->req->analysers = s->listener->analysers;
3810 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003811 s->rep->analysers = 0;
3812
3813 http_silent_debug(__LINE__, s);
3814}
3815
3816
3817/* This function updates the request state machine according to the response
3818 * state machine and buffer flags. It returns 1 if it changes anything (flag
3819 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3820 * it is only used to find when a request/response couple is complete. Both
3821 * this function and its equivalent should loop until both return zero. It
3822 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3823 */
3824int http_sync_req_state(struct session *s)
3825{
3826 struct buffer *buf = s->req;
3827 struct http_txn *txn = &s->txn;
3828 unsigned int old_flags = buf->flags;
3829 unsigned int old_state = txn->req.msg_state;
3830
3831 http_silent_debug(__LINE__, s);
3832 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3833 return 0;
3834
3835 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003836 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003837 * We can shut the read side unless we want to abort_on_close,
3838 * or we have a POST request. The issue with POST requests is
3839 * that some browsers still send a CRLF after the request, and
3840 * this CRLF must be read so that it does not remain in the kernel
3841 * buffers, otherwise a close could cause an RST on some systems
3842 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003843 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003844 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003845 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003846
3847 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3848 goto wait_other_side;
3849
3850 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3851 /* The server has not finished to respond, so we
3852 * don't want to move in order not to upset it.
3853 */
3854 goto wait_other_side;
3855 }
3856
3857 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3858 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003859 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003860 txn->req.msg_state = HTTP_MSG_TUNNEL;
3861 goto wait_other_side;
3862 }
3863
3864 /* When we get here, it means that both the request and the
3865 * response have finished receiving. Depending on the connection
3866 * mode, we'll have to wait for the last bytes to leave in either
3867 * direction, and sometimes for a close to be effective.
3868 */
3869
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003870 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3871 /* Server-close mode : queue a connection close to the server */
3872 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003873 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003874 }
3875 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3876 /* Option forceclose is set, or either side wants to close,
3877 * let's enforce it now that we're not expecting any new
3878 * data to come. The caller knows the session is complete
3879 * once both states are CLOSED.
3880 */
3881 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003882 buffer_shutr_now(buf);
3883 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003884 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003885 }
3886 else {
3887 /* The last possible modes are keep-alive and tunnel. Since tunnel
3888 * mode does not set the body analyser, we can't reach this place
3889 * in tunnel mode, so we're left with keep-alive only.
3890 * This mode is currently not implemented, we switch to tunnel mode.
3891 */
3892 buffer_auto_read(buf);
3893 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003894 }
3895
3896 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3897 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003898 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3899
Willy Tarreau610ecce2010-01-04 21:15:02 +01003900 if (!(buf->flags & BF_OUT_EMPTY)) {
3901 txn->req.msg_state = HTTP_MSG_CLOSING;
3902 goto http_msg_closing;
3903 }
3904 else {
3905 txn->req.msg_state = HTTP_MSG_CLOSED;
3906 goto http_msg_closed;
3907 }
3908 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003909 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003910 }
3911
3912 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3913 http_msg_closing:
3914 /* nothing else to forward, just waiting for the output buffer
3915 * to be empty and for the shutw_now to take effect.
3916 */
3917 if (buf->flags & BF_OUT_EMPTY) {
3918 txn->req.msg_state = HTTP_MSG_CLOSED;
3919 goto http_msg_closed;
3920 }
3921 else if (buf->flags & BF_SHUTW) {
3922 txn->req.msg_state = HTTP_MSG_ERROR;
3923 goto wait_other_side;
3924 }
3925 }
3926
3927 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3928 http_msg_closed:
3929 goto wait_other_side;
3930 }
3931
3932 wait_other_side:
3933 http_silent_debug(__LINE__, s);
3934 return txn->req.msg_state != old_state || buf->flags != old_flags;
3935}
3936
3937
3938/* This function updates the response state machine according to the request
3939 * state machine and buffer flags. It returns 1 if it changes anything (flag
3940 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3941 * it is only used to find when a request/response couple is complete. Both
3942 * this function and its equivalent should loop until both return zero. It
3943 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3944 */
3945int http_sync_res_state(struct session *s)
3946{
3947 struct buffer *buf = s->rep;
3948 struct http_txn *txn = &s->txn;
3949 unsigned int old_flags = buf->flags;
3950 unsigned int old_state = txn->rsp.msg_state;
3951
3952 http_silent_debug(__LINE__, s);
3953 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3954 return 0;
3955
3956 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3957 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003958 * still monitor the server connection for a possible close
3959 * while the request is being uploaded, so we don't disable
3960 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003961 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003962 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003963
3964 if (txn->req.msg_state == HTTP_MSG_ERROR)
3965 goto wait_other_side;
3966
3967 if (txn->req.msg_state < HTTP_MSG_DONE) {
3968 /* The client seems to still be sending data, probably
3969 * because we got an error response during an upload.
3970 * We have the choice of either breaking the connection
3971 * or letting it pass through. Let's do the later.
3972 */
3973 goto wait_other_side;
3974 }
3975
3976 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3977 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003978 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003979 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3980 goto wait_other_side;
3981 }
3982
3983 /* When we get here, it means that both the request and the
3984 * response have finished receiving. Depending on the connection
3985 * mode, we'll have to wait for the last bytes to leave in either
3986 * direction, and sometimes for a close to be effective.
3987 */
3988
3989 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3990 /* Server-close mode : shut read and wait for the request
3991 * side to close its output buffer. The caller will detect
3992 * when we're in DONE and the other is in CLOSED and will
3993 * catch that for the final cleanup.
3994 */
3995 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
3996 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003997 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003998 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3999 /* Option forceclose is set, or either side wants to close,
4000 * let's enforce it now that we're not expecting any new
4001 * data to come. The caller knows the session is complete
4002 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004004 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4005 buffer_shutr_now(buf);
4006 buffer_shutw_now(buf);
4007 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004008 }
4009 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004010 /* The last possible modes are keep-alive and tunnel. Since tunnel
4011 * mode does not set the body analyser, we can't reach this place
4012 * in tunnel mode, so we're left with keep-alive only.
4013 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004014 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004015 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004016 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017 }
4018
4019 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4020 /* if we've just closed an output, let's switch */
4021 if (!(buf->flags & BF_OUT_EMPTY)) {
4022 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4023 goto http_msg_closing;
4024 }
4025 else {
4026 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4027 goto http_msg_closed;
4028 }
4029 }
4030 goto wait_other_side;
4031 }
4032
4033 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4034 http_msg_closing:
4035 /* nothing else to forward, just waiting for the output buffer
4036 * to be empty and for the shutw_now to take effect.
4037 */
4038 if (buf->flags & BF_OUT_EMPTY) {
4039 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4040 goto http_msg_closed;
4041 }
4042 else if (buf->flags & BF_SHUTW) {
4043 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004044 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004045 if (target_srv(&s->target))
4046 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004047 goto wait_other_side;
4048 }
4049 }
4050
4051 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4052 http_msg_closed:
4053 /* drop any pending data */
4054 buffer_ignore(buf, buf->l - buf->send_max);
4055 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004056 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004057 goto wait_other_side;
4058 }
4059
4060 wait_other_side:
4061 http_silent_debug(__LINE__, s);
4062 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4063}
4064
4065
4066/* Resync the request and response state machines. Return 1 if either state
4067 * changes.
4068 */
4069int http_resync_states(struct session *s)
4070{
4071 struct http_txn *txn = &s->txn;
4072 int old_req_state = txn->req.msg_state;
4073 int old_res_state = txn->rsp.msg_state;
4074
4075 http_silent_debug(__LINE__, s);
4076 http_sync_req_state(s);
4077 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004078 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004079 if (!http_sync_res_state(s))
4080 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004081 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004082 if (!http_sync_req_state(s))
4083 break;
4084 }
4085 http_silent_debug(__LINE__, s);
4086 /* OK, both state machines agree on a compatible state.
4087 * There are a few cases we're interested in :
4088 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4089 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4090 * directions, so let's simply disable both analysers.
4091 * - HTTP_MSG_CLOSED on the response only means we must abort the
4092 * request.
4093 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4094 * with server-close mode means we've completed one request and we
4095 * must re-initialize the server connection.
4096 */
4097
4098 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4099 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4100 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4101 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4102 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004103 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004104 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004105 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004106 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004107 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004108 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004109 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4110 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004111 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004112 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004113 s->rep->analysers = 0;
4114 buffer_auto_close(s->rep);
4115 buffer_auto_read(s->rep);
4116 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004117 buffer_abort(s->req);
4118 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004119 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004120 buffer_ignore(s->req, s->req->l - s->req->send_max);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004121 }
4122 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4123 txn->rsp.msg_state == HTTP_MSG_DONE &&
4124 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4125 /* server-close: terminate this server connection and
4126 * reinitialize a fresh-new transaction.
4127 */
4128 http_end_txn_clean_session(s);
4129 }
4130
4131 http_silent_debug(__LINE__, s);
4132 return txn->req.msg_state != old_req_state ||
4133 txn->rsp.msg_state != old_res_state;
4134}
4135
Willy Tarreaud98cf932009-12-27 22:54:55 +01004136/* This function is an analyser which forwards request body (including chunk
4137 * sizes if any). It is called as soon as we must forward, even if we forward
4138 * zero byte. The only situation where it must not be called is when we're in
4139 * tunnel mode and we want to forward till the close. It's used both to forward
4140 * remaining data and to resync after end of body. It expects the msg_state to
4141 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4142 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004143 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004144 * bytes of pending data + the headers if not already done (between som and sov).
4145 * It eventually adjusts som to match sov after the data in between have been sent.
4146 */
4147int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4148{
4149 struct http_txn *txn = &s->txn;
4150 struct http_msg *msg = &s->txn.req;
4151
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004152 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4153 return 0;
4154
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004155 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
4156 ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004157 /* Output closed while we were sending data. We must abort and
4158 * wake the other side up.
4159 */
4160 msg->msg_state = HTTP_MSG_ERROR;
4161 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004162 return 1;
4163 }
4164
Willy Tarreau4fe41902010-06-07 22:27:41 +02004165 /* in most states, we should abort in case of early close */
4166 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004167
4168 /* Note that we don't have to send 100-continue back because we don't
4169 * need the data to complete our job, and it's up to the server to
4170 * decide whether to return 100, 417 or anything else in return of
4171 * an "Expect: 100-continue" header.
4172 */
4173
4174 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
4175 /* we have msg->col and msg->sov which both point to the first
4176 * byte of message body. msg->som still points to the beginning
4177 * of the message. We must save the body in req->lr because it
4178 * survives buffer re-alignments.
4179 */
4180 req->lr = req->data + msg->sov;
4181 if (txn->flags & TX_REQ_TE_CHNK)
4182 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4183 else {
4184 msg->msg_state = HTTP_MSG_DATA;
4185 }
4186 }
4187
Willy Tarreaud98cf932009-12-27 22:54:55 +01004188 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004189 int bytes;
4190
Willy Tarreau610ecce2010-01-04 21:15:02 +01004191 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004192 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004193 bytes = msg->sov - msg->som;
4194 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004195 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004196 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4197 bytes += req->size;
4198 msg->chunk_len += (unsigned int)bytes;
4199 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004200 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004201
Willy Tarreaucaabe412010-01-03 23:08:28 +01004202 if (msg->msg_state == HTTP_MSG_DATA) {
4203 /* must still forward */
4204 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004205 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004206
4207 /* nothing left to forward */
4208 if (txn->flags & TX_REQ_TE_CHNK)
4209 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004210 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004211 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004212 }
4213 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004214 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01004215 * set ->sov and ->lr to point to the body and switch to DATA or
4216 * TRAILERS state.
4217 */
4218 int ret = http_parse_chunk_size(req, msg);
4219
4220 if (!ret)
4221 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004222 else if (ret < 0) {
4223 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004224 if (msg->err_pos >= 0)
4225 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004226 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004227 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004228 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004229 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004230 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4231 /* we want the CRLF after the data */
4232 int ret;
4233
Willy Tarreaud3347ee2010-01-04 02:02:25 +01004234 req->lr = req->w + req->send_max;
4235 if (req->lr >= req->data + req->size)
4236 req->lr -= req->size;
4237
Willy Tarreaud98cf932009-12-27 22:54:55 +01004238 ret = http_skip_chunk_crlf(req, msg);
4239
4240 if (ret == 0)
4241 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004242 else if (ret < 0) {
4243 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004244 if (msg->err_pos >= 0)
4245 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004247 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004248 /* we're in MSG_CHUNK_SIZE now */
4249 }
4250 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4251 int ret = http_forward_trailers(req, msg);
4252
4253 if (ret == 0)
4254 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004255 else if (ret < 0) {
4256 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004257 if (msg->err_pos >= 0)
4258 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004259 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004260 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004261 /* we're in HTTP_MSG_DONE now */
4262 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004264 int old_state = msg->msg_state;
4265
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004267 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004268 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4269 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4270 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004271 if (http_resync_states(s)) {
4272 /* some state changes occurred, maybe the analyser
4273 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004274 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004275 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4276 if (req->flags & BF_SHUTW) {
4277 /* request errors are most likely due to
4278 * the server aborting the transfer.
4279 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004280 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004281 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004282 if (msg->err_pos >= 0)
4283 http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004285 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004287 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004288
4289 /* If "option abortonclose" is set on the backend, we
4290 * want to monitor the client's connection and forward
4291 * any shutdown notification to the server, which will
4292 * decide whether to close or to go on processing the
4293 * request.
4294 */
4295 if (s->be->options & PR_O_ABRT_CLOSE) {
4296 buffer_auto_read(req);
4297 buffer_auto_close(req);
4298 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004299 else if (s->txn.meth == HTTP_METH_POST) {
4300 /* POST requests may require to read extra CRLF
4301 * sent by broken browsers and which could cause
4302 * an RST to be sent upon close on some systems
4303 * (eg: Linux).
4304 */
4305 buffer_auto_read(req);
4306 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004307
Willy Tarreau610ecce2010-01-04 21:15:02 +01004308 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004309 }
4310 }
4311
Willy Tarreaud98cf932009-12-27 22:54:55 +01004312 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004313 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004314 if (req->flags & BF_SHUTR) {
4315 if (!(s->flags & SN_ERR_MASK))
4316 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004317 if (!(s->flags & SN_FINST_MASK)) {
4318 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4319 s->flags |= SN_FINST_H;
4320 else
4321 s->flags |= SN_FINST_D;
4322 }
4323
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004324 s->fe->fe_counters.cli_aborts++;
4325 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004326 if (target_srv(&s->target))
4327 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004328
4329 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004330 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004331
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004332 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004333 if (req->flags & BF_SHUTW)
4334 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004336 /* When TE: chunked is used, we need to get there again to parse remaining
4337 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4338 */
4339 if (txn->flags & TX_REQ_TE_CHNK)
4340 buffer_dont_close(req);
4341
Willy Tarreau5c620922011-05-11 19:56:11 +02004342 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004343 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4344 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004345 * modes are already handled by the stream sock layer. We must not do
4346 * this in content-length mode because it could present the MSG_MORE
4347 * flag with the last block of forwarded data, which would cause an
4348 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004349 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004350 if (txn->flags & TX_REQ_TE_CHNK)
4351 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004352
Willy Tarreau610ecce2010-01-04 21:15:02 +01004353 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004354 return 0;
4355
Willy Tarreaud98cf932009-12-27 22:54:55 +01004356 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004357 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004358 if (s->listener->counters)
4359 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004360 return_bad_req_stats_ok:
4361 txn->req.msg_state = HTTP_MSG_ERROR;
4362 if (txn->status) {
4363 /* Note: we don't send any error if some data were already sent */
4364 stream_int_retnclose(req->prod, NULL);
4365 } else {
4366 txn->status = 400;
4367 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4368 }
4369 req->analysers = 0;
4370 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004371
4372 if (!(s->flags & SN_ERR_MASK))
4373 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004374 if (!(s->flags & SN_FINST_MASK)) {
4375 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4376 s->flags |= SN_FINST_H;
4377 else
4378 s->flags |= SN_FINST_D;
4379 }
4380 return 0;
4381
4382 aborted_xfer:
4383 txn->req.msg_state = HTTP_MSG_ERROR;
4384 if (txn->status) {
4385 /* Note: we don't send any error if some data were already sent */
4386 stream_int_retnclose(req->prod, NULL);
4387 } else {
4388 txn->status = 502;
4389 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4390 }
4391 req->analysers = 0;
4392 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4393
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004394 s->fe->fe_counters.srv_aborts++;
4395 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004396 if (target_srv(&s->target))
4397 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004398
4399 if (!(s->flags & SN_ERR_MASK))
4400 s->flags |= SN_ERR_SRVCL;
4401 if (!(s->flags & SN_FINST_MASK)) {
4402 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4403 s->flags |= SN_FINST_H;
4404 else
4405 s->flags |= SN_FINST_D;
4406 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004407 return 0;
4408}
4409
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004410/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4411 * processing can continue on next analysers, or zero if it either needs more
4412 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4413 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4414 * when it has nothing left to do, and may remove any analyser when it wants to
4415 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004416 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004417int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004418{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004419 struct http_txn *txn = &s->txn;
4420 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004421 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004422 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004423 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004424 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004425
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004426 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 +02004427 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004428 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004429 rep,
4430 rep->rex, rep->wex,
4431 rep->flags,
4432 rep->l,
4433 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004434
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004435 /*
4436 * Now parse the partial (or complete) lines.
4437 * We will check the response syntax, and also join multi-line
4438 * headers. An index of all the lines will be elaborated while
4439 * parsing.
4440 *
4441 * For the parsing, we use a 28 states FSM.
4442 *
4443 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004444 * rep->data + msg->som = beginning of response
4445 * rep->data + msg->eoh = end of processed headers / start of current one
4446 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004447 * rep->lr = first non-visited byte
4448 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004449 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004450 */
4451
Willy Tarreau83e3af02009-12-28 17:39:57 +01004452 /* There's a protected area at the end of the buffer for rewriting
4453 * purposes. We don't want to start to parse the request if the
4454 * protected area is affected, because we may have to move processed
4455 * data later, which is much more complicated.
4456 */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004457 if (rep->l && msg->msg_state < HTTP_MSG_ERROR) {
4458 if (unlikely((rep->flags & BF_FULL) ||
4459 rep->r < rep->lr ||
4460 rep->r > rep->data + rep->size - global.tune.maxrewrite)) {
4461 if (rep->send_max) {
4462 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004463 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4464 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004465 buffer_dont_close(rep);
4466 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4467 return 0;
4468 }
4469 if (rep->l <= rep->size - global.tune.maxrewrite)
4470 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004471 }
4472
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004473 if (likely(rep->lr < rep->r))
4474 http_msg_analyzer(rep, msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004475 }
4476
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004477 /* 1: we might have to print this header in debug mode */
4478 if (unlikely((global.mode & MODE_DEBUG) &&
4479 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004480 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004481 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004482 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004483
Willy Tarreau663308b2010-06-07 14:06:08 +02004484 sol = rep->data + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004485 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004486 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004487
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004488 sol += hdr_idx_first_pos(&txn->hdr_idx);
4489 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004490
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004491 while (cur_idx) {
4492 eol = sol + txn->hdr_idx.v[cur_idx].len;
4493 debug_hdr("srvhdr", s, sol, eol);
4494 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4495 cur_idx = txn->hdr_idx.v[cur_idx].next;
4496 }
4497 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004498
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004499 /*
4500 * Now we quickly check if we have found a full valid response.
4501 * If not so, we check the FD and buffer states before leaving.
4502 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004503 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004504 * responses are checked first.
4505 *
4506 * Depending on whether the client is still there or not, we
4507 * may send an error response back or not. Note that normally
4508 * we should only check for HTTP status there, and check I/O
4509 * errors somewhere else.
4510 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004511
Willy Tarreau655dce92009-11-08 13:10:58 +01004512 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004513 /* Invalid response */
4514 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4515 /* we detected a parsing error. We want to archive this response
4516 * in the dedicated proxy area for later troubleshooting.
4517 */
4518 hdr_response_bad:
4519 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004520 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004521
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004522 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004523 if (target_srv(&s->target)) {
4524 target_srv(&s->target)->counters.failed_resp++;
4525 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004526 }
Willy Tarreau64648412010-03-05 10:41:54 +01004527 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004528 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004529 rep->analysers = 0;
4530 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004531 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004532 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004533 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4534
4535 if (!(s->flags & SN_ERR_MASK))
4536 s->flags |= SN_ERR_PRXCOND;
4537 if (!(s->flags & SN_FINST_MASK))
4538 s->flags |= SN_FINST_H;
4539
4540 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004541 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004542
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004543 /* too large response does not fit in buffer. */
4544 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004545 if (msg->err_pos < 0)
4546 msg->err_pos = rep->l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004547 goto hdr_response_bad;
4548 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004549
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004550 /* read error */
4551 else if (rep->flags & BF_READ_ERROR) {
4552 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004553 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004554
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004555 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004556 if (target_srv(&s->target)) {
4557 target_srv(&s->target)->counters.failed_resp++;
4558 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004559 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004560
Willy Tarreau90deb182010-01-07 00:20:41 +01004561 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004562 rep->analysers = 0;
4563 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004564 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004565 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004566 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004567
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004568 if (!(s->flags & SN_ERR_MASK))
4569 s->flags |= SN_ERR_SRVCL;
4570 if (!(s->flags & SN_FINST_MASK))
4571 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004572 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004573 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004574
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004575 /* read timeout : return a 504 to the client. */
4576 else if (rep->flags & BF_READ_TIMEOUT) {
4577 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004578 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004579
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004580 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004581 if (target_srv(&s->target)) {
4582 target_srv(&s->target)->counters.failed_resp++;
4583 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004584 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004585
Willy Tarreau90deb182010-01-07 00:20:41 +01004586 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004587 rep->analysers = 0;
4588 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004589 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004590 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004591 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004592
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004593 if (!(s->flags & SN_ERR_MASK))
4594 s->flags |= SN_ERR_SRVTO;
4595 if (!(s->flags & SN_FINST_MASK))
4596 s->flags |= SN_FINST_H;
4597 return 0;
4598 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004599
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004600 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004601 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004602 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004603 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004604
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004605 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004606 if (target_srv(&s->target)) {
4607 target_srv(&s->target)->counters.failed_resp++;
4608 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004609 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004610
Willy Tarreau90deb182010-01-07 00:20:41 +01004611 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 rep->analysers = 0;
4613 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004614 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004615 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004616 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004617
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004618 if (!(s->flags & SN_ERR_MASK))
4619 s->flags |= SN_ERR_SRVCL;
4620 if (!(s->flags & SN_FINST_MASK))
4621 s->flags |= SN_FINST_H;
4622 return 0;
4623 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004624
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004625 /* write error to client (we don't send any message then) */
4626 else if (rep->flags & BF_WRITE_ERROR) {
4627 if (msg->err_pos >= 0)
Willy Tarreau078272e2010-12-12 12:46:33 +01004628 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004629
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004630 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004632 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004633
4634 if (!(s->flags & SN_ERR_MASK))
4635 s->flags |= SN_ERR_CLICL;
4636 if (!(s->flags & SN_FINST_MASK))
4637 s->flags |= SN_FINST_H;
4638
4639 /* process_session() will take care of the error */
4640 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004641 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004642
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004643 buffer_dont_close(rep);
4644 return 0;
4645 }
4646
4647 /* More interesting part now : we know that we have a complete
4648 * response which at least looks like HTTP. We have an indicator
4649 * of each header's length, so we can parse them quickly.
4650 */
4651
4652 if (unlikely(msg->err_pos >= 0))
Willy Tarreau078272e2010-12-12 12:46:33 +01004653 http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004655 /*
4656 * 1: get the status code
4657 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01004658 n = msg->sol[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004659 if (n < 1 || n > 5)
4660 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004661 /* when the client triggers a 4xx from the server, it's most often due
4662 * to a missing object or permission. These events should be tracked
4663 * because if they happen often, it may indicate a brute force or a
4664 * vulnerability scan.
4665 */
4666 if (n == 4)
4667 session_inc_http_err_ctr(s);
4668
Willy Tarreau827aee92011-03-10 16:55:02 +01004669 if (target_srv(&s->target))
4670 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004671
Willy Tarreau5b154472009-12-21 20:11:07 +01004672 /* check if the response is HTTP/1.1 or above */
4673 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau962c3f42010-01-10 00:15:35 +01004674 ((msg->sol[5] > '1') ||
4675 ((msg->sol[5] == '1') &&
4676 (msg->sol[7] >= '1'))))
Willy Tarreau5b154472009-12-21 20:11:07 +01004677 txn->flags |= TX_RES_VER_11;
4678
4679 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004680 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 +01004681
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004682 /* transfer length unknown*/
4683 txn->flags &= ~TX_RES_XFER_LEN;
4684
Willy Tarreau962c3f42010-01-10 00:15:35 +01004685 txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004686
Willy Tarreau39650402010-03-15 19:44:39 +01004687 /* Adjust server's health based on status code. Note: status codes 501
4688 * and 505 are triggered on demand by client request, so we must not
4689 * count them as server failures.
4690 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004691 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004692 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004693 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004694 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004695 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004696 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004697
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004698 /*
4699 * 2: check for cacheability.
4700 */
4701
4702 switch (txn->status) {
4703 case 200:
4704 case 203:
4705 case 206:
4706 case 300:
4707 case 301:
4708 case 410:
4709 /* RFC2616 @13.4:
4710 * "A response received with a status code of
4711 * 200, 203, 206, 300, 301 or 410 MAY be stored
4712 * by a cache (...) unless a cache-control
4713 * directive prohibits caching."
4714 *
4715 * RFC2616 @9.5: POST method :
4716 * "Responses to this method are not cacheable,
4717 * unless the response includes appropriate
4718 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004719 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004720 if (likely(txn->meth != HTTP_METH_POST) &&
4721 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4722 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4723 break;
4724 default:
4725 break;
4726 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004727
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004728 /*
4729 * 3: we may need to capture headers
4730 */
4731 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004732 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau962c3f42010-01-10 00:15:35 +01004733 capture_headers(msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004734 txn->rsp.cap, s->fe->rsp_cap);
4735
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004736 /* 4: determine the transfer-length.
4737 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4738 * the presence of a message-body in a RESPONSE and its transfer length
4739 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004740 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004741 * All responses to the HEAD request method MUST NOT include a
4742 * message-body, even though the presence of entity-header fields
4743 * might lead one to believe they do. All 1xx (informational), 204
4744 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4745 * message-body. All other responses do include a message-body,
4746 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004747 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004748 * 1. Any response which "MUST NOT" include a message-body (such as the
4749 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4750 * always terminated by the first empty line after the header fields,
4751 * regardless of the entity-header fields present in the message.
4752 *
4753 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4754 * the "chunked" transfer-coding (Section 6.2) is used, the
4755 * transfer-length is defined by the use of this transfer-coding.
4756 * If a Transfer-Encoding header field is present and the "chunked"
4757 * transfer-coding is not present, the transfer-length is defined by
4758 * the sender closing the connection.
4759 *
4760 * 3. If a Content-Length header field is present, its decimal value in
4761 * OCTETs represents both the entity-length and the transfer-length.
4762 * If a message is received with both a Transfer-Encoding header
4763 * field and a Content-Length header field, the latter MUST be ignored.
4764 *
4765 * 4. If the message uses the media type "multipart/byteranges", and
4766 * the transfer-length is not otherwise specified, then this self-
4767 * delimiting media type defines the transfer-length. This media
4768 * type MUST NOT be used unless the sender knows that the recipient
4769 * can parse it; the presence in a request of a Range header with
4770 * multiple byte-range specifiers from a 1.1 client implies that the
4771 * client can parse multipart/byteranges responses.
4772 *
4773 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004774 */
4775
4776 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004777 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004778 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004779 * FIXME: should we parse anyway and return an error on chunked encoding ?
4780 */
4781 if (txn->meth == HTTP_METH_HEAD ||
4782 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004783 txn->status == 204 || txn->status == 304) {
4784 txn->flags |= TX_RES_XFER_LEN;
4785 goto skip_content_length;
4786 }
4787
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004788 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004789 ctx.idx = 0;
Willy Tarreau9e13c3c2009-12-22 09:59:58 +01004790 while ((txn->flags & TX_RES_VER_11) &&
4791 http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004792 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
4793 txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4794 else if (txn->flags & TX_RES_TE_CHNK) {
4795 /* bad transfer-encoding (chunked followed by something else) */
4796 use_close_only = 1;
4797 txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN);
4798 break;
4799 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004800 }
4801
4802 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4803 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004804 while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only &&
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004805 http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) {
4806 signed long long cl;
4807
Willy Tarreauad14f752011-09-02 20:33:27 +02004808 if (!ctx.vlen) {
4809 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004810 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004811 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004812
Willy Tarreauad14f752011-09-02 20:33:27 +02004813 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4814 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004815 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004816 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004817
Willy Tarreauad14f752011-09-02 20:33:27 +02004818 if (cl < 0) {
4819 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004820 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004821 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004822
Willy Tarreauad14f752011-09-02 20:33:27 +02004823 if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) {
4824 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004825 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004826 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004827
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004828 txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004829 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004830 }
4831
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004832 /* FIXME: we should also implement the multipart/byterange method.
4833 * For now on, we resort to close mode in this case (unknown length).
4834 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004835skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004836
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004837 /* end of job, return OK */
4838 rep->analysers &= ~an_bit;
4839 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004840 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004841 return 1;
4842}
4843
4844/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004845 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4846 * and updates t->rep->analysers. It might make sense to explode it into several
4847 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004848 */
4849int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4850{
4851 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004852 struct http_msg *msg = &txn->rsp;
4853 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004854 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004855
4856 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
4857 now_ms, __FUNCTION__,
4858 t,
4859 rep,
4860 rep->rex, rep->wex,
4861 rep->flags,
4862 rep->l,
4863 rep->analysers);
4864
Willy Tarreau655dce92009-11-08 13:10:58 +01004865 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004866 return 0;
4867
4868 rep->analysers &= ~an_bit;
4869 rep->analyse_exp = TICK_ETERNITY;
4870
Willy Tarreau5b154472009-12-21 20:11:07 +01004871 /* Now we have to check if we need to modify the Connection header.
4872 * This is more difficult on the response than it is on the request,
4873 * because we can have two different HTTP versions and we don't know
4874 * how the client will interprete a response. For instance, let's say
4875 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4876 * HTTP/1.1 response without any header. Maybe it will bound itself to
4877 * HTTP/1.0 because it only knows about it, and will consider the lack
4878 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4879 * the lack of header as a keep-alive. Thus we will use two flags
4880 * indicating how a request MAY be understood by the client. In case
4881 * of multiple possibilities, we'll fix the header to be explicit. If
4882 * ambiguous cases such as both close and keepalive are seen, then we
4883 * will fall back to explicit close. Note that we won't take risks with
4884 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004885 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004886 */
4887
Willy Tarreaudc008c52010-02-01 16:20:08 +01004888 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4889 txn->status == 101)) {
4890 /* Either we've established an explicit tunnel, or we're
4891 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004892 * to understand the next protocols. We have to switch to tunnel
4893 * mode, so that we transfer the request and responses then let
4894 * this protocol pass unmodified. When we later implement specific
4895 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004896 * header which contains information about that protocol for
4897 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004898 */
4899 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4900 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004901 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4902 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4903 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004904 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004905
Willy Tarreau60466522010-01-18 19:08:45 +01004906 /* on unknown transfer length, we must close */
4907 if (!(txn->flags & TX_RES_XFER_LEN) &&
4908 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4909 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004910
Willy Tarreau60466522010-01-18 19:08:45 +01004911 /* now adjust header transformations depending on current state */
4912 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4913 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4914 to_del |= 2; /* remove "keep-alive" on any response */
4915 if (!(txn->flags & TX_RES_VER_11))
4916 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004917 }
Willy Tarreau60466522010-01-18 19:08:45 +01004918 else { /* SCL / KAL */
4919 to_del |= 1; /* remove "close" on any response */
4920 if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11))
4921 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004922 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004923
Willy Tarreau60466522010-01-18 19:08:45 +01004924 /* Parse and remove some headers from the connection header */
4925 http_parse_connection_header(txn, msg, rep, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004926
Willy Tarreau60466522010-01-18 19:08:45 +01004927 /* Some keep-alive responses are converted to Server-close if
4928 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004929 */
Willy Tarreau60466522010-01-18 19:08:45 +01004930 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4931 if ((txn->flags & TX_HDR_CONN_CLO) ||
4932 (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0)
4933 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004934 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004935 }
4936
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004937 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004938 /*
4939 * 3: we will have to evaluate the filters.
4940 * As opposed to version 1.2, now they will be evaluated in the
4941 * filters order and not in the header order. This means that
4942 * each filter has to be validated among all headers.
4943 *
4944 * Filters are tried with ->be first, then with ->fe if it is
4945 * different from ->be.
4946 */
4947
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004948 cur_proxy = t->be;
4949 while (1) {
4950 struct proxy *rule_set = cur_proxy;
4951
4952 /* try headers filters */
4953 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004954 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004955 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004956 if (target_srv(&t->target)) {
4957 target_srv(&t->target)->counters.failed_resp++;
4958 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004959 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004960 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004961 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004962 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004963 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004964 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau0b89fbb2010-02-02 09:57:24 +01004965 buffer_ignore(rep, rep->l - rep->send_max);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004966 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004967 if (!(t->flags & SN_ERR_MASK))
4968 t->flags |= SN_ERR_PRXCOND;
4969 if (!(t->flags & SN_FINST_MASK))
4970 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004971 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004972 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004973 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004974
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004975 /* has the response been denied ? */
4976 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004977 if (target_srv(&t->target))
4978 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004979
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004980 t->be->be_counters.denied_resp++;
4981 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004982 if (t->listener->counters)
4983 t->listener->counters->denied_resp++;
4984
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004985 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004986 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004987
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004988 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004989 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004990 if (txn->status < 200)
4991 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004992 if (wl->cond) {
4993 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4994 ret = acl_pass(ret);
4995 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4996 ret = !ret;
4997 if (!ret)
4998 continue;
4999 }
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005000 if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005001 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005002 }
5003
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005004 /* check whether we're already working on the frontend */
5005 if (cur_proxy == t->fe)
5006 break;
5007 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005008 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005009
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005010 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005011 * We may be facing a 100-continue response, in which case this
5012 * is not the right response, and we're waiting for the next one.
5013 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005014 * next one.
5015 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005016 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005017 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau962c3f42010-01-10 00:15:35 +01005018 buffer_forward(rep, rep->lr - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005019 msg->msg_state = HTTP_MSG_RPBEFORE;
5020 txn->status = 0;
5021 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5022 return 1;
5023 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005024 else if (unlikely(txn->status < 200))
5025 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005026
5027 /* we don't have any 1xx status code now */
5028
5029 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005030 * 4: check for server cookie.
5031 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005032 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5033 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005034 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005035
Willy Tarreaubaaee002006-06-26 02:48:02 +02005036
Willy Tarreaua15645d2007-03-18 16:22:39 +01005037 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005038 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005039 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005040 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005041 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005042
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005043 /*
5044 * 6: add server cookie in the response if needed
5045 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005046 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005047 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005048 (!(t->flags & SN_DIRECT) ||
5049 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5050 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5051 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5052 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005053 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5054 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005055 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005056 /* the server is known, it's not the one the client requested, or the
5057 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005058 * insert a set-cookie here, except if we want to insert only on POST
5059 * requests and this one isn't. Note that servers which don't have cookies
5060 * (eg: some backup servers) will return a full cookie removal request.
5061 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005062 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005063 len = sprintf(trash,
5064 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5065 t->be->cookie_name);
5066 }
5067 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005068 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005069
5070 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5071 /* emit last_date, which is mandatory */
5072 trash[len++] = COOKIE_DELIM_DATE;
5073 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5074 if (t->be->cookie_maxlife) {
5075 /* emit first_date, which is either the original one or
5076 * the current date.
5077 */
5078 trash[len++] = COOKIE_DELIM_DATE;
5079 s30tob64(txn->cookie_first_date ?
5080 txn->cookie_first_date >> 2 :
5081 (date.tv_sec+3) >> 2, trash + len);
5082 len += 5;
5083 }
5084 }
5085 len += sprintf(trash + len, "; path=/");
5086 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005087
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005088 if (t->be->cookie_domain)
5089 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005090
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005091 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005092 trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005093 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005094
Willy Tarreauf1348312010-10-07 15:54:11 +02005095 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005096 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005097 /* the server did not change, only the date was updated */
5098 txn->flags |= TX_SCK_UPDATED;
5099 else
5100 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005101
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005102 /* Here, we will tell an eventual cache on the client side that we don't
5103 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5104 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5105 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5106 */
5107 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005108
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005109 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5110
5111 if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005112 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005113 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005114 }
5115 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005116
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005117 /*
5118 * 7: check if result will be cacheable with a cookie.
5119 * We'll block the response if security checks have caught
5120 * nasty things such as a cacheable cookie.
5121 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005122 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5123 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005124 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005125
5126 /* we're in presence of a cacheable response containing
5127 * a set-cookie header. We'll block it as requested by
5128 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005129 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005130 if (target_srv(&t->target))
5131 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005132
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005133 t->be->be_counters.denied_resp++;
5134 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005135 if (t->listener->counters)
5136 t->listener->counters->denied_resp++;
5137
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005138 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005139 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005140 send_log(t->be, LOG_ALERT,
5141 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005142 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005143 goto return_srv_prx_502;
5144 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005145
5146 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005147 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005148 */
Willy Tarreau60466522010-01-18 19:08:45 +01005149 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5150 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5151 unsigned int want_flags = 0;
5152
5153 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5154 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5155 /* we want a keep-alive response here. Keep-alive header
5156 * required if either side is not 1.1.
5157 */
5158 if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11))
5159 want_flags |= TX_CON_KAL_SET;
5160 }
5161 else {
5162 /* we want a close response here. Close header required if
5163 * the server is 1.1, regardless of the client.
5164 */
5165 if (txn->flags & TX_RES_VER_11)
5166 want_flags |= TX_CON_CLO_SET;
5167 }
5168
5169 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
5170 http_change_connection_header(txn, msg, rep, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005172
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005173 skip_header_mangling:
Willy Tarreaudc008c52010-02-01 16:20:08 +01005174 if ((txn->flags & TX_RES_XFER_LEN) ||
5175 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005176 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005177
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005178 /*************************************************************
5179 * OK, that's finished for the headers. We have done what we *
5180 * could. Let's switch to the DATA state. *
5181 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005182
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005183 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005184
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005185 /* if the user wants to log as soon as possible, without counting
5186 * bytes from the server, then this is the right moment. We have
5187 * to temporarily assign bytes_out to log what we currently have.
5188 */
5189 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5190 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5191 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005192 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005193 t->logs.bytes_out = 0;
5194 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005195
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005196 /* Note: we must not try to cheat by jumping directly to DATA,
5197 * otherwise we would not let the client side wake up.
5198 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005199
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005200 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005201 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005202 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005203}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005204
Willy Tarreaud98cf932009-12-27 22:54:55 +01005205/* This function is an analyser which forwards response body (including chunk
5206 * sizes if any). It is called as soon as we must forward, even if we forward
5207 * zero byte. The only situation where it must not be called is when we're in
5208 * tunnel mode and we want to forward till the close. It's used both to forward
5209 * remaining data and to resync after end of body. It expects the msg_state to
5210 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5211 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005212 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005213 * bytes of pending data + the headers if not already done (between som and sov).
5214 * It eventually adjusts som to match sov after the data in between have been sent.
5215 */
5216int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5217{
5218 struct http_txn *txn = &s->txn;
5219 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005220 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005221
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005222 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5223 return 0;
5224
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005225 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01005226 ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005227 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005228 /* Output closed while we were sending data. We must abort and
5229 * wake the other side up.
5230 */
5231 msg->msg_state = HTTP_MSG_ERROR;
5232 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005233 return 1;
5234 }
5235
Willy Tarreau4fe41902010-06-07 22:27:41 +02005236 /* in most states, we should abort in case of early close */
5237 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005238
Willy Tarreaud98cf932009-12-27 22:54:55 +01005239 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
5240 /* we have msg->col and msg->sov which both point to the first
5241 * byte of message body. msg->som still points to the beginning
5242 * of the message. We must save the body in req->lr because it
5243 * survives buffer re-alignments.
5244 */
5245 res->lr = res->data + msg->sov;
5246 if (txn->flags & TX_RES_TE_CHNK)
5247 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5248 else {
5249 msg->msg_state = HTTP_MSG_DATA;
5250 }
5251 }
5252
Willy Tarreaud98cf932009-12-27 22:54:55 +01005253 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005254 int bytes;
5255
Willy Tarreau610ecce2010-01-04 21:15:02 +01005256 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005257 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005258 bytes = msg->sov - msg->som;
5259 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005260 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005261 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5262 bytes += res->size;
5263 msg->chunk_len += (unsigned int)bytes;
5264 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005265 }
5266
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005267
Willy Tarreaucaabe412010-01-03 23:08:28 +01005268 if (msg->msg_state == HTTP_MSG_DATA) {
5269 /* must still forward */
5270 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005271 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005272
5273 /* nothing left to forward */
5274 if (txn->flags & TX_RES_TE_CHNK)
5275 msg->msg_state = HTTP_MSG_DATA_CRLF;
5276 else
5277 msg->msg_state = HTTP_MSG_DONE;
5278 }
5279 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005280 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaud98cf932009-12-27 22:54:55 +01005281 * set ->sov to point to the body and switch to DATA or TRAILERS state.
5282 */
5283 int ret = http_parse_chunk_size(res, msg);
5284
5285 if (!ret)
5286 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005287 else if (ret < 0) {
5288 if (msg->err_pos >= 0)
5289 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005290 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005291 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005292 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005293 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005294 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5295 /* we want the CRLF after the data */
5296 int ret;
5297
Willy Tarreaud3347ee2010-01-04 02:02:25 +01005298 res->lr = res->w + res->send_max;
5299 if (res->lr >= res->data + res->size)
5300 res->lr -= res->size;
5301
Willy Tarreaud98cf932009-12-27 22:54:55 +01005302 ret = http_skip_chunk_crlf(res, msg);
5303
5304 if (!ret)
5305 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005306 else if (ret < 0) {
5307 if (msg->err_pos >= 0)
5308 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005309 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005310 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005311 /* we're in MSG_CHUNK_SIZE now */
5312 }
5313 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5314 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005315
Willy Tarreaud98cf932009-12-27 22:54:55 +01005316 if (ret == 0)
5317 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005318 else if (ret < 0) {
5319 if (msg->err_pos >= 0)
5320 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005321 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005322 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005323 /* we're in HTTP_MSG_DONE now */
5324 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005325 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005326 int old_state = msg->msg_state;
5327
Willy Tarreau610ecce2010-01-04 21:15:02 +01005328 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005329 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005330 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5331 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5332 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005333 if (http_resync_states(s)) {
5334 http_silent_debug(__LINE__, s);
5335 /* some state changes occurred, maybe the analyser
5336 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005337 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005338 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5339 if (res->flags & BF_SHUTW) {
5340 /* response errors are most likely due to
5341 * the client aborting the transfer.
5342 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005343 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005344 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005345 if (msg->err_pos >= 0)
5346 http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005348 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005350 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005351 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005352 }
5353 }
5354
Willy Tarreaud98cf932009-12-27 22:54:55 +01005355 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005356 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005357 if (res->flags & BF_SHUTR) {
5358 if (!(s->flags & SN_ERR_MASK))
5359 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005360 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005361 if (target_srv(&s->target))
5362 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005363 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005364 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005365
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005366 if (res->flags & BF_SHUTW)
5367 goto aborted_xfer;
5368
Willy Tarreau40dba092010-03-04 18:14:51 +01005369 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005370 if (!s->req->analysers)
5371 goto return_bad_res;
5372
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005373 /* forward any pending data */
5374 bytes = msg->sov - msg->som;
5375 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005376 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005377 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5378 bytes += res->size;
5379 msg->chunk_len += (unsigned int)bytes;
5380 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005381 }
5382
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005383 /* When TE: chunked is used, we need to get there again to parse remaining
5384 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5385 * Similarly, with keep-alive on the client side, we don't want to forward a
5386 * close.
5387 */
5388 if ((txn->flags & TX_RES_TE_CHNK) ||
5389 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5390 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5391 buffer_dont_close(res);
5392
Willy Tarreau5c620922011-05-11 19:56:11 +02005393 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005394 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5395 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005396 * modes are already handled by the stream sock layer. We must not do
5397 * this in content-length mode because it could present the MSG_MORE
5398 * flag with the last block of forwarded data, which would cause an
5399 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005400 */
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005401 if (txn->flags & TX_RES_TE_CHNK)
5402 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005403
Willy Tarreaud98cf932009-12-27 22:54:55 +01005404 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005405 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005406 return 0;
5407
Willy Tarreau40dba092010-03-04 18:14:51 +01005408 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005409 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005410 if (target_srv(&s->target))
5411 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005412
5413 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005414 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005415 /* don't send any error message as we're in the body */
5416 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005417 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005418 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005419 if (target_srv(&s->target))
5420 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005421
5422 if (!(s->flags & SN_ERR_MASK))
5423 s->flags |= SN_ERR_PRXCOND;
5424 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005425 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005426 return 0;
5427
5428 aborted_xfer:
5429 txn->rsp.msg_state = HTTP_MSG_ERROR;
5430 /* don't send any error message as we're in the body */
5431 stream_int_retnclose(res->cons, NULL);
5432 res->analysers = 0;
5433 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5434
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005435 s->fe->fe_counters.cli_aborts++;
5436 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005437 if (target_srv(&s->target))
5438 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005439
5440 if (!(s->flags & SN_ERR_MASK))
5441 s->flags |= SN_ERR_CLICL;
5442 if (!(s->flags & SN_FINST_MASK))
5443 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005444 return 0;
5445}
5446
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005447/* Iterate the same filter through all request headers.
5448 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005449 * Since it can manage the switch to another backend, it updates the per-proxy
5450 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005451 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005452int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005453{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005454 char term;
5455 char *cur_ptr, *cur_end, *cur_next;
5456 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005457 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005458 struct hdr_idx_elem *cur_hdr;
5459 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005460
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005461 last_hdr = 0;
5462
Willy Tarreau962c3f42010-01-10 00:15:35 +01005463 cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005464 old_idx = 0;
5465
5466 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005467 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005468 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005469 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005470 (exp->action == ACT_ALLOW ||
5471 exp->action == ACT_DENY ||
5472 exp->action == ACT_TARPIT))
5473 return 0;
5474
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005475 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005476 if (!cur_idx)
5477 break;
5478
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005479 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005480 cur_ptr = cur_next;
5481 cur_end = cur_ptr + cur_hdr->len;
5482 cur_next = cur_end + cur_hdr->cr + 1;
5483
5484 /* Now we have one header between cur_ptr and cur_end,
5485 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005486 */
5487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005488 /* The annoying part is that pattern matching needs
5489 * that we modify the contents to null-terminate all
5490 * strings before testing them.
5491 */
5492
5493 term = *cur_end;
5494 *cur_end = '\0';
5495
5496 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5497 switch (exp->action) {
5498 case ACT_SETBE:
5499 /* It is not possible to jump a second time.
5500 * FIXME: should we return an HTTP/500 here so that
5501 * the admin knows there's a problem ?
5502 */
5503 if (t->be != t->fe)
5504 break;
5505
5506 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005507 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005508 last_hdr = 1;
5509 break;
5510
5511 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005512 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005513 last_hdr = 1;
5514 break;
5515
5516 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005517 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005518 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005519
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005520 t->fe->fe_counters.denied_req++;
5521 if (t->fe != t->be)
5522 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005523 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005524 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005526 break;
5527
5528 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005529 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005530 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005531
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005532 t->fe->fe_counters.denied_req++;
5533 if (t->fe != t->be)
5534 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005535 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005536 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005537
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005538 break;
5539
5540 case ACT_REPLACE:
5541 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5542 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5543 /* FIXME: if the user adds a newline in the replacement, the
5544 * index will not be recalculated for now, and the new line
5545 * will not be counted as a new header.
5546 */
5547
5548 cur_end += delta;
5549 cur_next += delta;
5550 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005551 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005552 break;
5553
5554 case ACT_REMOVE:
5555 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5556 cur_next += delta;
5557
Willy Tarreaufa355d42009-11-29 18:12:29 +01005558 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005559 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5560 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005561 cur_hdr->len = 0;
5562 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005563 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005564 break;
5565
5566 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005567 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005568 if (cur_end)
5569 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005570
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005571 /* keep the link from this header to next one in case of later
5572 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005573 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 old_idx = cur_idx;
5575 }
5576 return 0;
5577}
5578
5579
5580/* Apply the filter to the request line.
5581 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5582 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005583 * Since it can manage the switch to another backend, it updates the per-proxy
5584 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005585 */
5586int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5587{
5588 char term;
5589 char *cur_ptr, *cur_end;
5590 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005591 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005592 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005593
Willy Tarreau58f10d72006-12-04 02:26:12 +01005594
Willy Tarreau3d300592007-03-18 18:34:41 +01005595 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005596 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005597 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005598 (exp->action == ACT_ALLOW ||
5599 exp->action == ACT_DENY ||
5600 exp->action == ACT_TARPIT))
5601 return 0;
5602 else if (exp->action == ACT_REMOVE)
5603 return 0;
5604
5605 done = 0;
5606
Willy Tarreau962c3f42010-01-10 00:15:35 +01005607 cur_ptr = txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005608 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005609
5610 /* Now we have the request line between cur_ptr and cur_end */
5611
5612 /* The annoying part is that pattern matching needs
5613 * that we modify the contents to null-terminate all
5614 * strings before testing them.
5615 */
5616
5617 term = *cur_end;
5618 *cur_end = '\0';
5619
5620 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5621 switch (exp->action) {
5622 case ACT_SETBE:
5623 /* It is not possible to jump a second time.
5624 * FIXME: should we return an HTTP/500 here so that
5625 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005626 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005627 if (t->be != t->fe)
5628 break;
5629
5630 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005631 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005632 done = 1;
5633 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005634
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005635 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005636 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005637 done = 1;
5638 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005639
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005641 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005642
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005643 t->fe->fe_counters.denied_req++;
5644 if (t->fe != t->be)
5645 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005646 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005647 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005648
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005649 done = 1;
5650 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005651
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005652 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005653 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005654
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005655 t->fe->fe_counters.denied_req++;
5656 if (t->fe != t->be)
5657 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005658 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005659 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005660
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005661 done = 1;
5662 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005663
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005664 case ACT_REPLACE:
5665 *cur_end = term; /* restore the string terminator */
5666 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5667 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5668 /* FIXME: if the user adds a newline in the replacement, the
5669 * index will not be recalculated for now, and the new line
5670 * will not be counted as a new header.
5671 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005672
Willy Tarreaufa355d42009-11-29 18:12:29 +01005673 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 cur_end += delta;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005675 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005676 HTTP_MSG_RQMETH,
5677 cur_ptr, cur_end + 1,
5678 NULL, NULL);
5679 if (unlikely(!cur_end))
5680 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005681
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005682 /* we have a full request and we know that we have either a CR
5683 * or an LF at <ptr>.
5684 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005685 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5686 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005687 /* there is no point trying this regex on headers */
5688 return 1;
5689 }
5690 }
5691 *cur_end = term; /* restore the string terminator */
5692 return done;
5693}
Willy Tarreau97de6242006-12-27 17:18:38 +01005694
Willy Tarreau58f10d72006-12-04 02:26:12 +01005695
Willy Tarreau58f10d72006-12-04 02:26:12 +01005696
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005697/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005698 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005699 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005700 * unparsable request. Since it can manage the switch to another backend, it
5701 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005702 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005703int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005704{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005705 struct http_txn *txn = &s->txn;
5706 struct hdr_exp *exp;
5707
5708 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005710
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005711 /*
5712 * The interleaving of transformations and verdicts
5713 * makes it difficult to decide to continue or stop
5714 * the evaluation.
5715 */
5716
Willy Tarreau6c123b12010-01-28 20:22:06 +01005717 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5718 break;
5719
Willy Tarreau3d300592007-03-18 18:34:41 +01005720 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005721 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005722 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005723 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005724
5725 /* if this filter had a condition, evaluate it now and skip to
5726 * next filter if the condition does not match.
5727 */
5728 if (exp->cond) {
5729 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5730 ret = acl_pass(ret);
5731 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5732 ret = !ret;
5733
5734 if (!ret)
5735 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005736 }
5737
5738 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005739 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005740 if (unlikely(ret < 0))
5741 return -1;
5742
5743 if (likely(ret == 0)) {
5744 /* The filter did not match the request, it can be
5745 * iterated through all headers.
5746 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005747 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005748 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005749 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005750 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005751}
5752
5753
Willy Tarreaua15645d2007-03-18 16:22:39 +01005754
Willy Tarreau58f10d72006-12-04 02:26:12 +01005755/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005756 * Try to retrieve the server associated to the appsession.
5757 * If the server is found, it's assigned to the session.
5758 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005759void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005760 struct http_txn *txn = &t->txn;
5761 appsess *asession = NULL;
5762 char *sessid_temp = NULL;
5763
Cyril Bontéb21570a2009-11-29 20:04:48 +01005764 if (len > t->be->appsession_len) {
5765 len = t->be->appsession_len;
5766 }
5767
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005768 if (t->be->options2 & PR_O2_AS_REQL) {
5769 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005770 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005771 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005772 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005773 }
5774
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005775 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005776 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5777 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5778 return;
5779 }
5780
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005781 memcpy(txn->sessid, buf, len);
5782 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005783 }
5784
5785 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5786 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5787 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5788 return;
5789 }
5790
Cyril Bontéb21570a2009-11-29 20:04:48 +01005791 memcpy(sessid_temp, buf, len);
5792 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005793
5794 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5795 /* free previously allocated memory */
5796 pool_free2(apools.sessid, sessid_temp);
5797
5798 if (asession != NULL) {
5799 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5800 if (!(t->be->options2 & PR_O2_AS_REQL))
5801 asession->request_count++;
5802
5803 if (asession->serverid != NULL) {
5804 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005805
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005806 while (srv) {
5807 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005808 if ((srv->state & SRV_RUNNING) ||
5809 (t->be->options & PR_O_PERSIST) ||
5810 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005811 /* we found the server and it's usable */
5812 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005813 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005814 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005815 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005816
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005817 break;
5818 } else {
5819 txn->flags &= ~TX_CK_MASK;
5820 txn->flags |= TX_CK_DOWN;
5821 }
5822 }
5823 srv = srv->next;
5824 }
5825 }
5826 }
5827}
5828
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005829/* Find the end of a cookie value contained between <s> and <e>. It works the
5830 * same way as with headers above except that the semi-colon also ends a token.
5831 * See RFC2965 for more information. Note that it requires a valid header to
5832 * return a valid result.
5833 */
5834char *find_cookie_value_end(char *s, const char *e)
5835{
5836 int quoted, qdpair;
5837
5838 quoted = qdpair = 0;
5839 for (; s < e; s++) {
5840 if (qdpair) qdpair = 0;
5841 else if (quoted) {
5842 if (*s == '\\') qdpair = 1;
5843 else if (*s == '"') quoted = 0;
5844 }
5845 else if (*s == '"') quoted = 1;
5846 else if (*s == ',' || *s == ';') return s;
5847 }
5848 return s;
5849}
5850
5851/* Delete a value in a header between delimiters <from> and <next> in buffer
5852 * <buf>. The number of characters displaced is returned, and the pointer to
5853 * the first delimiter is updated if required. The function tries as much as
5854 * possible to respect the following principles :
5855 * - replace <from> delimiter by the <next> one unless <from> points to a
5856 * colon, in which case <next> is simply removed
5857 * - set exactly one space character after the new first delimiter, unless
5858 * there are not enough characters in the block being moved to do so.
5859 * - remove unneeded spaces before the previous delimiter and after the new
5860 * one.
5861 *
5862 * It is the caller's responsibility to ensure that :
5863 * - <from> points to a valid delimiter or the colon ;
5864 * - <next> points to a valid delimiter or the final CR/LF ;
5865 * - there are non-space chars before <from> ;
5866 * - there is a CR/LF at or after <next>.
5867 */
5868int del_hdr_value(struct buffer *buf, char **from, char *next)
5869{
5870 char *prev = *from;
5871
5872 if (*prev == ':') {
5873 /* We're removing the first value, preserve the colon and add a
5874 * space if possible.
5875 */
5876 if (!http_is_crlf[(unsigned char)*next])
5877 next++;
5878 prev++;
5879 if (prev < next)
5880 *prev++ = ' ';
5881
5882 while (http_is_spht[(unsigned char)*next])
5883 next++;
5884 } else {
5885 /* Remove useless spaces before the old delimiter. */
5886 while (http_is_spht[(unsigned char)*(prev-1)])
5887 prev--;
5888 *from = prev;
5889
5890 /* copy the delimiter and if possible a space if we're
5891 * not at the end of the line.
5892 */
5893 if (!http_is_crlf[(unsigned char)*next]) {
5894 *prev++ = *next++;
5895 if (prev + 1 < next)
5896 *prev++ = ' ';
5897 while (http_is_spht[(unsigned char)*next])
5898 next++;
5899 }
5900 }
5901 return buffer_replace2(buf, prev, next, NULL, 0);
5902}
5903
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005904/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005905 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005906 * desirable to call it only when needed. This code is quite complex because
5907 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5908 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005909 */
5910void manage_client_side_cookies(struct session *t, struct buffer *req)
5911{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005912 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005913 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005914 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005915 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5916 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005917
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005918 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005919 old_idx = 0;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005920 hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005921
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005922 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005923 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005924 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005926 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005927 hdr_beg = hdr_next;
5928 hdr_end = hdr_beg + cur_hdr->len;
5929 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005930
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005931 /* We have one full header between hdr_beg and hdr_end, and the
5932 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933 * "Cookie:" headers.
5934 */
5935
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005936 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005937 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005938 old_idx = cur_idx;
5939 continue;
5940 }
5941
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005942 del_from = NULL; /* nothing to be deleted */
5943 preserve_hdr = 0; /* assume we may kill the whole header */
5944
Willy Tarreau58f10d72006-12-04 02:26:12 +01005945 /* Now look for cookies. Conforming to RFC2109, we have to support
5946 * attributes whose name begin with a '$', and associate them with
5947 * the right cookie, if we want to delete this cookie.
5948 * So there are 3 cases for each cookie read :
5949 * 1) it's a special attribute, beginning with a '$' : ignore it.
5950 * 2) it's a server id cookie that we *MAY* want to delete : save
5951 * some pointers on it (last semi-colon, beginning of cookie...)
5952 * 3) it's an application cookie : we *MAY* have to delete a previous
5953 * "special" cookie.
5954 * At the end of loop, if a "special" cookie remains, we may have to
5955 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005956 * *MUST* delete it.
5957 *
5958 * Note: RFC2965 is unclear about the processing of spaces around
5959 * the equal sign in the ATTR=VALUE form. A careful inspection of
5960 * the RFC explicitly allows spaces before it, and not within the
5961 * tokens (attrs or values). An inspection of RFC2109 allows that
5962 * too but section 10.1.3 lets one think that spaces may be allowed
5963 * after the equal sign too, resulting in some (rare) buggy
5964 * implementations trying to do that. So let's do what servers do.
5965 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5966 * allowed quoted strings in values, with any possible character
5967 * after a backslash, including control chars and delimitors, which
5968 * causes parsing to become ambiguous. Browsers also allow spaces
5969 * within values even without quotes.
5970 *
5971 * We have to keep multiple pointers in order to support cookie
5972 * removal at the beginning, middle or end of header without
5973 * corrupting the header. All of these headers are valid :
5974 *
5975 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5976 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5977 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5978 * | | | | | | | | |
5979 * | | | | | | | | hdr_end <--+
5980 * | | | | | | | +--> next
5981 * | | | | | | +----> val_end
5982 * | | | | | +-----------> val_beg
5983 * | | | | +--------------> equal
5984 * | | | +----------------> att_end
5985 * | | +---------------------> att_beg
5986 * | +--------------------------> prev
5987 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005988 */
5989
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005990 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5991 /* Iterate through all cookies on this line */
5992
5993 /* find att_beg */
5994 att_beg = prev + 1;
5995 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5996 att_beg++;
5997
5998 /* find att_end : this is the first character after the last non
5999 * space before the equal. It may be equal to hdr_end.
6000 */
6001 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006002
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006003 while (equal < hdr_end) {
6004 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006005 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006006 if (http_is_spht[(unsigned char)*equal++])
6007 continue;
6008 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006009 }
6010
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006011 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6012 * is between <att_beg> and <equal>, both may be identical.
6013 */
6014
6015 /* look for end of cookie if there is an equal sign */
6016 if (equal < hdr_end && *equal == '=') {
6017 /* look for the beginning of the value */
6018 val_beg = equal + 1;
6019 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6020 val_beg++;
6021
6022 /* find the end of the value, respecting quotes */
6023 next = find_cookie_value_end(val_beg, hdr_end);
6024
6025 /* make val_end point to the first white space or delimitor after the value */
6026 val_end = next;
6027 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6028 val_end--;
6029 } else {
6030 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006031 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006032
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006033 /* We have nothing to do with attributes beginning with '$'. However,
6034 * they will automatically be removed if a header before them is removed,
6035 * since they're supposed to be linked together.
6036 */
6037 if (*att_beg == '$')
6038 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006039
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006040 /* Ignore cookies with no equal sign */
6041 if (equal == next) {
6042 /* This is not our cookie, so we must preserve it. But if we already
6043 * scheduled another cookie for removal, we cannot remove the
6044 * complete header, but we can remove the previous block itself.
6045 */
6046 preserve_hdr = 1;
6047 if (del_from != NULL) {
6048 int delta = del_hdr_value(req, &del_from, prev);
6049 val_end += delta;
6050 next += delta;
6051 hdr_end += delta;
6052 hdr_next += delta;
6053 cur_hdr->len += delta;
6054 http_msg_move_end(&txn->req, delta);
6055 prev = del_from;
6056 del_from = NULL;
6057 }
6058 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006059 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006060
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006061 /* if there are spaces around the equal sign, we need to
6062 * strip them otherwise we'll get trouble for cookie captures,
6063 * or even for rewrites. Since this happens extremely rarely,
6064 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006065 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006066 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6067 int stripped_before = 0;
6068 int stripped_after = 0;
6069
6070 if (att_end != equal) {
6071 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6072 equal += stripped_before;
6073 val_beg += stripped_before;
6074 }
6075
6076 if (val_beg > equal + 1) {
6077 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6078 val_beg += stripped_after;
6079 stripped_before += stripped_after;
6080 }
6081
6082 val_end += stripped_before;
6083 next += stripped_before;
6084 hdr_end += stripped_before;
6085 hdr_next += stripped_before;
6086 cur_hdr->len += stripped_before;
6087 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006089 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006091 /* First, let's see if we want to capture this cookie. We check
6092 * that we don't already have a client side cookie, because we
6093 * can only capture one. Also as an optimisation, we ignore
6094 * cookies shorter than the declared name.
6095 */
6096 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6097 (val_end - att_beg >= t->fe->capture_namelen) &&
6098 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6099 int log_len = val_end - att_beg;
6100
6101 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6102 Alert("HTTP logging : out of memory.\n");
6103 } else {
6104 if (log_len > t->fe->capture_len)
6105 log_len = t->fe->capture_len;
6106 memcpy(txn->cli_cookie, att_beg, log_len);
6107 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006108 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006109 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006110
Willy Tarreaubca99692010-10-06 19:25:55 +02006111 /* Persistence cookies in passive, rewrite or insert mode have the
6112 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006113 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006114 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006115 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006116 * For cookies in prefix mode, the form is :
6117 *
6118 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 */
6120 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6121 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6122 struct server *srv = t->be->srv;
6123 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006124
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006125 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6126 * have the server ID between val_beg and delim, and the original cookie between
6127 * delim+1 and val_end. Otherwise, delim==val_end :
6128 *
6129 * Cookie: NAME=SRV; # in all but prefix modes
6130 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6131 * | || || | |+-> next
6132 * | || || | +--> val_end
6133 * | || || +---------> delim
6134 * | || |+------------> val_beg
6135 * | || +-------------> att_end = equal
6136 * | |+-----------------> att_beg
6137 * | +------------------> prev
6138 * +-------------------------> hdr_beg
6139 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006140
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006141 if (t->be->options & PR_O_COOK_PFX) {
6142 for (delim = val_beg; delim < val_end; delim++)
6143 if (*delim == COOKIE_DELIM)
6144 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006145 } else {
6146 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006147 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006148 /* Now check if the cookie contains a date field, which would
6149 * appear after a vertical bar ('|') just after the server name
6150 * and before the delimiter.
6151 */
6152 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6153 if (vbar1) {
6154 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006155 * right is the last seen date. It is a base64 encoded
6156 * 30-bit value representing the UNIX date since the
6157 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006158 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006159 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006160 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006161 if (val_end - vbar1 >= 5) {
6162 val = b64tos30(vbar1);
6163 if (val > 0)
6164 txn->cookie_last_date = val << 2;
6165 }
6166 /* look for a second vertical bar */
6167 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6168 if (vbar1 && (val_end - vbar1 > 5)) {
6169 val = b64tos30(vbar1 + 1);
6170 if (val > 0)
6171 txn->cookie_first_date = val << 2;
6172 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006173 }
6174 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006175
Willy Tarreauf64d1412010-10-07 20:06:11 +02006176 /* if the cookie has an expiration date and the proxy wants to check
6177 * it, then we do that now. We first check if the cookie is too old,
6178 * then only if it has expired. We detect strict overflow because the
6179 * time resolution here is not great (4 seconds). Cookies with dates
6180 * in the future are ignored if their offset is beyond one day. This
6181 * allows an admin to fix timezone issues without expiring everyone
6182 * and at the same time avoids keeping unwanted side effects for too
6183 * long.
6184 */
6185 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006186 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6187 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006188 txn->flags &= ~TX_CK_MASK;
6189 txn->flags |= TX_CK_OLD;
6190 delim = val_beg; // let's pretend we have not found the cookie
6191 txn->cookie_first_date = 0;
6192 txn->cookie_last_date = 0;
6193 }
6194 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006195 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6196 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006197 txn->flags &= ~TX_CK_MASK;
6198 txn->flags |= TX_CK_EXPIRED;
6199 delim = val_beg; // let's pretend we have not found the cookie
6200 txn->cookie_first_date = 0;
6201 txn->cookie_last_date = 0;
6202 }
6203
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006204 /* Here, we'll look for the first running server which supports the cookie.
6205 * This allows to share a same cookie between several servers, for example
6206 * to dedicate backup servers to specific servers only.
6207 * However, to prevent clients from sticking to cookie-less backup server
6208 * when they have incidentely learned an empty cookie, we simply ignore
6209 * empty cookies and mark them as invalid.
6210 * The same behaviour is applied when persistence must be ignored.
6211 */
6212 if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST))
6213 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006215 while (srv) {
6216 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6217 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6218 if ((srv->state & SRV_RUNNING) ||
6219 (t->be->options & PR_O_PERSIST) ||
6220 (t->flags & SN_FORCE_PRST)) {
6221 /* we found the server and we can use it */
6222 txn->flags &= ~TX_CK_MASK;
6223 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6224 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006225 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006226 break;
6227 } else {
6228 /* we found a server, but it's down,
6229 * mark it as such and go on in case
6230 * another one is available.
6231 */
6232 txn->flags &= ~TX_CK_MASK;
6233 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006235 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006236 srv = srv->next;
6237 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006238
Willy Tarreauf64d1412010-10-07 20:06:11 +02006239 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006240 /* no server matched this cookie */
6241 txn->flags &= ~TX_CK_MASK;
6242 txn->flags |= TX_CK_INVALID;
6243 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006244
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 /* depending on the cookie mode, we may have to either :
6246 * - delete the complete cookie if we're in insert+indirect mode, so that
6247 * the server never sees it ;
6248 * - remove the server id from the cookie value, and tag the cookie as an
6249 * application cookie so that it does not get accidentely removed later,
6250 * if we're in cookie prefix mode
6251 */
6252 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6253 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006255 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6256 val_end += delta;
6257 next += delta;
6258 hdr_end += delta;
6259 hdr_next += delta;
6260 cur_hdr->len += delta;
6261 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006262
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006263 del_from = NULL;
6264 preserve_hdr = 1; /* we want to keep this cookie */
6265 }
6266 else if (del_from == NULL &&
6267 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6268 del_from = prev;
6269 }
6270 } else {
6271 /* This is not our cookie, so we must preserve it. But if we already
6272 * scheduled another cookie for removal, we cannot remove the
6273 * complete header, but we can remove the previous block itself.
6274 */
6275 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006276
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 if (del_from != NULL) {
6278 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006279 if (att_beg >= del_from)
6280 att_beg += delta;
6281 if (att_end >= del_from)
6282 att_end += delta;
6283 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006284 val_end += delta;
6285 next += delta;
6286 hdr_end += delta;
6287 hdr_next += delta;
6288 cur_hdr->len += delta;
6289 http_msg_move_end(&txn->req, delta);
6290 prev = del_from;
6291 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006292 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006293 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006294
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006295 /* Look for the appsession cookie unless persistence must be ignored */
6296 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6297 int cmp_len, value_len;
6298 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006299
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 if (t->be->options2 & PR_O2_AS_PFX) {
6301 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6302 value_begin = att_beg + t->be->appsession_name_len;
6303 value_len = val_end - att_beg - t->be->appsession_name_len;
6304 } else {
6305 cmp_len = att_end - att_beg;
6306 value_begin = val_beg;
6307 value_len = val_end - val_beg;
6308 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006309
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006310 /* let's see if the cookie is our appcookie */
6311 if (cmp_len == t->be->appsession_name_len &&
6312 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6313 manage_client_side_appsession(t, value_begin, value_len);
6314 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006315 }
6316
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006317 /* continue with next cookie on this header line */
6318 att_beg = next;
6319 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006320
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 /* There are no more cookies on this line.
6322 * We may still have one (or several) marked for deletion at the
6323 * end of the line. We must do this now in two ways :
6324 * - if some cookies must be preserved, we only delete from the
6325 * mark to the end of line ;
6326 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006327 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006329 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006330 if (preserve_hdr) {
6331 delta = del_hdr_value(req, &del_from, hdr_end);
6332 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006333 cur_hdr->len += delta;
6334 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006335 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336
6337 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006338 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6339 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006341 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006343 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006344 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006345 }
6346
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006347 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006348 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006349 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006350}
6351
6352
Willy Tarreaua15645d2007-03-18 16:22:39 +01006353/* Iterate the same filter through all response headers contained in <rtr>.
6354 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6355 */
6356int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6357{
6358 char term;
6359 char *cur_ptr, *cur_end, *cur_next;
6360 int cur_idx, old_idx, last_hdr;
6361 struct http_txn *txn = &t->txn;
6362 struct hdr_idx_elem *cur_hdr;
6363 int len, delta;
6364
6365 last_hdr = 0;
6366
Willy Tarreau962c3f42010-01-10 00:15:35 +01006367 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006368 old_idx = 0;
6369
6370 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006371 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006372 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006373 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006374 (exp->action == ACT_ALLOW ||
6375 exp->action == ACT_DENY))
6376 return 0;
6377
6378 cur_idx = txn->hdr_idx.v[old_idx].next;
6379 if (!cur_idx)
6380 break;
6381
6382 cur_hdr = &txn->hdr_idx.v[cur_idx];
6383 cur_ptr = cur_next;
6384 cur_end = cur_ptr + cur_hdr->len;
6385 cur_next = cur_end + cur_hdr->cr + 1;
6386
6387 /* Now we have one header between cur_ptr and cur_end,
6388 * and the next header starts at cur_next.
6389 */
6390
6391 /* The annoying part is that pattern matching needs
6392 * that we modify the contents to null-terminate all
6393 * strings before testing them.
6394 */
6395
6396 term = *cur_end;
6397 *cur_end = '\0';
6398
6399 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6400 switch (exp->action) {
6401 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006402 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006403 last_hdr = 1;
6404 break;
6405
6406 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006407 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006408 last_hdr = 1;
6409 break;
6410
6411 case ACT_REPLACE:
6412 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6413 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6414 /* FIXME: if the user adds a newline in the replacement, the
6415 * index will not be recalculated for now, and the new line
6416 * will not be counted as a new header.
6417 */
6418
6419 cur_end += delta;
6420 cur_next += delta;
6421 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006422 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006423 break;
6424
6425 case ACT_REMOVE:
6426 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6427 cur_next += delta;
6428
Willy Tarreaufa355d42009-11-29 18:12:29 +01006429 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006430 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6431 txn->hdr_idx.used--;
6432 cur_hdr->len = 0;
6433 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006434 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006435 break;
6436
6437 }
6438 }
6439 if (cur_end)
6440 *cur_end = term; /* restore the string terminator */
6441
6442 /* keep the link from this header to next one in case of later
6443 * removal of next header.
6444 */
6445 old_idx = cur_idx;
6446 }
6447 return 0;
6448}
6449
6450
6451/* Apply the filter to the status line in the response buffer <rtr>.
6452 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6453 * or -1 if a replacement resulted in an invalid status line.
6454 */
6455int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6456{
6457 char term;
6458 char *cur_ptr, *cur_end;
6459 int done;
6460 struct http_txn *txn = &t->txn;
6461 int len, delta;
6462
6463
Willy Tarreau3d300592007-03-18 18:34:41 +01006464 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006465 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006466 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006467 (exp->action == ACT_ALLOW ||
6468 exp->action == ACT_DENY))
6469 return 0;
6470 else if (exp->action == ACT_REMOVE)
6471 return 0;
6472
6473 done = 0;
6474
Willy Tarreau962c3f42010-01-10 00:15:35 +01006475 cur_ptr = txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006476 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006477
6478 /* Now we have the status line between cur_ptr and cur_end */
6479
6480 /* The annoying part is that pattern matching needs
6481 * that we modify the contents to null-terminate all
6482 * strings before testing them.
6483 */
6484
6485 term = *cur_end;
6486 *cur_end = '\0';
6487
6488 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6489 switch (exp->action) {
6490 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006491 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006492 done = 1;
6493 break;
6494
6495 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006496 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006497 done = 1;
6498 break;
6499
6500 case ACT_REPLACE:
6501 *cur_end = term; /* restore the string terminator */
6502 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6503 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6504 /* FIXME: if the user adds a newline in the replacement, the
6505 * index will not be recalculated for now, and the new line
6506 * will not be counted as a new header.
6507 */
6508
Willy Tarreaufa355d42009-11-29 18:12:29 +01006509 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006510 cur_end += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006511 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006512 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006513 cur_ptr, cur_end + 1,
6514 NULL, NULL);
6515 if (unlikely(!cur_end))
6516 return -1;
6517
6518 /* we have a full respnse and we know that we have either a CR
6519 * or an LF at <ptr>.
6520 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01006521 txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006522 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006523 /* there is no point trying this regex on headers */
6524 return 1;
6525 }
6526 }
6527 *cur_end = term; /* restore the string terminator */
6528 return done;
6529}
6530
6531
6532
6533/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006534 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006535 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6536 * unparsable response.
6537 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006538int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006539{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006540 struct http_txn *txn = &s->txn;
6541 struct hdr_exp *exp;
6542
6543 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006544 int ret;
6545
6546 /*
6547 * The interleaving of transformations and verdicts
6548 * makes it difficult to decide to continue or stop
6549 * the evaluation.
6550 */
6551
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006552 if (txn->flags & TX_SVDENY)
6553 break;
6554
Willy Tarreau3d300592007-03-18 18:34:41 +01006555 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006556 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6557 exp->action == ACT_PASS)) {
6558 exp = exp->next;
6559 continue;
6560 }
6561
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006562 /* if this filter had a condition, evaluate it now and skip to
6563 * next filter if the condition does not match.
6564 */
6565 if (exp->cond) {
6566 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6567 ret = acl_pass(ret);
6568 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6569 ret = !ret;
6570 if (!ret)
6571 continue;
6572 }
6573
Willy Tarreaua15645d2007-03-18 16:22:39 +01006574 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006575 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576 if (unlikely(ret < 0))
6577 return -1;
6578
6579 if (likely(ret == 0)) {
6580 /* The filter did not match the response, it can be
6581 * iterated through all headers.
6582 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006583 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006584 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585 }
6586 return 0;
6587}
6588
6589
Willy Tarreaua15645d2007-03-18 16:22:39 +01006590/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006591 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006592 * desirable to call it only when needed. This function is also used when we
6593 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006594 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006595void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596{
6597 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006598 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006599 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006600 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006601 char *hdr_beg, *hdr_end, *hdr_next;
6602 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603
Willy Tarreaua15645d2007-03-18 16:22:39 +01006604 /* Iterate through the headers.
6605 * we start with the start line.
6606 */
6607 old_idx = 0;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006608 hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609
6610 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6611 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006612 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006613
6614 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006615 hdr_beg = hdr_next;
6616 hdr_end = hdr_beg + cur_hdr->len;
6617 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006618
Willy Tarreau24581ba2010-08-31 22:39:35 +02006619 /* We have one full header between hdr_beg and hdr_end, and the
6620 * next header starts at hdr_next. We're only interested in
6621 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006622 */
6623
Willy Tarreau24581ba2010-08-31 22:39:35 +02006624 is_cookie2 = 0;
6625 prev = hdr_beg + 10;
6626 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006627 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006628 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6629 if (!val) {
6630 old_idx = cur_idx;
6631 continue;
6632 }
6633 is_cookie2 = 1;
6634 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006635 }
6636
Willy Tarreau24581ba2010-08-31 22:39:35 +02006637 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6638 * <prev> points to the colon.
6639 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006640 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006641
Willy Tarreau24581ba2010-08-31 22:39:35 +02006642 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6643 * check-cache is enabled) and we are not interested in checking
6644 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006645 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006646 if (t->be->cookie_name == NULL &&
6647 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006648 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006649 return;
6650
Willy Tarreau24581ba2010-08-31 22:39:35 +02006651 /* OK so now we know we have to process this response cookie.
6652 * The format of the Set-Cookie header is slightly different
6653 * from the format of the Cookie header in that it does not
6654 * support the comma as a cookie delimiter (thus the header
6655 * cannot be folded) because the Expires attribute described in
6656 * the original Netscape's spec may contain an unquoted date
6657 * with a comma inside. We have to live with this because
6658 * many browsers don't support Max-Age and some browsers don't
6659 * support quoted strings. However the Set-Cookie2 header is
6660 * clean.
6661 *
6662 * We have to keep multiple pointers in order to support cookie
6663 * removal at the beginning, middle or end of header without
6664 * corrupting the header (in case of set-cookie2). A special
6665 * pointer, <scav> points to the beginning of the set-cookie-av
6666 * fields after the first semi-colon. The <next> pointer points
6667 * either to the end of line (set-cookie) or next unquoted comma
6668 * (set-cookie2). All of these headers are valid :
6669 *
6670 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6671 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6672 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6673 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6674 * | | | | | | | | | |
6675 * | | | | | | | | +-> next hdr_end <--+
6676 * | | | | | | | +------------> scav
6677 * | | | | | | +--------------> val_end
6678 * | | | | | +--------------------> val_beg
6679 * | | | | +----------------------> equal
6680 * | | | +------------------------> att_end
6681 * | | +----------------------------> att_beg
6682 * | +------------------------------> prev
6683 * +-----------------------------------------> hdr_beg
6684 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006685
Willy Tarreau24581ba2010-08-31 22:39:35 +02006686 for (; prev < hdr_end; prev = next) {
6687 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006688
Willy Tarreau24581ba2010-08-31 22:39:35 +02006689 /* find att_beg */
6690 att_beg = prev + 1;
6691 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6692 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006693
Willy Tarreau24581ba2010-08-31 22:39:35 +02006694 /* find att_end : this is the first character after the last non
6695 * space before the equal. It may be equal to hdr_end.
6696 */
6697 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006698
Willy Tarreau24581ba2010-08-31 22:39:35 +02006699 while (equal < hdr_end) {
6700 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6701 break;
6702 if (http_is_spht[(unsigned char)*equal++])
6703 continue;
6704 att_end = equal;
6705 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706
Willy Tarreau24581ba2010-08-31 22:39:35 +02006707 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6708 * is between <att_beg> and <equal>, both may be identical.
6709 */
6710
6711 /* look for end of cookie if there is an equal sign */
6712 if (equal < hdr_end && *equal == '=') {
6713 /* look for the beginning of the value */
6714 val_beg = equal + 1;
6715 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6716 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006717
Willy Tarreau24581ba2010-08-31 22:39:35 +02006718 /* find the end of the value, respecting quotes */
6719 next = find_cookie_value_end(val_beg, hdr_end);
6720
6721 /* make val_end point to the first white space or delimitor after the value */
6722 val_end = next;
6723 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6724 val_end--;
6725 } else {
6726 /* <equal> points to next comma, semi-colon or EOL */
6727 val_beg = val_end = next = equal;
6728 }
6729
6730 if (next < hdr_end) {
6731 /* Set-Cookie2 supports multiple cookies, and <next> points to
6732 * a colon or semi-colon before the end. So skip all attr-value
6733 * pairs and look for the next comma. For Set-Cookie, since
6734 * commas are permitted in values, skip to the end.
6735 */
6736 if (is_cookie2)
6737 next = find_hdr_value_end(next, hdr_end);
6738 else
6739 next = hdr_end;
6740 }
6741
6742 /* Now everything is as on the diagram above */
6743
6744 /* Ignore cookies with no equal sign */
6745 if (equal == val_end)
6746 continue;
6747
6748 /* If there are spaces around the equal sign, we need to
6749 * strip them otherwise we'll get trouble for cookie captures,
6750 * or even for rewrites. Since this happens extremely rarely,
6751 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006752 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006753 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6754 int stripped_before = 0;
6755 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006756
Willy Tarreau24581ba2010-08-31 22:39:35 +02006757 if (att_end != equal) {
6758 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6759 equal += stripped_before;
6760 val_beg += stripped_before;
6761 }
6762
6763 if (val_beg > equal + 1) {
6764 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6765 val_beg += stripped_after;
6766 stripped_before += stripped_after;
6767 }
6768
6769 val_end += stripped_before;
6770 next += stripped_before;
6771 hdr_end += stripped_before;
6772 hdr_next += stripped_before;
6773 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006774 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006775 }
6776
6777 /* First, let's see if we want to capture this cookie. We check
6778 * that we don't already have a server side cookie, because we
6779 * can only capture one. Also as an optimisation, we ignore
6780 * cookies shorter than the declared name.
6781 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006782 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006783 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006784 (val_end - att_beg >= t->fe->capture_namelen) &&
6785 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6786 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006787 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006788 Alert("HTTP logging : out of memory.\n");
6789 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006790 else {
6791 if (log_len > t->fe->capture_len)
6792 log_len = t->fe->capture_len;
6793 memcpy(txn->srv_cookie, att_beg, log_len);
6794 txn->srv_cookie[log_len] = 0;
6795 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006796 }
6797
Willy Tarreau827aee92011-03-10 16:55:02 +01006798 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006800 if (!(t->flags & SN_IGNORE_PRST) &&
6801 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6802 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006803 /* assume passive cookie by default */
6804 txn->flags &= ~TX_SCK_MASK;
6805 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806
6807 /* If the cookie is in insert mode on a known server, we'll delete
6808 * this occurrence because we'll insert another one later.
6809 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006810 * a direct access.
6811 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006812 if (t->be->options2 & PR_O2_COOK_PSV) {
6813 /* The "preserve" flag was set, we don't want to touch the
6814 * server's cookie.
6815 */
6816 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006817 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006818 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006819 /* this cookie must be deleted */
6820 if (*prev == ':' && next == hdr_end) {
6821 /* whole header */
6822 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6823 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6824 txn->hdr_idx.used--;
6825 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006826 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006827 hdr_next += delta;
6828 http_msg_move_end(&txn->rsp, delta);
6829 /* note: while both invalid now, <next> and <hdr_end>
6830 * are still equal, so the for() will stop as expected.
6831 */
6832 } else {
6833 /* just remove the value */
6834 int delta = del_hdr_value(res, &prev, next);
6835 next = prev;
6836 hdr_end += delta;
6837 hdr_next += delta;
6838 cur_hdr->len += delta;
6839 http_msg_move_end(&txn->rsp, delta);
6840 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006841 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006842 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006843 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006845 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006846 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847 * with this server since we know it.
6848 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006849 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006850 next += delta;
6851 hdr_end += delta;
6852 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006854 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855
Willy Tarreauf1348312010-10-07 15:54:11 +02006856 txn->flags &= ~TX_SCK_MASK;
6857 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006858 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006859 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006861 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006863 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006864 next += delta;
6865 hdr_end += delta;
6866 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006867 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006868 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006869
Willy Tarreau827aee92011-03-10 16:55:02 +01006870 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006871 txn->flags &= ~TX_SCK_MASK;
6872 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873 }
6874 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006875 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6876 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006877 int cmp_len, value_len;
6878 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006879
Cyril Bontéb21570a2009-11-29 20:04:48 +01006880 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006881 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6882 value_begin = att_beg + t->be->appsession_name_len;
6883 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006884 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006885 cmp_len = att_end - att_beg;
6886 value_begin = val_beg;
6887 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006888 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006889
Cyril Bonté17530c32010-04-06 21:11:10 +02006890 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006891 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6892 /* free a possibly previously allocated memory */
6893 pool_free2(apools.sessid, txn->sessid);
6894
Cyril Bontéb21570a2009-11-29 20:04:48 +01006895 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006896 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006897 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6898 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6899 return;
6900 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006901 memcpy(txn->sessid, value_begin, value_len);
6902 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006903 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006904 }
6905 /* that's done for this cookie, check the next one on the same
6906 * line when next != hdr_end (only if is_cookie2).
6907 */
6908 }
6909 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006910 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006911 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006912
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006913 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006914 appsess *asession = NULL;
6915 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006916 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006917 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006918 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006919 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6920 Alert("Not enough Memory process_srv():asession:calloc().\n");
6921 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6922 return;
6923 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006924 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6925
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006926 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6927 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6928 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006929 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006930 return;
6931 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006932 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006933 asession->sessid[t->be->appsession_len] = 0;
6934
Willy Tarreau827aee92011-03-10 16:55:02 +01006935 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006936 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006937 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006938 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006939 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006940 return;
6941 }
6942 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006943 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006944
6945 asession->request_count = 0;
6946 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6947 }
6948
6949 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6950 asession->request_count++;
6951 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006952}
6953
6954
Willy Tarreaua15645d2007-03-18 16:22:39 +01006955/*
6956 * Check if response is cacheable or not. Updates t->flags.
6957 */
6958void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6959{
6960 struct http_txn *txn = &t->txn;
6961 char *p1, *p2;
6962
6963 char *cur_ptr, *cur_end, *cur_next;
6964 int cur_idx;
6965
Willy Tarreau5df51872007-11-25 16:20:08 +01006966 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006967 return;
6968
6969 /* Iterate through the headers.
6970 * we start with the start line.
6971 */
6972 cur_idx = 0;
Willy Tarreau962c3f42010-01-10 00:15:35 +01006973 cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974
6975 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6976 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006977 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006978
6979 cur_hdr = &txn->hdr_idx.v[cur_idx];
6980 cur_ptr = cur_next;
6981 cur_end = cur_ptr + cur_hdr->len;
6982 cur_next = cur_end + cur_hdr->cr + 1;
6983
6984 /* We have one full header between cur_ptr and cur_end, and the
6985 * next header starts at cur_next. We're only interested in
6986 * "Cookie:" headers.
6987 */
6988
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006989 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6990 if (val) {
6991 if ((cur_end - (cur_ptr + val) >= 8) &&
6992 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6993 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6994 return;
6995 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006996 }
6997
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006998 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
6999 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000 continue;
7001
7002 /* OK, right now we know we have a cache-control header at cur_ptr */
7003
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007004 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007005
7006 if (p1 >= cur_end) /* no more info */
7007 continue;
7008
7009 /* p1 is at the beginning of the value */
7010 p2 = p1;
7011
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007012 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013 p2++;
7014
7015 /* we have a complete value between p1 and p2 */
7016 if (p2 < cur_end && *p2 == '=') {
7017 /* we have something of the form no-cache="set-cookie" */
7018 if ((cur_end - p1 >= 21) &&
7019 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7020 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007021 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007022 continue;
7023 }
7024
7025 /* OK, so we know that either p2 points to the end of string or to a comma */
7026 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7027 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7028 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7029 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007030 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007031 return;
7032 }
7033
7034 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007035 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036 continue;
7037 }
7038 }
7039}
7040
7041
Willy Tarreau58f10d72006-12-04 02:26:12 +01007042/*
7043 * Try to retrieve a known appsession in the URI, then the associated server.
7044 * If the server is found, it's assigned to the session.
7045 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007046void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007047{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007048 char *end_params, *first_param, *cur_param, *next_param;
7049 char separator;
7050 int value_len;
7051
7052 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007053
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007054 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007055 (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 +01007056 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007057 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007058
Cyril Bontéb21570a2009-11-29 20:04:48 +01007059 first_param = NULL;
7060 switch (mode) {
7061 case PR_O2_AS_M_PP:
7062 first_param = memchr(begin, ';', len);
7063 break;
7064 case PR_O2_AS_M_QS:
7065 first_param = memchr(begin, '?', len);
7066 break;
7067 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007068
Cyril Bontéb21570a2009-11-29 20:04:48 +01007069 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007070 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007071 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072
Cyril Bontéb21570a2009-11-29 20:04:48 +01007073 switch (mode) {
7074 case PR_O2_AS_M_PP:
7075 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7076 end_params = (char *) begin + len;
7077 }
7078 separator = ';';
7079 break;
7080 case PR_O2_AS_M_QS:
7081 end_params = (char *) begin + len;
7082 separator = '&';
7083 break;
7084 default:
7085 /* unknown mode, shouldn't happen */
7086 return;
7087 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007088
Cyril Bontéb21570a2009-11-29 20:04:48 +01007089 cur_param = next_param = end_params;
7090 while (cur_param > first_param) {
7091 cur_param--;
7092 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7093 /* let's see if this is the appsession parameter */
7094 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7095 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7096 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7097 /* Cool... it's the right one */
7098 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7099 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7100 if (value_len > 0) {
7101 manage_client_side_appsession(t, cur_param, value_len);
7102 }
7103 break;
7104 }
7105 next_param = cur_param;
7106 }
7107 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007108#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007109 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007110 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007111#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007112}
7113
Willy Tarreaub2513902006-12-17 14:52:38 +01007114/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007115 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007116 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007117 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007118 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007119 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007120 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007121 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007122 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007123int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007124{
7125 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007126 char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007127
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007128 if (!uri_auth)
7129 return 0;
7130
Cyril Bonté70be45d2010-10-12 00:14:35 +02007131 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007132 return 0;
7133
Willy Tarreau295a8372011-03-10 11:25:07 +01007134 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007135
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007136 /* check URI size */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01007137 if (uri_auth->uri_len > txn->req.sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007138 return 0;
7139
Willy Tarreau962c3f42010-01-10 00:15:35 +01007140 h = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007141
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007142 /* the URI is in h */
7143 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007144 return 0;
7145
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007146 h += uri_auth->uri_len;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007147 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007148 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007149 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007150 break;
7151 }
7152 h++;
7153 }
7154
7155 if (uri_auth->refresh) {
Willy Tarreau962c3f42010-01-10 00:15:35 +01007156 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7157 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007158 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007159 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007160 break;
7161 }
7162 h++;
7163 }
7164 }
7165
Willy Tarreau962c3f42010-01-10 00:15:35 +01007166 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7167 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007168 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007169 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007170 break;
7171 }
7172 h++;
7173 }
7174
Cyril Bonté70be45d2010-10-12 00:14:35 +02007175 h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len;
7176 while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) {
7177 if (memcmp(h, ";st=", 4) == 0) {
7178 h += 4;
7179
7180 if (memcmp(h, STAT_STATUS_DONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007181 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007182 else if (memcmp(h, STAT_STATUS_NONE, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007183 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02007184 else if (memcmp(h, STAT_STATUS_PART, 4) == 0)
7185 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
7186 else if (memcmp(h, STAT_STATUS_ERRP, 4) == 0)
7187 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007188 else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007189 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté474be412010-10-12 00:14:36 +02007190 else if (memcmp(h, STAT_STATUS_DENY, 4) == 0)
Willy Tarreau295a8372011-03-10 11:25:07 +01007191 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007192 else
Willy Tarreau295a8372011-03-10 11:25:07 +01007193 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007194 break;
7195 }
7196 h++;
7197 }
7198
Willy Tarreau295a8372011-03-10 11:25:07 +01007199 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007200
Willy Tarreaub2513902006-12-17 14:52:38 +01007201 return 1;
7202}
7203
Willy Tarreau4076a152009-04-02 15:18:36 +02007204/*
7205 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007206 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
7207 * assume that msg->sol = buf->data + msg->som.
Willy Tarreau4076a152009-04-02 15:18:36 +02007208 */
7209void http_capture_bad_message(struct error_snapshot *es, struct session *s,
7210 struct buffer *buf, struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007211 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007212{
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007213 if (buf->r <= (buf->data + msg->som)) { /* message wraps */
7214 int len1 = buf->size - msg->som;
7215 es->len = buf->r - (buf->data + msg->som) + buf->size;
7216 memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf)));
7217 if (es->len > len1 && len1 < sizeof(es->buf))
7218 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7219 }
7220 else {
7221 es->len = buf->r - (buf->data + msg->som);
7222 memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf)));
7223 }
7224
Willy Tarreau4076a152009-04-02 15:18:36 +02007225 if (msg->err_pos >= 0)
Willy Tarreau2df8d712009-05-01 11:33:17 +02007226 es->pos = msg->err_pos - msg->som;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007227 else if (buf->lr >= (buf->data + msg->som))
Willy Tarreau2df8d712009-05-01 11:33:17 +02007228 es->pos = buf->lr - (buf->data + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007229 else
7230 es->pos = buf->lr - (buf->data + msg->som) + buf->size;
7231
Willy Tarreau4076a152009-04-02 15:18:36 +02007232 es->when = date; // user-visible date
7233 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007234 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007235 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007236 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007237 es->state = state;
7238 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007239 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007240}
Willy Tarreaub2513902006-12-17 14:52:38 +01007241
Willy Tarreau294c4732011-12-16 21:35:50 +01007242/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7243 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7244 * performed over the whole headers. Otherwise it must contain a valid header
7245 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7246 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7247 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7248 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7249 * -1.
7250 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007251 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007252unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7253 struct hdr_idx *idx, int occ,
7254 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007255{
Willy Tarreau294c4732011-12-16 21:35:50 +01007256 struct hdr_ctx local_ctx;
7257 char *ptr_hist[MAX_HDR_HISTORY];
7258 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007259 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007260 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007261
Willy Tarreau294c4732011-12-16 21:35:50 +01007262 if (!ctx) {
7263 local_ctx.idx = 0;
7264 ctx = &local_ctx;
7265 }
7266
Willy Tarreaubce70882009-09-07 11:51:47 +02007267 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007268 /* search from the beginning */
7269 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007270 occ--;
7271 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007272 *vptr = ctx->line + ctx->val;
7273 *vlen = ctx->vlen;
7274 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007275 }
7276 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007277 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007278 }
7279
7280 /* negative occurrence, we scan all the list then walk back */
7281 if (-occ > MAX_HDR_HISTORY)
7282 return 0;
7283
Willy Tarreau294c4732011-12-16 21:35:50 +01007284 found = hist_ptr = 0;
7285 while (http_find_header2(hname, hlen, msg->sol, idx, ctx)) {
7286 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7287 len_hist[hist_ptr] = ctx->vlen;
7288 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007289 hist_ptr = 0;
7290 found++;
7291 }
7292 if (-occ > found)
7293 return 0;
7294 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7295 * find occurrence -occ, so we have to check [hist_ptr+occ].
7296 */
7297 hist_ptr += occ;
7298 if (hist_ptr >= MAX_HDR_HISTORY)
7299 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007300 *vptr = ptr_hist[hist_ptr];
7301 *vlen = len_hist[hist_ptr];
7302 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007303}
7304
Willy Tarreaubaaee002006-06-26 02:48:02 +02007305/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007306 * Print a debug line with a header
7307 */
7308void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7309{
7310 int len, max;
7311 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007312 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007313 max = end - start;
7314 UBOUND(max, sizeof(trash) - len - 1);
7315 len += strlcpy2(trash + len, start, max + 1);
7316 trash[len++] = '\n';
7317 write(1, trash, len);
7318}
7319
Willy Tarreau0937bc42009-12-22 15:03:09 +01007320/*
7321 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7322 * the required fields are properly allocated and that we only need to (re)init
7323 * them. This should be used before processing any new request.
7324 */
7325void http_init_txn(struct session *s)
7326{
7327 struct http_txn *txn = &s->txn;
7328 struct proxy *fe = s->fe;
7329
7330 txn->flags = 0;
7331 txn->status = -1;
7332
Willy Tarreauf64d1412010-10-07 20:06:11 +02007333 txn->cookie_first_date = 0;
7334 txn->cookie_last_date = 0;
7335
Willy Tarreau0937bc42009-12-22 15:03:09 +01007336 txn->req.sol = txn->req.eol = NULL;
7337 txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
7338 txn->rsp.sol = txn->rsp.eol = NULL;
7339 txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreau124d9912011-03-01 20:30:48 +01007340 txn->req.chunk_len = 0LL;
7341 txn->req.body_len = 0LL;
7342 txn->rsp.chunk_len = 0LL;
7343 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007344 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7345 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007346
7347 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007348
7349 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7350 if (fe->options2 & PR_O2_REQBUG_OK)
7351 txn->req.err_pos = -1; /* let buggy requests pass */
7352
Willy Tarreau46023632010-01-07 22:51:47 +01007353 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007354 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7355
Willy Tarreau46023632010-01-07 22:51:47 +01007356 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007357 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7358
7359 if (txn->hdr_idx.v)
7360 hdr_idx_init(&txn->hdr_idx);
7361}
7362
7363/* to be used at the end of a transaction */
7364void http_end_txn(struct session *s)
7365{
7366 struct http_txn *txn = &s->txn;
7367
7368 /* these ones will have been dynamically allocated */
7369 pool_free2(pool2_requri, txn->uri);
7370 pool_free2(pool2_capture, txn->cli_cookie);
7371 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007372 pool_free2(apools.sessid, txn->sessid);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007373
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007374 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007375 txn->uri = NULL;
7376 txn->srv_cookie = NULL;
7377 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007378
7379 if (txn->req.cap) {
7380 struct cap_hdr *h;
7381 for (h = s->fe->req_cap; h; h = h->next)
7382 pool_free2(h->pool, txn->req.cap[h->index]);
7383 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7384 }
7385
7386 if (txn->rsp.cap) {
7387 struct cap_hdr *h;
7388 for (h = s->fe->rsp_cap; h; h = h->next)
7389 pool_free2(h->pool, txn->rsp.cap[h->index]);
7390 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7391 }
7392
Willy Tarreau0937bc42009-12-22 15:03:09 +01007393}
7394
7395/* to be used at the end of a transaction to prepare a new one */
7396void http_reset_txn(struct session *s)
7397{
7398 http_end_txn(s);
7399 http_init_txn(s);
7400
7401 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007402 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007403 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007404 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007405 /* re-init store persistence */
7406 s->store_count = 0;
7407
Willy Tarreau0937bc42009-12-22 15:03:09 +01007408 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007409
7410 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7411
Willy Tarreau739cfba2010-01-25 23:11:14 +01007412 /* We must trim any excess data from the response buffer, because we
7413 * may have blocked an invalid response from a server that we don't
7414 * want to accidentely forward once we disable the analysers, nor do
7415 * we want those data to come along with next response. A typical
7416 * example of such data would be from a buggy server responding to
7417 * a HEAD with some data, or sending more than the advertised
7418 * content-length.
7419 */
7420 if (unlikely(s->rep->l > s->rep->send_max)) {
7421 s->rep->l = s->rep->send_max;
7422 s->rep->r = s->rep->w + s->rep->l;
7423 if (s->rep->r >= s->rep->data + s->rep->size)
7424 s->rep->r -= s->rep->size;
7425 }
7426
Willy Tarreau0937bc42009-12-22 15:03:09 +01007427 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007428 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007429
Willy Tarreaud04e8582010-05-31 12:31:35 +02007430 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007431 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007432
7433 s->req->rex = TICK_ETERNITY;
7434 s->req->wex = TICK_ETERNITY;
7435 s->req->analyse_exp = TICK_ETERNITY;
7436 s->rep->rex = TICK_ETERNITY;
7437 s->rep->wex = TICK_ETERNITY;
7438 s->rep->analyse_exp = TICK_ETERNITY;
7439}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007440
Willy Tarreauff011f22011-01-06 17:51:27 +01007441void free_http_req_rules(struct list *r) {
7442 struct http_req_rule *tr, *pr;
7443
7444 list_for_each_entry_safe(pr, tr, r, list) {
7445 LIST_DEL(&pr->list);
7446 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7447 free(pr->http_auth.realm);
7448
7449 free(pr);
7450 }
7451}
7452
7453struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7454{
7455 struct http_req_rule *rule;
7456 int cur_arg;
7457
7458 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7459 if (!rule) {
7460 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7461 return NULL;
7462 }
7463
7464 if (!*args[0]) {
7465 goto req_error_parsing;
7466 } else if (!strcmp(args[0], "allow")) {
7467 rule->action = HTTP_REQ_ACT_ALLOW;
7468 cur_arg = 1;
7469 } else if (!strcmp(args[0], "deny")) {
7470 rule->action = HTTP_REQ_ACT_DENY;
7471 cur_arg = 1;
7472 } else if (!strcmp(args[0], "auth")) {
7473 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7474 cur_arg = 1;
7475
7476 while(*args[cur_arg]) {
7477 if (!strcmp(args[cur_arg], "realm")) {
7478 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7479 cur_arg+=2;
7480 continue;
7481 } else
7482 break;
7483 }
7484 } else {
7485req_error_parsing:
7486 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7487 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7488 return NULL;
7489 }
7490
7491 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7492 struct acl_cond *cond;
7493
7494 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7495 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7496 file, linenum, args[0]);
7497 return NULL;
7498 }
7499 rule->cond = cond;
7500 }
7501 else if (*args[cur_arg]) {
7502 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7503 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7504 file, linenum, args[0], args[cur_arg]);
7505 return NULL;
7506 }
7507
7508 return rule;
7509}
7510
Willy Tarreau8797c062007-05-07 00:55:35 +02007511/************************************************************************/
7512/* The code below is dedicated to ACL parsing and matching */
7513/************************************************************************/
7514
7515
7516
7517
7518/* 1. Check on METHOD
7519 * We use the pre-parsed method if it is known, and store its number as an
7520 * integer. If it is unknown, we use the pointer and the length.
7521 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007522static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007523{
7524 int len, meth;
7525
Willy Tarreauae8b7962007-06-09 23:10:04 +02007526 len = strlen(*text);
7527 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007528
7529 pattern->val.i = meth;
7530 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007531 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007532 if (!pattern->ptr.str)
7533 return 0;
7534 pattern->len = len;
7535 }
7536 return 1;
7537}
7538
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007539/* This function fetches the method of current HTTP request and stores
7540 * it in the global pattern struct as a chunk. There are two possibilities :
7541 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7542 * in <len> and <ptr> is NULL ;
7543 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7544 * <len> to its length.
7545 * This is intended to be used with acl_match_meth() only.
7546 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007547static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007548acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7549 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007550{
7551 int meth;
7552 struct http_txn *txn = l7;
7553
Willy Tarreaub6866442008-07-14 23:54:42 +02007554 if (!txn)
7555 return 0;
7556
Willy Tarreau655dce92009-11-08 13:10:58 +01007557 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007558 return 0;
7559
Willy Tarreau8797c062007-05-07 00:55:35 +02007560 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007561 temp_pattern.data.str.len = meth;
7562 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007563 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007564 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7565 /* ensure the indexes are not affected */
7566 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007567 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
7568 temp_pattern.data.str.str = txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007569 }
7570 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7571 return 1;
7572}
7573
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007574/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007575static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7576{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007577 int icase;
7578
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007579
7580 if (temp_pattern.data.str.str == NULL) {
7581 /* well-known method */
7582 if (temp_pattern.data.str.len == pattern->val.i)
7583 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007584 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007585 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007586
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007587 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7588 if (pattern->val.i != HTTP_METH_OTHER)
7589 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007590
7591 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007592 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007593 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007594
7595 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007596 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7597 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007598 return ACL_PAT_FAIL;
7599 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007600}
7601
7602/* 2. Check on Request/Status Version
7603 * We simply compare strings here.
7604 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007605static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007606{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007607 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007608 if (!pattern->ptr.str)
7609 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007610 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007611 return 1;
7612}
7613
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007614static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007615acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7616 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007617{
7618 struct http_txn *txn = l7;
7619 char *ptr;
7620 int len;
7621
Willy Tarreaub6866442008-07-14 23:54:42 +02007622 if (!txn)
7623 return 0;
7624
Willy Tarreau655dce92009-11-08 13:10:58 +01007625 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007626 return 0;
7627
Willy Tarreau8797c062007-05-07 00:55:35 +02007628 len = txn->req.sl.rq.v_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007629 ptr = txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007630
7631 while ((len-- > 0) && (*ptr++ != '/'));
7632 if (len <= 0)
7633 return 0;
7634
Willy Tarreau664092c2011-12-16 19:11:42 +01007635 temp_pattern.data.str.str = ptr;
7636 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007637
7638 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7639 return 1;
7640}
7641
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007642static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007643acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7644 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007645{
7646 struct http_txn *txn = l7;
7647 char *ptr;
7648 int len;
7649
Willy Tarreaub6866442008-07-14 23:54:42 +02007650 if (!txn)
7651 return 0;
7652
Willy Tarreau655dce92009-11-08 13:10:58 +01007653 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007654 return 0;
7655
Willy Tarreau8797c062007-05-07 00:55:35 +02007656 len = txn->rsp.sl.st.v_l;
7657 ptr = txn->rsp.sol;
7658
7659 while ((len-- > 0) && (*ptr++ != '/'));
7660 if (len <= 0)
7661 return 0;
7662
Willy Tarreau664092c2011-12-16 19:11:42 +01007663 temp_pattern.data.str.str = ptr;
7664 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007665
7666 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7667 return 1;
7668}
7669
7670/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007671static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007672acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7673 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007674{
7675 struct http_txn *txn = l7;
7676 char *ptr;
7677 int len;
7678
Willy Tarreaub6866442008-07-14 23:54:42 +02007679 if (!txn)
7680 return 0;
7681
Willy Tarreau655dce92009-11-08 13:10:58 +01007682 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007683 return 0;
7684
Willy Tarreau8797c062007-05-07 00:55:35 +02007685 len = txn->rsp.sl.st.c_l;
Willy Tarreau962c3f42010-01-10 00:15:35 +01007686 ptr = txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007687
Willy Tarreaua5e37562011-12-16 17:06:15 +01007688 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007689 test->flags = ACL_TEST_F_VOL_1ST;
7690 return 1;
7691}
7692
7693/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007694static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007695acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7696 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007697{
7698 struct http_txn *txn = l7;
7699
Willy Tarreaub6866442008-07-14 23:54:42 +02007700 if (!txn)
7701 return 0;
7702
Willy Tarreau655dce92009-11-08 13:10:58 +01007703 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007704 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007705
Willy Tarreauc11416f2007-06-17 16:58:38 +02007706 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7707 /* ensure the indexes are not affected */
7708 return 0;
7709
Willy Tarreau664092c2011-12-16 19:11:42 +01007710 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
7711 temp_pattern.data.str.str = txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007712
Willy Tarreauf3d25982007-05-08 22:45:09 +02007713 /* we do not need to set READ_ONLY because the data is in a buffer */
7714 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007715 return 1;
7716}
7717
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007718static int
7719acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7720 struct acl_expr *expr, struct acl_test *test)
7721{
7722 struct http_txn *txn = l7;
7723
Willy Tarreaub6866442008-07-14 23:54:42 +02007724 if (!txn)
7725 return 0;
7726
Willy Tarreau655dce92009-11-08 13:10:58 +01007727 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007728 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007729
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007730 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7731 /* ensure the indexes are not affected */
7732 return 0;
7733
7734 /* Parse HTTP request */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007735 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 +01007736 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7737 return 0;
7738 temp_pattern.type = PATTERN_TYPE_IP;
7739 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007740
7741 /*
7742 * If we are parsing url in frontend space, we prepare backend stage
7743 * to not parse again the same url ! optimization lazyness...
7744 */
7745 if (px->options & PR_O_HTTP_PROXY)
7746 l4->flags |= SN_ADDR_SET;
7747
Willy Tarreauf4362b32011-12-16 17:49:52 +01007748 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007749 return 1;
7750}
7751
7752static int
7753acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7754 struct acl_expr *expr, struct acl_test *test)
7755{
7756 struct http_txn *txn = l7;
7757
Willy Tarreaub6866442008-07-14 23:54:42 +02007758 if (!txn)
7759 return 0;
7760
Willy Tarreau655dce92009-11-08 13:10:58 +01007761 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007762 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007763
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007764 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7765 /* ensure the indexes are not affected */
7766 return 0;
7767
7768 /* Same optimization as url_ip */
Willy Tarreau6471afb2011-09-23 10:54:59 +02007769 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 +01007770 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007771
7772 if (px->options & PR_O_HTTP_PROXY)
7773 l4->flags |= SN_ADDR_SET;
7774
7775 test->flags = ACL_TEST_F_READ_ONLY;
7776 return 1;
7777}
7778
Willy Tarreauc11416f2007-06-17 16:58:38 +02007779/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7780 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7781 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007782static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007783acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007784 struct acl_expr *expr, struct acl_test *test)
7785{
7786 struct http_txn *txn = l7;
7787 struct hdr_idx *idx = &txn->hdr_idx;
7788 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007789
Willy Tarreaub6866442008-07-14 23:54:42 +02007790 if (!txn)
7791 return 0;
7792
Willy Tarreau33a7e692007-06-10 19:45:56 +02007793 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7794 /* search for header from the beginning */
7795 ctx->idx = 0;
7796
Willy Tarreau33a7e692007-06-10 19:45:56 +02007797 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7798 test->flags |= ACL_TEST_F_FETCH_MORE;
7799 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007800 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7801 temp_pattern.data.str.len = ctx->vlen;
7802
Willy Tarreau33a7e692007-06-10 19:45:56 +02007803 return 1;
7804 }
7805
7806 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7807 test->flags |= ACL_TEST_F_VOL_HDR;
7808 return 0;
7809}
7810
Willy Tarreau33a7e692007-06-10 19:45:56 +02007811static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007812acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7813 struct acl_expr *expr, struct acl_test *test)
7814{
7815 struct http_txn *txn = l7;
7816
Willy Tarreaub6866442008-07-14 23:54:42 +02007817 if (!txn)
7818 return 0;
7819
Willy Tarreau655dce92009-11-08 13:10:58 +01007820 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007821 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007822
Willy Tarreauc11416f2007-06-17 16:58:38 +02007823 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7824 /* ensure the indexes are not affected */
7825 return 0;
7826
7827 return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test);
7828}
7829
7830static int
7831acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7832 struct acl_expr *expr, struct acl_test *test)
7833{
7834 struct http_txn *txn = l7;
7835
Willy Tarreaub6866442008-07-14 23:54:42 +02007836 if (!txn)
7837 return 0;
7838
Willy Tarreau655dce92009-11-08 13:10:58 +01007839 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007840 return 0;
7841
7842 return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test);
7843}
7844
7845/* 6. Check on HTTP header count. The number of occurrences is returned.
7846 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7847 */
7848static int
7849acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007850 struct acl_expr *expr, struct acl_test *test)
7851{
7852 struct http_txn *txn = l7;
7853 struct hdr_idx *idx = &txn->hdr_idx;
7854 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007855 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007856
Willy Tarreaub6866442008-07-14 23:54:42 +02007857 if (!txn)
7858 return 0;
7859
Willy Tarreau33a7e692007-06-10 19:45:56 +02007860 ctx.idx = 0;
7861 cnt = 0;
7862 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7863 cnt++;
7864
Willy Tarreaua5e37562011-12-16 17:06:15 +01007865 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007866 test->flags = ACL_TEST_F_VOL_HDR;
7867 return 1;
7868}
7869
Willy Tarreauc11416f2007-06-17 16:58:38 +02007870static int
7871acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7872 struct acl_expr *expr, struct acl_test *test)
7873{
7874 struct http_txn *txn = l7;
7875
Willy Tarreaub6866442008-07-14 23:54:42 +02007876 if (!txn)
7877 return 0;
7878
Willy Tarreau655dce92009-11-08 13:10:58 +01007879 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007880 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007881
Willy Tarreauc11416f2007-06-17 16:58:38 +02007882 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7883 /* ensure the indexes are not affected */
7884 return 0;
7885
7886 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test);
7887}
7888
7889static int
7890acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7891 struct acl_expr *expr, struct acl_test *test)
7892{
7893 struct http_txn *txn = l7;
7894
Willy Tarreaub6866442008-07-14 23:54:42 +02007895 if (!txn)
7896 return 0;
7897
Willy Tarreau655dce92009-11-08 13:10:58 +01007898 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007899 return 0;
7900
7901 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test);
7902}
7903
Willy Tarreau33a7e692007-06-10 19:45:56 +02007904/* 7. Check on HTTP header's integer value. The integer value is returned.
7905 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007906 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007907 */
7908static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007909acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007910 struct acl_expr *expr, struct acl_test *test)
7911{
7912 struct http_txn *txn = l7;
7913 struct hdr_idx *idx = &txn->hdr_idx;
7914 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007915
Willy Tarreaub6866442008-07-14 23:54:42 +02007916 if (!txn)
7917 return 0;
7918
Willy Tarreau33a7e692007-06-10 19:45:56 +02007919 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7920 /* search for header from the beginning */
7921 ctx->idx = 0;
7922
Willy Tarreau33a7e692007-06-10 19:45:56 +02007923 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7924 test->flags |= ACL_TEST_F_FETCH_MORE;
7925 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007926 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007927 return 1;
7928 }
7929
7930 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7931 test->flags |= ACL_TEST_F_VOL_HDR;
7932 return 0;
7933}
7934
Willy Tarreauc11416f2007-06-17 16:58:38 +02007935static int
7936acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7937 struct acl_expr *expr, struct acl_test *test)
7938{
7939 struct http_txn *txn = l7;
7940
Willy Tarreaub6866442008-07-14 23:54:42 +02007941 if (!txn)
7942 return 0;
7943
Willy Tarreau655dce92009-11-08 13:10:58 +01007944 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007945 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007946
Willy Tarreauc11416f2007-06-17 16:58:38 +02007947 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7948 /* ensure the indexes are not affected */
7949 return 0;
7950
7951 return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test);
7952}
7953
7954static int
7955acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7956 struct acl_expr *expr, struct acl_test *test)
7957{
7958 struct http_txn *txn = l7;
7959
Willy Tarreaub6866442008-07-14 23:54:42 +02007960 if (!txn)
7961 return 0;
7962
Willy Tarreau655dce92009-11-08 13:10:58 +01007963 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007964 return 0;
7965
7966 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test);
7967}
7968
Willy Tarreau106f9792009-09-19 07:54:16 +02007969/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7970 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7971 */
7972static int
7973acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7974 struct acl_expr *expr, struct acl_test *test)
7975{
7976 struct http_txn *txn = l7;
7977 struct hdr_idx *idx = &txn->hdr_idx;
7978 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7979
7980 if (!txn)
7981 return 0;
7982
7983 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7984 /* search for header from the beginning */
7985 ctx->idx = 0;
7986
Willy Tarreauf4362b32011-12-16 17:49:52 +01007987 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02007988 test->flags |= ACL_TEST_F_FETCH_MORE;
7989 test->flags |= ACL_TEST_F_VOL_HDR;
7990 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01007991 temp_pattern.type = PATTERN_TYPE_IP;
7992 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
7993 return 1;
7994 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02007995 }
7996
7997 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7998 test->flags |= ACL_TEST_F_VOL_HDR;
7999 return 0;
8000}
8001
8002static int
8003acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8004 struct acl_expr *expr, struct acl_test *test)
8005{
8006 struct http_txn *txn = l7;
8007
8008 if (!txn)
8009 return 0;
8010
Willy Tarreau655dce92009-11-08 13:10:58 +01008011 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008012 return 0;
8013
8014 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8015 /* ensure the indexes are not affected */
8016 return 0;
8017
8018 return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test);
8019}
8020
8021static int
8022acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8023 struct acl_expr *expr, struct acl_test *test)
8024{
8025 struct http_txn *txn = l7;
8026
8027 if (!txn)
8028 return 0;
8029
Willy Tarreau655dce92009-11-08 13:10:58 +01008030 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008031 return 0;
8032
8033 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test);
8034}
8035
Willy Tarreau737b0c12007-06-10 21:28:46 +02008036/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8037 * the first '/' after the possible hostname, and ends before the possible '?'.
8038 */
8039static int
8040acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8041 struct acl_expr *expr, struct acl_test *test)
8042{
8043 struct http_txn *txn = l7;
8044 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008045
Willy Tarreaub6866442008-07-14 23:54:42 +02008046 if (!txn)
8047 return 0;
8048
Willy Tarreau655dce92009-11-08 13:10:58 +01008049 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008050 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008051
Willy Tarreauc11416f2007-06-17 16:58:38 +02008052 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8053 /* ensure the indexes are not affected */
8054 return 0;
8055
Willy Tarreau962c3f42010-01-10 00:15:35 +01008056 end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008057 ptr = http_get_path(txn);
8058 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008059 return 0;
8060
8061 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008062 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008063
8064 while (ptr < end && *ptr != '?')
8065 ptr++;
8066
Willy Tarreau664092c2011-12-16 19:11:42 +01008067 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008068
8069 /* we do not need to set READ_ONLY because the data is in a buffer */
8070 test->flags = ACL_TEST_F_VOL_1ST;
8071 return 1;
8072}
8073
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008074static int
8075acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8076 struct acl_expr *expr, struct acl_test *test)
8077{
8078 struct buffer *req = s->req;
8079 struct http_txn *txn = &s->txn;
8080 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008081
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008082 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8083 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8084 */
8085
8086 if (!s || !req)
8087 return 0;
8088
Willy Tarreau655dce92009-11-08 13:10:58 +01008089 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008090 /* Already decoded as OK */
8091 test->flags |= ACL_TEST_F_SET_RES_PASS;
8092 return 1;
8093 }
8094
8095 /* Try to decode HTTP request */
8096 if (likely(req->lr < req->r))
8097 http_msg_analyzer(req, msg, &txn->hdr_idx);
8098
Willy Tarreau655dce92009-11-08 13:10:58 +01008099 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008100 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8101 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8102 return 1;
8103 }
8104 /* wait for final state */
8105 test->flags |= ACL_TEST_F_MAY_CHANGE;
8106 return 0;
8107 }
8108
8109 /* OK we got a valid HTTP request. We have some minor preparation to
8110 * perform so that further checks can rely on HTTP tests.
8111 */
Willy Tarreau962c3f42010-01-10 00:15:35 +01008112 txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008113 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8114 s->flags |= SN_REDIRECTABLE;
8115
8116 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8117 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8118 return 1;
8119 }
8120
8121 test->flags |= ACL_TEST_F_SET_RES_PASS;
8122 return 1;
8123}
8124
Willy Tarreau7f18e522010-10-22 20:04:13 +02008125/* return a valid test if the current request is the first one on the connection */
8126static int
8127acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8128 struct acl_expr *expr, struct acl_test *test)
8129{
8130 if (!s)
8131 return 0;
8132
8133 if (s->txn.flags & TX_NOT_FIRST)
8134 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8135 else
8136 test->flags |= ACL_TEST_F_SET_RES_PASS;
8137
8138 return 1;
8139}
8140
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008141static int
8142acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8143 struct acl_expr *expr, struct acl_test *test)
8144{
8145
8146 if (!s)
8147 return 0;
8148
8149 if (!get_http_auth(s))
8150 return 0;
8151
8152 test->ctx.a[0] = expr->arg.ul;
8153 test->ctx.a[1] = s->txn.auth.user;
8154 test->ctx.a[2] = s->txn.auth.pass;
8155
8156 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8157
8158 return 1;
8159}
Willy Tarreau8797c062007-05-07 00:55:35 +02008160
8161/************************************************************************/
8162/* All supported keywords must be declared here. */
8163/************************************************************************/
8164
8165/* Note: must not be declared <const> as its list will be overwritten */
8166static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008167 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8168
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008169 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008170 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8171 { "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 +02008172 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008173
Willy Tarreauc4262962010-05-10 23:42:40 +02008174 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008175 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8176 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8177 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8178 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8179 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8180 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008181 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008182 { "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 +02008183 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008184
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008185 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008186 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008187 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8188 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8189 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8190 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8191 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8192 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8193 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008194 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008195 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008196 { "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 +02008197
Willy Tarreauc4262962010-05-10 23:42:40 +02008198 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008199 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8200 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8201 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8202 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8203 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8204 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8205 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008206 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008207 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008208 { "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 +02008209
Willy Tarreauc4262962010-05-10 23:42:40 +02008210 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008211 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8212 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8213 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8214 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8215 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8216 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008217 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008218
Willy Tarreauf3d25982007-05-08 22:45:09 +02008219#if 0
Willy Tarreau8797c062007-05-07 00:55:35 +02008220 { "line", acl_parse_str, acl_fetch_line, acl_match_str },
8221 { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg },
8222 { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg },
8223 { "line_end", acl_parse_str, acl_fetch_line, acl_match_end },
8224 { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub },
8225 { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir },
8226 { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom },
8227
Willy Tarreau8797c062007-05-07 00:55:35 +02008228 { "cook", acl_parse_str, acl_fetch_cook, acl_match_str },
8229 { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg },
8230 { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg },
8231 { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end },
8232 { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub },
8233 { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir },
8234 { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom },
8235 { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst },
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008236#endif
Willy Tarreau8797c062007-05-07 00:55:35 +02008237
Willy Tarreau7f18e522010-10-22 20:04:13 +02008238 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8239 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8240 { "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 +02008241 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008242}};
8243
Willy Tarreau4a568972010-05-12 08:08:50 +02008244/************************************************************************/
8245/* The code below is dedicated to pattern fetching and matching */
8246/************************************************************************/
8247
Willy Tarreaue428fb72011-12-16 21:50:30 +01008248/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008249static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008250pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8251 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008252{
8253 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008254
Willy Tarreaue428fb72011-12-16 21:50:30 +01008255 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8256 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008257}
8258
David Cournapeau16023ee2010-12-23 20:55:41 +09008259/*
8260 * Given a path string and its length, find the position of beginning of the
8261 * query string. Returns NULL if no query string is found in the path.
8262 *
8263 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8264 *
8265 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8266 */
8267static inline char *find_query_string(char *path, size_t path_l)
8268{
8269 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008270
David Cournapeau16023ee2010-12-23 20:55:41 +09008271 p = memchr(path, '?', path_l);
8272 return p ? p + 1 : NULL;
8273}
8274
8275static inline int is_param_delimiter(char c)
8276{
8277 return c == '&' || c == ';';
8278}
8279
8280/*
8281 * Given a url parameter, find the starting position of the first occurence,
8282 * or NULL if the parameter is not found.
8283 *
8284 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8285 * the function will return query_string+8.
8286 */
8287static char*
8288find_url_param_pos(char* query_string, size_t query_string_l,
8289 char* url_param_name, size_t url_param_name_l)
8290{
8291 char *pos, *last;
8292
8293 pos = query_string;
8294 last = query_string + query_string_l - url_param_name_l - 1;
8295
8296 while (pos <= last) {
8297 if (pos[url_param_name_l] == '=') {
8298 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8299 return pos;
8300 pos += url_param_name_l + 1;
8301 }
8302 while (pos <= last && !is_param_delimiter(*pos))
8303 pos++;
8304 pos++;
8305 }
8306 return NULL;
8307}
8308
8309/*
8310 * Given a url parameter name, returns its value and size into *value and
8311 * *value_l respectively, and returns non-zero. If the parameter is not found,
8312 * zero is returned and value/value_l are not touched.
8313 */
8314static int
8315find_url_param_value(char* path, size_t path_l,
8316 char* url_param_name, size_t url_param_name_l,
8317 char** value, size_t* value_l)
8318{
8319 char *query_string, *qs_end;
8320 char *arg_start;
8321 char *value_start, *value_end;
8322
8323 query_string = find_query_string(path, path_l);
8324 if (!query_string)
8325 return 0;
8326
8327 qs_end = path + path_l;
8328 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8329 url_param_name, url_param_name_l);
8330 if (!arg_start)
8331 return 0;
8332
8333 value_start = arg_start + url_param_name_l + 1;
8334 value_end = value_start;
8335
8336 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8337 value_end++;
8338
8339 *value = value_start;
8340 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008341 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008342}
8343
8344static int
8345pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8346 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8347{
8348 struct http_txn *txn = l7;
8349 struct http_msg *msg = &txn->req;
8350 char *url_param_value;
8351 size_t url_param_value_l;
8352
8353 if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
8354 arg_p->data.str.str, arg_p->data.str.len,
8355 &url_param_value, &url_param_value_l))
8356 return 0;
8357
8358 data->str.str = url_param_value;
8359 data->str.len = url_param_value_l;
8360 return 1;
8361}
8362
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008363/* Try to find the last occurrence of a cookie name in a cookie header value.
8364 * The pointer and size of the last occurrence of the cookie value is returned
8365 * into *value and *value_l, and the function returns non-zero if the value was
8366 * found. Otherwise if the cookie was not found, zero is returned and neither
8367 * value nor value_l are touched. The input hdr string should begin at the
8368 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8369 * value may represent a list of values (cookie headers).
8370 */
8371static int
8372extract_cookie_value(char *hdr, size_t hdr_l,
8373 char *cookie_name, size_t cookie_name_l, int list,
8374 char **value, size_t *value_l)
8375{
8376 int found = 0;
8377 char *equal, *att_end, *att_beg, *hdr_end, *val_beg, *val_end;
8378 char *next;
8379
8380 /* Note that multiple cookies may be delimited with semi-colons, so we
8381 * also have to loop on this.
8382 */
8383
8384 /* we search at least a cookie name followed by an equal, and more
8385 * generally something like this :
8386 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8387 */
8388 if (hdr_l < cookie_name_l + 1)
8389 return 0;
8390
8391 hdr_end = hdr + hdr_l;
8392
8393 for (att_beg = hdr; att_beg < hdr_end; att_beg = next + 1) {
8394 /* Iterate through all cookies on this line */
8395
8396 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8397 att_beg++;
8398
8399 /* find att_end : this is the first character after the last non
8400 * space before the equal. It may be equal to hdr_end.
8401 */
8402 equal = att_end = att_beg;
8403
8404 while (equal < hdr_end) {
8405 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8406 break;
8407 if (http_is_spht[(unsigned char)*equal++])
8408 continue;
8409 att_end = equal;
8410 }
8411
8412 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8413 * is between <att_beg> and <equal>, both may be identical.
8414 */
8415
8416 /* look for end of cookie if there is an equal sign */
8417 if (equal < hdr_end && *equal == '=') {
8418 /* look for the beginning of the value */
8419 val_beg = equal + 1;
8420 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8421 val_beg++;
8422
8423 /* find the end of the value, respecting quotes */
8424 next = find_cookie_value_end(val_beg, hdr_end);
8425
8426 /* make val_end point to the first white space or delimitor after the value */
8427 val_end = next;
8428 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8429 val_end--;
8430 } else {
8431 val_beg = val_end = next = equal;
8432 }
8433
8434 /* We have nothing to do with attributes beginning with '$'. However,
8435 * they will automatically be removed if a header before them is removed,
8436 * since they're supposed to be linked together.
8437 */
8438 if (*att_beg == '$')
8439 continue;
8440
8441 /* Ignore cookies with no equal sign */
8442 if (equal == next)
8443 continue;
8444
8445 /* Now we have the cookie name between att_beg and att_end, and
8446 * its value between val_beg and val_end.
8447 */
8448
8449 if (att_end - att_beg == cookie_name_l &&
8450 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8451 found = 1;
8452 *value = val_beg;
8453 *value_l = val_end - val_beg;
8454 /* right now we want to catch the last occurrence
8455 * of the cookie, so let's go on searching.
8456 */
8457 }
8458
8459 /* Set-Cookie headers only have the name in the first attr=value part */
8460 if (!list)
8461 break;
8462 }
8463
8464 return found;
8465}
8466
8467/* Try to find in request or response message is in <msg> and whose transaction
8468 * is in <txn> the last occurrence of a cookie name in all cookie header values
8469 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8470 * pointer and size of the last occurrence of the cookie value is returned into
8471 * <value> and <value_l>, and the function returns non-zero if the value was
8472 * found. Otherwise if the cookie was not found, zero is returned and neither
8473 * value nor value_l are touched. The input hdr string should begin at the
8474 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8475 * value may represent a list of values (cookie headers).
8476 */
8477
8478static int
8479find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8480 const char *hdr_name, int hdr_name_len,
8481 char *cookie_name, size_t cookie_name_l, int list,
8482 char **value, size_t *value_l)
8483{
8484 struct hdr_ctx ctx;
8485 int found = 0;
8486
8487 ctx.idx = 0;
8488 while (http_find_header2(hdr_name, hdr_name_len, msg->sol, &txn->hdr_idx, &ctx)) {
8489 if (ctx.vlen < cookie_name_l + 1)
8490 continue;
8491
8492 found |= extract_cookie_value(ctx.line + ctx.val, ctx.vlen,
8493 cookie_name, cookie_name_l, 1,
8494 value, value_l);
8495 }
8496 return found;
8497}
8498
8499static int
8500pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8501 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8502{
8503 struct http_txn *txn = l7;
8504 struct http_msg *msg = &txn->req;
8505 char *cookie_value;
8506 size_t cookie_value_l;
8507 int found = 0;
8508
8509 found = find_cookie_value(msg, txn, "Cookie", 6,
8510 arg_p->data.str.str, arg_p->data.str.len, 1,
8511 &cookie_value, &cookie_value_l);
8512 if (found) {
8513 data->str.str = cookie_value;
8514 data->str.len = cookie_value_l;
8515 }
8516
8517 return found;
8518}
8519
8520
8521static int
8522pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8523 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8524{
8525 struct http_txn *txn = l7;
8526 struct http_msg *msg = &txn->rsp;
8527 char *cookie_value;
8528 size_t cookie_value_l;
8529 int found = 0;
8530
8531 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8532 arg_p->data.str.str, arg_p->data.str.len, 1,
8533 &cookie_value, &cookie_value_l);
8534 if (found) {
8535 data->str.str = cookie_value;
8536 data->str.len = cookie_value_l;
8537 }
8538
8539 return found;
8540}
8541
Emeric Brun485479d2010-09-23 18:02:19 +02008542
Willy Tarreau4a568972010-05-12 08:08:50 +02008543/************************************************************************/
8544/* All supported keywords must be declared here. */
8545/************************************************************************/
8546/* Note: must not be declared <const> as its list will be overwritten */
8547static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008548 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008549 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008550 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8551 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008552 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008553}};
8554
Willy Tarreau8797c062007-05-07 00:55:35 +02008555
8556__attribute__((constructor))
8557static void __http_protocol_init(void)
8558{
8559 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008560 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008561}
8562
8563
Willy Tarreau58f10d72006-12-04 02:26:12 +01008564/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008565 * Local variables:
8566 * c-indent-level: 8
8567 * c-basic-offset: 8
8568 * End:
8569 */