blob: 7f65be47b6886c5a50de17881d342a6faa12a8e2 [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
Cyril Bonté19979e12012-04-04 12:57:21 +0200191/* status codes available for the stats admin page (strictly 4 chars length) */
192const char *stat_status_codes[STAT_STATUS_SIZE] = {
193 [STAT_STATUS_DENY] = "DENY",
194 [STAT_STATUS_DONE] = "DONE",
195 [STAT_STATUS_ERRP] = "ERRP",
196 [STAT_STATUS_EXCD] = "EXCD",
197 [STAT_STATUS_NONE] = "NONE",
198 [STAT_STATUS_PART] = "PART",
199 [STAT_STATUS_UNKN] = "UNKN",
200};
201
202
Willy Tarreau80587432006-12-24 17:47:20 +0100203/* We must put the messages here since GCC cannot initialize consts depending
204 * on strlen().
205 */
206struct chunk http_err_chunks[HTTP_ERR_SIZE];
207
Willy Tarreau42250582007-04-01 01:30:43 +0200208#define FD_SETS_ARE_BITFIELDS
209#ifdef FD_SETS_ARE_BITFIELDS
210/*
211 * This map is used with all the FD_* macros to check whether a particular bit
212 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
213 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
214 * byte should be encoded. Be careful to always pass bytes from 0 to 255
215 * exclusively to the macros.
216 */
217fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
218fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
219
220#else
221#error "Check if your OS uses bitfields for fd_sets"
222#endif
223
Willy Tarreau80587432006-12-24 17:47:20 +0100224void init_proto_http()
225{
Willy Tarreau42250582007-04-01 01:30:43 +0200226 int i;
227 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100228 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200229
Willy Tarreau80587432006-12-24 17:47:20 +0100230 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
231 if (!http_err_msgs[msg]) {
232 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
233 abort();
234 }
235
236 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
237 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
238 }
Willy Tarreau42250582007-04-01 01:30:43 +0200239
240 /* initialize the log header encoding map : '{|}"#' should be encoded with
241 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
242 * URL encoding only requires '"', '#' to be encoded as well as non-
243 * printable characters above.
244 */
245 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
246 memset(url_encode_map, 0, sizeof(url_encode_map));
247 for (i = 0; i < 32; i++) {
248 FD_SET(i, hdr_encode_map);
249 FD_SET(i, url_encode_map);
250 }
251 for (i = 127; i < 256; i++) {
252 FD_SET(i, hdr_encode_map);
253 FD_SET(i, url_encode_map);
254 }
255
256 tmp = "\"#{|}";
257 while (*tmp) {
258 FD_SET(*tmp, hdr_encode_map);
259 tmp++;
260 }
261
262 tmp = "\"#";
263 while (*tmp) {
264 FD_SET(*tmp, url_encode_map);
265 tmp++;
266 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200267
268 /* memory allocations */
269 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200270 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100271 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100272}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200273
Willy Tarreau53b6c742006-12-17 13:37:46 +0100274/*
275 * We have 26 list of methods (1 per first letter), each of which can have
276 * up to 3 entries (2 valid, 1 null).
277 */
278struct http_method_desc {
279 http_meth_t meth;
280 int len;
281 const char text[8];
282};
283
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100284const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100285 ['C' - 'A'] = {
286 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
287 },
288 ['D' - 'A'] = {
289 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
290 },
291 ['G' - 'A'] = {
292 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
293 },
294 ['H' - 'A'] = {
295 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
296 },
297 ['P' - 'A'] = {
298 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
299 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
300 },
301 ['T' - 'A'] = {
302 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
303 },
304 /* rest is empty like this :
305 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
306 */
307};
308
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100309/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200310 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100311 * RFC2616 for those chars.
312 */
313
314const char http_is_spht[256] = {
315 [' '] = 1, ['\t'] = 1,
316};
317
318const char http_is_crlf[256] = {
319 ['\r'] = 1, ['\n'] = 1,
320};
321
322const char http_is_lws[256] = {
323 [' '] = 1, ['\t'] = 1,
324 ['\r'] = 1, ['\n'] = 1,
325};
326
327const char http_is_sep[256] = {
328 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
329 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
330 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
331 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
332 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
333};
334
335const char http_is_ctl[256] = {
336 [0 ... 31] = 1,
337 [127] = 1,
338};
339
340/*
341 * A token is any ASCII char that is neither a separator nor a CTL char.
342 * Do not overwrite values in assignment since gcc-2.95 will not handle
343 * them correctly. Instead, define every non-CTL char's status.
344 */
345const char http_is_token[256] = {
346 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
347 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
348 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
349 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
350 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
351 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
352 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
353 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
354 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
355 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
356 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
357 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
358 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
359 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
360 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
361 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
362 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
363 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
364 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
365 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
366 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
367 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
368 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
369 ['|'] = 1, ['}'] = 0, ['~'] = 1,
370};
371
372
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100373/*
374 * An http ver_token is any ASCII which can be found in an HTTP version,
375 * which includes 'H', 'T', 'P', '/', '.' and any digit.
376 */
377const char http_is_ver_token[256] = {
378 ['.'] = 1, ['/'] = 1,
379 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
380 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
381 ['H'] = 1, ['P'] = 1, ['T'] = 1,
382};
383
384
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100385/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100386 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
387 */
388#if defined(DEBUG_FSM)
389static void http_silent_debug(int line, struct session *s)
390{
391 int size = 0;
392 size += snprintf(trash + size, sizeof(trash) - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100393 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
Willy Tarreaua458b672012-03-05 11:17:50 +0100396 s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->p, s->req->o, s->req->to_forward, s->txn.flags);
Willy Tarreaue988a792010-01-04 21:13:14 +0100397 write(-1, trash, size);
398 size = 0;
399 size += snprintf(trash + size, sizeof(trash) - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100400 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
Willy Tarreaua458b672012-03-05 11:17:50 +0100403 s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->p, s->rep->o, s->rep->to_forward);
Willy Tarreaue988a792010-01-04 21:13:14 +0100404
405 write(-1, trash, size);
406}
407#else
408#define http_silent_debug(l,s) do { } while (0)
409#endif
410
411/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100412 * Adds a header and its CRLF at the tail of the message's buffer, just before
413 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100414 * The header is also automatically added to the index <hdr_idx>, and the end
415 * of headers is automatically adjusted. The number of bytes added is returned
416 * on success, otherwise <0 is returned indicating an error.
417 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100418int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100419{
420 int bytes, len;
421
422 len = strlen(text);
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100423 bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100424 if (!bytes)
425 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100426 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100427 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
428}
429
430/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100431 * Adds a header and its CRLF at the tail of the message's buffer, just before
432 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100433 * the buffer is only opened and the space reserved, but nothing is copied.
434 * The header is also automatically added to the index <hdr_idx>, and the end
435 * of headers is automatically adjusted. The number of bytes added is returned
436 * on success, otherwise <0 is returned indicating an error.
437 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100438int http_header_add_tail2(struct http_msg *msg,
439 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100440{
441 int bytes;
442
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100443 bytes = buffer_insert_line2(msg->buf, msg->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100444 if (!bytes)
445 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100446 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100447 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
448}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449
450/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100451 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
452 * If so, returns the position of the first non-space character relative to
453 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
454 * to return a pointer to the place after the first space. Returns 0 if the
455 * header name does not match. Checks are case-insensitive.
456 */
457int http_header_match2(const char *hdr, const char *end,
458 const char *name, int len)
459{
460 const char *val;
461
462 if (hdr + len >= end)
463 return 0;
464 if (hdr[len] != ':')
465 return 0;
466 if (strncasecmp(hdr, name, len) != 0)
467 return 0;
468 val = hdr + len + 1;
469 while (val < end && HTTP_IS_SPHT(*val))
470 val++;
471 if ((val >= end) && (len + 2 <= end - hdr))
472 return len + 2; /* we may replace starting from second space */
473 return val - hdr;
474}
475
Willy Tarreau68085d82010-01-18 14:54:04 +0100476/* Find the end of the header value contained between <s> and <e>. See RFC2616,
477 * par 2.2 for more information. Note that it requires a valid header to return
478 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200479 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100480char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200481{
482 int quoted, qdpair;
483
484 quoted = qdpair = 0;
485 for (; s < e; s++) {
486 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200487 else if (quoted) {
488 if (*s == '\\') qdpair = 1;
489 else if (*s == '"') quoted = 0;
490 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200491 else if (*s == '"') quoted = 1;
492 else if (*s == ',') return s;
493 }
494 return s;
495}
496
497/* Find the first or next occurrence of header <name> in message buffer <sol>
498 * using headers index <idx>, and return it in the <ctx> structure. This
499 * structure holds everything necessary to use the header and find next
500 * occurrence. If its <idx> member is 0, the header is searched from the
501 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 * 1 when it finds a value, and 0 when there is no more. It is designed to work
503 * with headers defined as comma-separated lists. As a special case, if ctx->val
504 * is NULL when searching for a new values of a header, the current header is
505 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200506 */
507int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100508 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200509 struct hdr_ctx *ctx)
510{
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 char *eol, *sov;
512 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513
Willy Tarreau68085d82010-01-18 14:54:04 +0100514 cur_idx = ctx->idx;
515 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200516 /* We have previously returned a value, let's search
517 * another one on the same line.
518 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200520 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100521 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 eol = sol + idx->v[cur_idx].len;
523
524 if (sov >= eol)
525 /* no more values in this header */
526 goto next_hdr;
527
Willy Tarreau68085d82010-01-18 14:54:04 +0100528 /* values remaining for this header, skip the comma but save it
529 * for later use (eg: for header deletion).
530 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 sov++;
532 while (sov < eol && http_is_lws[(unsigned char)*sov])
533 sov++;
534
535 goto return_hdr;
536 }
537
538 /* first request for this header */
539 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100540 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 while (cur_idx) {
543 eol = sol + idx->v[cur_idx].len;
544
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200545 if (len == 0) {
546 /* No argument was passed, we want any header.
547 * To achieve this, we simply build a fake request. */
548 while (sol + len < eol && sol[len] != ':')
549 len++;
550 name = sol;
551 }
552
Willy Tarreau33a7e692007-06-10 19:45:56 +0200553 if ((len < eol - sol) &&
554 (sol[len] == ':') &&
555 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100556 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200557 sov = sol + len + 1;
558 while (sov < eol && http_is_lws[(unsigned char)*sov])
559 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100560
Willy Tarreau33a7e692007-06-10 19:45:56 +0200561 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100562 ctx->prev = old_idx;
563 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200564 ctx->idx = cur_idx;
565 ctx->val = sov - sol;
566
567 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200568 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200569 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200570 eol--;
571 ctx->tws++;
572 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200573 ctx->vlen = eol - sov;
574 return 1;
575 }
576 next_hdr:
577 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100578 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200579 cur_idx = idx->v[cur_idx].next;
580 }
581 return 0;
582}
583
584int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100585 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200586 struct hdr_ctx *ctx)
587{
588 return http_find_header2(name, strlen(name), sol, idx, ctx);
589}
590
Willy Tarreau68085d82010-01-18 14:54:04 +0100591/* Remove one value of a header. This only works on a <ctx> returned by one of
592 * the http_find_header functions. The value is removed, as well as surrounding
593 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100594 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100595 * message <msg>. The new index is returned. If it is zero, it means there is
596 * no more header, so any processing may stop. The ctx is always left in a form
597 * that can be handled by http_find_header2() to find next occurrence.
598 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100599int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100600{
601 int cur_idx = ctx->idx;
602 char *sol = ctx->line;
603 struct hdr_idx_elem *hdr;
604 int delta, skip_comma;
605
606 if (!cur_idx)
607 return 0;
608
609 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200610 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100611 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100612 delta = buffer_replace2(msg->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 http_msg_move_end(msg, delta);
614 idx->used--;
615 hdr->len = 0; /* unused entry */
616 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100617 if (idx->tail == ctx->idx)
618 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100619 ctx->idx = ctx->prev; /* walk back to the end of previous header */
620 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
621 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200622 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100623 return ctx->idx;
624 }
625
626 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200627 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
628 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100629 */
630
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200631 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100632 delta = buffer_replace2(msg->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 NULL, 0);
635 hdr->len += delta;
636 http_msg_move_end(msg, delta);
637 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200638 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100639 return ctx->idx;
640}
641
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100642/* This function handles a server error at the stream interface level. The
643 * stream interface is assumed to be already in a closed state. An optional
644 * message is copied into the input buffer, and an HTTP status code stored.
645 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100646 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100648static void http_server_error(struct session *t, struct stream_interface *si,
649 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650{
Willy Tarreaud5fd51c2010-01-22 14:17:47 +0100651 buffer_auto_read(si->ob);
652 buffer_abort(si->ob);
653 buffer_auto_close(si->ob);
654 buffer_erase(si->ob);
Willy Tarreau520d95e2009-09-19 21:04:57 +0200655 buffer_auto_close(si->ib);
Willy Tarreau90deb182010-01-07 00:20:41 +0100656 buffer_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100657 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100658 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200659 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660 }
661 if (!(t->flags & SN_ERR_MASK))
662 t->flags |= err;
663 if (!(t->flags & SN_FINST_MASK))
664 t->flags |= finst;
665}
666
Willy Tarreau80587432006-12-24 17:47:20 +0100667/* This function returns the appropriate error location for the given session
668 * and message.
669 */
670
671struct chunk *error_message(struct session *s, int msgnum)
672{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200673 if (s->be->errmsg[msgnum].str)
674 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100675 else if (s->fe->errmsg[msgnum].str)
676 return &s->fe->errmsg[msgnum];
677 else
678 return &http_err_chunks[msgnum];
679}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680
Willy Tarreau53b6c742006-12-17 13:37:46 +0100681/*
682 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
683 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
684 */
685static http_meth_t find_http_meth(const char *str, const int len)
686{
687 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100688 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100689
690 m = ((unsigned)*str - 'A');
691
692 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100693 for (h = http_methods[m]; h->len > 0; h++) {
694 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100695 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100696 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100698 };
699 return HTTP_METH_OTHER;
700 }
701 return HTTP_METH_NONE;
702
703}
704
Willy Tarreau21d2af32008-02-14 20:25:24 +0100705/* Parse the URI from the given transaction (which is assumed to be in request
706 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
707 * It is returned otherwise.
708 */
709static char *
710http_get_path(struct http_txn *txn)
711{
712 char *ptr, *end;
713
Willy Tarreau3a215be2012-03-09 21:39:51 +0100714 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100715 end = ptr + txn->req.sl.rq.u_l;
716
717 if (ptr >= end)
718 return NULL;
719
720 /* RFC2616, par. 5.1.2 :
721 * Request-URI = "*" | absuri | abspath | authority
722 */
723
724 if (*ptr == '*')
725 return NULL;
726
727 if (isalpha((unsigned char)*ptr)) {
728 /* this is a scheme as described by RFC3986, par. 3.1 */
729 ptr++;
730 while (ptr < end &&
731 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
732 ptr++;
733 /* skip '://' */
734 if (ptr == end || *ptr++ != ':')
735 return NULL;
736 if (ptr == end || *ptr++ != '/')
737 return NULL;
738 if (ptr == end || *ptr++ != '/')
739 return NULL;
740 }
741 /* skip [user[:passwd]@]host[:[port]] */
742
743 while (ptr < end && *ptr != '/')
744 ptr++;
745
746 if (ptr == end)
747 return NULL;
748
749 /* OK, we got the '/' ! */
750 return ptr;
751}
752
Willy Tarreauefb453c2008-10-26 20:49:47 +0100753/* Returns a 302 for a redirectable request. This may only be called just after
754 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
755 * left unchanged and will follow normal proxy processing.
756 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100758{
759 struct http_txn *txn;
760 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 char *path;
763 int len;
764
765 /* 1: create the response header */
766 rdr.len = strlen(HTTP_302);
767 rdr.str = trash;
Willy Tarreau59e0b0f2010-01-09 21:29:23 +0100768 rdr.size = sizeof(trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100769 memcpy(rdr.str, HTTP_302, rdr.len);
770
Willy Tarreau827aee92011-03-10 16:55:02 +0100771 srv = target_srv(&s->target);
772
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100774 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775 return;
776
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100777 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100778 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
779 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
780 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100781 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100782
783 /* 3: add the request URI */
784 txn = &s->txn;
785 path = http_get_path(txn);
786 if (!path)
787 return;
788
Willy Tarreau3a215be2012-03-09 21:39:51 +0100789 len = txn->req.sl.rq.u_l + (s->req->p + txn->req.sol + txn->req.sl.rq.u) - path;
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200790 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100791 return;
792
793 memcpy(rdr.str + rdr.len, path, len);
794 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100795
796 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
797 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
798 rdr.len += 29;
799 } else {
800 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
801 rdr.len += 23;
802 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100803
804 /* prepare to return without error. */
Willy Tarreau99126c32008-11-27 10:30:51 +0100805 si->shutr(si);
806 si->shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100807 si->err_type = SI_ET_NONE;
808 si->err_loc = NULL;
809 si->state = SI_ST_CLO;
810
811 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100812 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813
814 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100815 if (srv)
816 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100817}
818
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100819/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100820 * that the server side is closed. Note that err_type is actually a
821 * bitmask, where almost only aborts may be cumulated with other
822 * values. We consider that aborted operations are more important
823 * than timeouts or errors due to the fact that nobody else in the
824 * logs might explain incomplete retries. All others should avoid
825 * being cumulated. It should normally not be possible to have multiple
826 * aborts at once, but just in case, the first one in sequence is reported.
827 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100828void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100829{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100830 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831
832 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100833 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
834 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100836 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
837 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100838 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100839 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
840 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100841 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100842 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
843 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100844 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100845 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
846 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100847 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100848 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
849 503, error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100851 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
852 500, error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100853}
854
Willy Tarreau42250582007-04-01 01:30:43 +0200855extern const char sess_term_cond[8];
856extern const char sess_fin_state[8];
857extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200858struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200859struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100860struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100861
Willy Tarreau117f59e2007-03-04 18:17:17 +0100862/*
863 * Capture headers from message starting at <som> according to header list
864 * <cap_hdr>, and fill the <idx> structure appropriately.
865 */
866void capture_headers(char *som, struct hdr_idx *idx,
867 char **cap, struct cap_hdr *cap_hdr)
868{
869 char *eol, *sol, *col, *sov;
870 int cur_idx;
871 struct cap_hdr *h;
872 int len;
873
874 sol = som + hdr_idx_first_pos(idx);
875 cur_idx = hdr_idx_first_idx(idx);
876
877 while (cur_idx) {
878 eol = sol + idx->v[cur_idx].len;
879
880 col = sol;
881 while (col < eol && *col != ':')
882 col++;
883
884 sov = col + 1;
885 while (sov < eol && http_is_lws[(unsigned char)*sov])
886 sov++;
887
888 for (h = cap_hdr; h; h = h->next) {
889 if ((h->namelen == col - sol) &&
890 (strncasecmp(sol, h->name, h->namelen) == 0)) {
891 if (cap[h->index] == NULL)
892 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200893 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100894
895 if (cap[h->index] == NULL) {
896 Alert("HTTP capture : out of memory.\n");
897 continue;
898 }
899
900 len = eol - sov;
901 if (len > h->len)
902 len = h->len;
903
904 memcpy(cap[h->index], sov, len);
905 cap[h->index][len]=0;
906 }
907 }
908 sol = eol + idx->v[cur_idx].cr + 1;
909 cur_idx = idx->v[cur_idx].next;
910 }
911}
912
913
Willy Tarreau42250582007-04-01 01:30:43 +0200914/* either we find an LF at <ptr> or we jump to <bad>.
915 */
916#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
917
918/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
919 * otherwise to <http_msg_ood> with <state> set to <st>.
920 */
921#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
922 ptr++; \
923 if (likely(ptr < end)) \
924 goto good; \
925 else { \
926 state = (st); \
927 goto http_msg_ood; \
928 } \
929 } while (0)
930
931
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100933 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100934 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
935 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
936 * will give undefined results.
937 * Note that it is upon the caller's responsibility to ensure that ptr < end,
938 * and that msg->sol points to the beginning of the response.
939 * If a complete line is found (which implies that at least one CR or LF is
940 * found before <end>, the updated <ptr> is returned, otherwise NULL is
941 * returned indicating an incomplete line (which does not mean that parts have
942 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
943 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
944 * upon next call.
945 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200946 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100947 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
948 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200949 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100950 */
Willy Tarreaue69eada2008-01-27 00:34:10 +0100951const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf,
952 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100953 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100954{
Willy Tarreau62f791e2012-03-09 11:32:30 +0100955 const char *msg_start = msg->buf->p;
956
Willy Tarreau8973c702007-01-21 23:58:29 +0100957 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200959 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100960 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100961 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
962
963 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100964 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100965 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
966 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100967 state = HTTP_MSG_ERROR;
968 break;
969
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200971 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100972 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100973 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100974 goto http_msg_rpcode;
975 }
976 if (likely(HTTP_IS_SPHT(*ptr)))
977 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
978 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100979 state = HTTP_MSG_ERROR;
980 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100981
Willy Tarreau8973c702007-01-21 23:58:29 +0100982 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200983 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100984 if (likely(!HTTP_IS_LWS(*ptr)))
985 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
986
987 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100988 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100989 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
990 }
991
992 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100993 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 http_msg_rsp_reason:
995 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100996 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100997 msg->sl.st.r_l = 0;
998 goto http_msg_rpline_eol;
999
Willy Tarreau8973c702007-01-21 23:58:29 +01001000 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001001 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001003 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 goto http_msg_rpreason;
1005 }
1006 if (likely(HTTP_IS_SPHT(*ptr)))
1007 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1008 /* so it's a CR/LF, so there is no reason phrase */
1009 goto http_msg_rsp_reason;
1010
Willy Tarreau8973c702007-01-21 23:58:29 +01001011 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001012 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001013 if (likely(!HTTP_IS_CRLF(*ptr)))
1014 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001015 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001016 http_msg_rpline_eol:
1017 /* We have seen the end of line. Note that we do not
1018 * necessarily have the \n yet, but at least we know that we
1019 * have EITHER \r OR \n, otherwise the response would not be
1020 * complete. We can then record the response length and return
1021 * to the caller which will be able to register it.
1022 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001023 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 return ptr;
1025
1026#ifdef DEBUG_FULL
1027 default:
1028 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1029 exit(1);
1030#endif
1031 }
1032
1033 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001034 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001035 if (ret_state)
1036 *ret_state = state;
1037 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001038 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001039 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001040}
1041
Willy Tarreau8973c702007-01-21 23:58:29 +01001042/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001043 * This function parses a request line between <ptr> and <end>, starting with
1044 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1045 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1046 * will give undefined results.
1047 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1048 * and that msg->sol points to the beginning of the request.
1049 * If a complete line is found (which implies that at least one CR or LF is
1050 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1051 * returned indicating an incomplete line (which does not mean that parts have
1052 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1053 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1054 * upon next call.
1055 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001056 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001057 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1058 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001059 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001060 */
Willy Tarreaue69eada2008-01-27 00:34:10 +01001061const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf,
1062 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001063 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001064{
Willy Tarreau62f791e2012-03-09 11:32:30 +01001065 const char *msg_start = msg->buf->p;
1066
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001067 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001069 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001070 if (likely(HTTP_IS_TOKEN(*ptr)))
1071 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001074 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1076 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 if (likely(HTTP_IS_CRLF(*ptr))) {
1079 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001080 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001082 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001084 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001085 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 msg->sl.rq.v_l = 0;
1088 goto http_msg_rqline_eol;
1089 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001090 state = HTTP_MSG_ERROR;
1091 break;
1092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001094 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001096 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001097 goto http_msg_rquri;
1098 }
1099 if (likely(HTTP_IS_SPHT(*ptr)))
1100 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1101 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1102 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001103
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001105 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001106 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001110 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001111 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1112 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001114 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001115 /* non-ASCII chars are forbidden unless option
1116 * accept-invalid-http-request is enabled in the frontend.
1117 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001118 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001119 if (msg->err_pos < -1)
1120 goto invalid_char;
1121 if (msg->err_pos == -1)
1122 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001123 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1124 }
1125
1126 if (likely(HTTP_IS_CRLF(*ptr))) {
1127 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1128 goto http_msg_req09_uri_e;
1129 }
1130
1131 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001132 invalid_char:
1133 msg->err_pos = ptr - msg_buf;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001134 state = HTTP_MSG_ERROR;
1135 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001136
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001137 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001138 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001139 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001140 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001141 goto http_msg_rqver;
1142 }
1143 if (likely(HTTP_IS_SPHT(*ptr)))
1144 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1145 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1146 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001147
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001149 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001150 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001151 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001152
1153 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001154 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001155 http_msg_rqline_eol:
1156 /* We have seen the end of line. Note that we do not
1157 * necessarily have the \n yet, but at least we know that we
1158 * have EITHER \r OR \n, otherwise the request would not be
1159 * complete. We can then record the request length and return
1160 * to the caller which will be able to register it.
1161 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001162 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001163 return ptr;
1164 }
1165
1166 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001167 state = HTTP_MSG_ERROR;
1168 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001170#ifdef DEBUG_FULL
1171 default:
1172 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1173 exit(1);
1174#endif
1175 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001176
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001177 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001178 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001179 if (ret_state)
1180 *ret_state = state;
1181 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001182 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001185
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001186/*
1187 * Returns the data from Authorization header. Function may be called more
1188 * than once so data is stored in txn->auth_data. When no header is found
1189 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1190 * searching again for something we are unable to find anyway.
1191 */
1192
1193char get_http_auth_buff[BUFSIZE];
1194
1195int
1196get_http_auth(struct session *s)
1197{
1198
1199 struct http_txn *txn = &s->txn;
1200 struct chunk auth_method;
1201 struct hdr_ctx ctx;
1202 char *h, *p;
1203 int len;
1204
1205#ifdef DEBUG_AUTH
1206 printf("Auth for session %p: %d\n", s, txn->auth.method);
1207#endif
1208
1209 if (txn->auth.method == HTTP_AUTH_WRONG)
1210 return 0;
1211
1212 if (txn->auth.method)
1213 return 1;
1214
1215 txn->auth.method = HTTP_AUTH_WRONG;
1216
1217 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001218
1219 if (txn->flags & TX_USE_PX_CONN) {
1220 h = "Proxy-Authorization";
1221 len = strlen(h);
1222 } else {
1223 h = "Authorization";
1224 len = strlen(h);
1225 }
1226
Willy Tarreau3a215be2012-03-09 21:39:51 +01001227 if (!http_find_header2(h, len, s->req->p + txn->req.sol, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001228 return 0;
1229
1230 h = ctx.line + ctx.val;
1231
1232 p = memchr(h, ' ', ctx.vlen);
1233 if (!p || p == h)
1234 return 0;
1235
1236 chunk_initlen(&auth_method, h, 0, p-h);
1237 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1238
1239 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1240
1241 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1242 get_http_auth_buff, BUFSIZE - 1);
1243
1244 if (len < 0)
1245 return 0;
1246
1247
1248 get_http_auth_buff[len] = '\0';
1249
1250 p = strchr(get_http_auth_buff, ':');
1251
1252 if (!p)
1253 return 0;
1254
1255 txn->auth.user = get_http_auth_buff;
1256 *p = '\0';
1257 txn->auth.pass = p+1;
1258
1259 txn->auth.method = HTTP_AUTH_BASIC;
1260 return 1;
1261 }
1262
1263 return 0;
1264}
1265
Willy Tarreau58f10d72006-12-04 02:26:12 +01001266
Willy Tarreau8973c702007-01-21 23:58:29 +01001267/*
1268 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001269 * depending on the initial msg->msg_state. The caller is responsible for
1270 * ensuring that the message does not wrap. The function can be preempted
1271 * everywhere when data are missing and recalled at the exact same location
1272 * with no information loss. The message may even be realigned between two
1273 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001274 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001275 * fields. Note that msg->som and msg->sol will be initialized after completing
1276 * the first state, so that none of the msg pointers has to be initialized
1277 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001278 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001279void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001281 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001282 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaua560c212012-03-09 13:50:57 +01001283 struct buffer *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001284
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001285 state = msg->msg_state;
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001286 ptr = buf->p + msg->next;
1287 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001288
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001289 if (unlikely(ptr >= end))
1290 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001292 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001293 /*
1294 * First, states that are specific to the response only.
1295 * We check them first so that request and headers are
1296 * closer to each other (accessed more often).
1297 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001299 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001301 /* we have a start of message, but we have to check
1302 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001303 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001304 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001305 char *beg = buf->p;
1306
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 if (unlikely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001308 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001309 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001310 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001311 bi_fast_delete(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001313 msg->sol = msg->som = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001314 hdr_idx_init(idx);
1315 state = HTTP_MSG_RPVER;
1316 goto http_msg_rpver;
1317 }
1318
1319 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1320 goto http_msg_invalid;
1321
1322 if (unlikely(*ptr == '\n'))
1323 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1324 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1325 /* stop here */
1326
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001328 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001329 EXPECT_LF_HERE(ptr, http_msg_invalid);
1330 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1331 /* stop here */
1332
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001334 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 case HTTP_MSG_RPVER_SP:
1336 case HTTP_MSG_RPCODE:
1337 case HTTP_MSG_RPCODE_SP:
1338 case HTTP_MSG_RPREASON:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001339 ptr = (char *)http_parse_stsline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001340 state, ptr, end,
1341 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001342 if (unlikely(!ptr))
1343 return;
1344
1345 /* we have a full response and we know that we have either a CR
1346 * or an LF at <ptr>.
1347 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1349
Willy Tarreau3a215be2012-03-09 21:39:51 +01001350 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001351 if (likely(*ptr == '\r'))
1352 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1353 goto http_msg_rpline_end;
1354
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001356 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001357 /* msg->sol must point to the first of CR or LF. */
1358 EXPECT_LF_HERE(ptr, http_msg_invalid);
1359 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1360 /* stop here */
1361
1362 /*
1363 * Second, states that are specific to the request only
1364 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001366 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001367 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001368 /* we have a start of message, but we have to check
1369 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001370 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001371 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001372 char *beg = buf->p;
1373
Willy Tarreau15de77e2010-01-02 21:59:16 +01001374 if (likely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001375 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001377 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02001378 bi_fast_delete(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001379 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001380 msg->sol = msg->som = ptr - buf->p;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001381 /* we will need this when keep-alive will be supported
1382 hdr_idx_init(idx);
1383 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001384 state = HTTP_MSG_RQMETH;
1385 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001387
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1389 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001390
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001391 if (unlikely(*ptr == '\n'))
1392 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001394 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001395
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001397 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 EXPECT_LF_HERE(ptr, http_msg_invalid);
1399 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001401
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001403 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 case HTTP_MSG_RQMETH_SP:
1405 case HTTP_MSG_RQURI:
1406 case HTTP_MSG_RQURI_SP:
1407 case HTTP_MSG_RQVER:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001408 ptr = (char *)http_parse_reqline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001409 state, ptr, end,
1410 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 if (unlikely(!ptr))
1412 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001413
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 /* we have a full request and we know that we have either a CR
1415 * or an LF at <ptr>.
1416 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001417 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001418
Willy Tarreau3a215be2012-03-09 21:39:51 +01001419 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 if (likely(*ptr == '\r'))
1421 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001423
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001425 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 /* check for HTTP/0.9 request : no version information available.
1427 * msg->sol must point to the first of CR or LF.
1428 */
1429 if (unlikely(msg->sl.rq.v_l == 0))
1430 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001431
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001432 EXPECT_LF_HERE(ptr, http_msg_invalid);
1433 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001435
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 /*
1437 * Common states below
1438 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001440 http_msg_hdr_first:
Willy Tarreau3a215be2012-03-09 21:39:51 +01001441 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 if (likely(!HTTP_IS_CRLF(*ptr))) {
1443 goto http_msg_hdr_name;
1444 }
1445
1446 if (likely(*ptr == '\r'))
1447 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1448 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001449
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001451 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 /* assumes msg->sol points to the first char */
1453 if (likely(HTTP_IS_TOKEN(*ptr)))
1454 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001455
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001456 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001458
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001459 if (likely(msg->err_pos < -1) || *ptr == '\n')
1460 goto http_msg_invalid;
1461
1462 if (msg->err_pos == -1) /* capture error pointer */
1463 msg->err_pos = ptr - buf->data; /* >= 0 now */
1464
1465 /* and we still accept this non-token character */
1466 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001467
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001469 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001470 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001471 if (likely(HTTP_IS_SPHT(*ptr)))
1472 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001473
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001474 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001475 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001476
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001477 if (likely(!HTTP_IS_CRLF(*ptr))) {
1478 goto http_msg_hdr_val;
1479 }
1480
1481 if (likely(*ptr == '\r'))
1482 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1483 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001484
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001486 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 EXPECT_LF_HERE(ptr, http_msg_invalid);
1488 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001491 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 if (likely(HTTP_IS_SPHT(*ptr))) {
1493 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001494 for (; buf->p + msg->sov < ptr; msg->sov++)
1495 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001496 goto http_msg_hdr_l1_sp;
1497 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001498 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001499 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 goto http_msg_complete_header;
1501
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001503 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001504 /* assumes msg->sol points to the first char, and msg->sov
1505 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 */
1507 if (likely(!HTTP_IS_CRLF(*ptr)))
1508 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001509
Willy Tarreau12e48b32012-03-05 16:57:34 +01001510 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001511 /* Note: we could also copy eol into ->eoh so that we have the
1512 * real header end in case it ends with lots of LWS, but is this
1513 * really needed ?
1514 */
1515 if (likely(*ptr == '\r'))
1516 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1517 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001518
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001520 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 EXPECT_LF_HERE(ptr, http_msg_invalid);
1522 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001523
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001525 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1527 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001528 for (; buf->p + msg->eol < ptr; msg->eol++)
1529 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001530 goto http_msg_hdr_val;
1531 }
1532 http_msg_complete_header:
1533 /*
1534 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001535 * Assumes msg->sol points to the first char, msg->sov points
1536 * to the first character of the value and msg->eol to the
1537 * first CR or LF so we know how the line ends. We insert last
1538 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001540 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 idx, idx->tail) < 0))
1542 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001543
Willy Tarreau3a215be2012-03-09 21:39:51 +01001544 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001545 if (likely(!HTTP_IS_CRLF(*ptr))) {
1546 goto http_msg_hdr_name;
1547 }
1548
1549 if (likely(*ptr == '\r'))
1550 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1551 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001552
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001554 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 /* Assumes msg->sol points to the first of either CR or LF */
1556 EXPECT_LF_HERE(ptr, http_msg_invalid);
1557 ptr++;
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001558 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001559 msg->eoh = msg->sol;
1560 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001561 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001563
1564 case HTTP_MSG_ERROR:
1565 /* this may only happen if we call http_msg_analyser() twice with an error */
1566 break;
1567
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001568#ifdef DEBUG_FULL
1569 default:
1570 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1571 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001572#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001573 }
1574 http_msg_ood:
1575 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001576 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001577 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001579
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 http_msg_invalid:
1581 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001582 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001583 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584 return;
1585}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001586
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001587/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1588 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1589 * nothing is done and 1 is returned.
1590 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001591static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001592{
1593 int delta;
1594 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001595 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001596
1597 if (msg->sl.rq.v_l != 0)
1598 return 1;
1599
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001600 cur_end = msg->buf->p + msg->sol + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001601 delta = 0;
1602
1603 if (msg->sl.rq.u_l == 0) {
1604 /* if no URI was set, add "/" */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001605 delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001606 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001607 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608 }
1609 /* add HTTP version */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001610 delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001611 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612 cur_end += delta;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001613 cur_end = (char *)http_parse_reqline(msg, msg->buf->data,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 HTTP_MSG_RQMETH,
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001615 msg->buf->p + msg->sol, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001616 NULL, NULL);
1617 if (unlikely(!cur_end))
1618 return 0;
1619
1620 /* we have a full HTTP/1.0 request now and we know that
1621 * we have either a CR or an LF at <ptr>.
1622 */
1623 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1624 return 1;
1625}
1626
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001627/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001628 * and "keep-alive" values. If we already know that some headers may safely
1629 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001630 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1631 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1632 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1633 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1634 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001635 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001636 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001637void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001638{
Willy Tarreau5b154472009-12-21 20:11:07 +01001639 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001640 const char *hdr_val = "Connection";
1641 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001642
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001643 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001644 return;
1645
Willy Tarreau88d349d2010-01-25 12:15:43 +01001646 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1647 hdr_val = "Proxy-Connection";
1648 hdr_len = 16;
1649 }
1650
Willy Tarreau5b154472009-12-21 20:11:07 +01001651 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001652 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001653 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001654 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1655 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001656 if (to_del & 2)
1657 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001658 else
1659 txn->flags |= TX_CON_KAL_SET;
1660 }
1661 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1662 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001663 if (to_del & 1)
1664 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001665 else
1666 txn->flags |= TX_CON_CLO_SET;
1667 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001668 }
1669
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001670 txn->flags |= TX_HDR_CONN_PRS;
1671 return;
1672}
Willy Tarreau5b154472009-12-21 20:11:07 +01001673
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001674/* Apply desired changes on the Connection: header. Values may be removed and/or
1675 * added depending on the <wanted> flags, which are exclusively composed of
1676 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1677 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1678 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001679void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001680{
1681 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001682 const char *hdr_val = "Connection";
1683 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001684
1685 ctx.idx = 0;
1686
Willy Tarreau88d349d2010-01-25 12:15:43 +01001687
1688 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1689 hdr_val = "Proxy-Connection";
1690 hdr_len = 16;
1691 }
1692
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001693 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001694 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001695 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1696 if (wanted & TX_CON_KAL_SET)
1697 txn->flags |= TX_CON_KAL_SET;
1698 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001699 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001700 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001701 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1702 if (wanted & TX_CON_CLO_SET)
1703 txn->flags |= TX_CON_CLO_SET;
1704 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001705 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001706 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001707 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001708
1709 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1710 return;
1711
1712 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1713 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001714 hdr_val = "Connection: close";
1715 hdr_len = 17;
1716 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1717 hdr_val = "Proxy-Connection: close";
1718 hdr_len = 23;
1719 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001720 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001721 }
1722
1723 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1724 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001725 hdr_val = "Connection: keep-alive";
1726 hdr_len = 22;
1727 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1728 hdr_val = "Proxy-Connection: keep-alive";
1729 hdr_len = 28;
1730 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001731 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001732 }
1733 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001734}
1735
Willy Tarreaua458b672012-03-05 11:17:50 +01001736/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001737 * first byte of body, and increments msg->sov by the number of bytes parsed,
1738 * so that we know we can forward between ->som and ->sov. Note that due to
1739 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1740 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001741 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001742 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001743 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001744int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001745{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001746 const struct buffer *buf = msg->buf;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001747 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001748 const char *ptr_old = ptr;
1749 const char *end = buf->data + buf->size;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001750 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001751 unsigned int chunk = 0;
1752
1753 /* The chunk size is in the following form, though we are only
1754 * interested in the size and CRLF :
1755 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1756 */
1757 while (1) {
1758 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001759 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001760 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001761 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001762 if (c < 0) /* not a hex digit anymore */
1763 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764 if (++ptr >= end)
1765 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001766 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001767 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001768 chunk = (chunk << 4) + c;
1769 }
1770
Willy Tarreaud98cf932009-12-27 22:54:55 +01001771 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001772 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001773 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001774
1775 while (http_is_spht[(unsigned char)*ptr]) {
1776 if (++ptr >= end)
1777 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001778 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001779 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001780 }
1781
Willy Tarreaud98cf932009-12-27 22:54:55 +01001782 /* Up to there, we know that at least one byte is present at *ptr. Check
1783 * for the end of chunk size.
1784 */
1785 while (1) {
1786 if (likely(HTTP_IS_CRLF(*ptr))) {
1787 /* we now have a CR or an LF at ptr */
1788 if (likely(*ptr == '\r')) {
1789 if (++ptr >= end)
1790 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001791 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001792 return 0;
1793 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001794
Willy Tarreaud98cf932009-12-27 22:54:55 +01001795 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001796 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001797 if (++ptr >= end)
1798 ptr = buf->data;
1799 /* done */
1800 break;
1801 }
1802 else if (*ptr == ';') {
1803 /* chunk extension, ends at next CRLF */
1804 if (++ptr >= end)
1805 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001806 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001807 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001808
1809 while (!HTTP_IS_CRLF(*ptr)) {
1810 if (++ptr >= end)
1811 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001812 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001813 return 0;
1814 }
1815 /* we have a CRLF now, loop above */
1816 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001817 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001818 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001819 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 }
1821
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001823 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001824 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001825 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001826 msg->sov += ptr - ptr_old;
1827 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001828 msg->chunk_len = chunk;
1829 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001830 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001831 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001832 error:
1833 msg->err_pos = ptr - buf->data;
1834 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001835}
1836
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001837/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001838 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001839 * the trailers is found, it is automatically scheduled to be forwarded,
1840 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1841 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001842 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001843 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001844 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001845 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1846 * which implies that all non-trailers data have already been scheduled for
1847 * forwarding, and that the difference between msg->som and msg->sov exactly
1848 * matches the length of trailers already parsed and not forwarded. It is also
1849 * important to note that this function is designed to be able to parse wrapped
1850 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001851 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001852int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001853{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001854 const struct buffer *buf = msg->buf;
1855
Willy Tarreaua458b672012-03-05 11:17:50 +01001856 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001858 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001859 const char *ptr = b_ptr(buf, msg->next);
1860 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001861 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001862
1863 /* scan current line and stop at LF or CRLF */
1864 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001865 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001866 return 0;
1867
1868 if (*ptr == '\n') {
1869 if (!p1)
1870 p1 = ptr;
1871 p2 = ptr;
1872 break;
1873 }
1874
1875 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001876 if (p1) {
1877 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001878 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001879 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001880 p1 = ptr;
1881 }
1882
1883 ptr++;
1884 if (ptr >= buf->data + buf->size)
1885 ptr = buf->data;
1886 }
1887
1888 /* after LF; point to beginning of next line */
1889 p2++;
1890 if (p2 >= buf->data + buf->size)
1891 p2 = buf->data;
1892
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001893 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001894 if (bytes < 0)
1895 bytes += buf->size;
1896
1897 /* schedule this line for forwarding */
1898 msg->sov += bytes;
1899 if (msg->sov >= buf->size)
1900 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001901
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02001902 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001903 /* LF/CRLF at beginning of line => end of trailers at p2.
1904 * Everything was scheduled for forwarding, there's nothing
1905 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001906 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001907 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001908 msg->msg_state = HTTP_MSG_DONE;
1909 return 1;
1910 }
1911 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001912 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001913 }
1914}
1915
1916/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1917 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreaua458b672012-03-05 11:17:50 +01001918 * ->som, ->next in order to include this part into the next forwarding phase.
1919 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1921 * not enough data are available, the function does not change anything and
1922 * returns zero. If a parse error is encountered, the function returns < 0 and
1923 * does not change anything. Note: this function is designed to parse wrapped
1924 * CRLF at the end of the buffer.
1925 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001926int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001927{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001928 const struct buffer *buf = msg->buf;
1929 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001930 int bytes;
1931
1932 /* NB: we'll check data availabilty at the end. It's not a
1933 * problem because whatever we match first will be checked
1934 * against the correct length.
1935 */
1936 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001937 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001938 if (*ptr == '\r') {
1939 bytes++;
1940 ptr++;
1941 if (ptr >= buf->data + buf->size)
1942 ptr = buf->data;
1943 }
1944
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001945 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001946 return 0;
1947
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001948 if (*ptr != '\n') {
1949 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001951 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952
1953 ptr++;
1954 if (ptr >= buf->data + buf->size)
1955 ptr = buf->data;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001956 /* prepare the CRLF to be forwarded (between ->som and ->sov) */
1957 msg->som = 0;
1958 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001959 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1960 return 1;
1961}
Willy Tarreau5b154472009-12-21 20:11:07 +01001962
Willy Tarreau21710ff2012-03-09 13:58:04 +01001963/* This function realigns a possibly wrapping http message at the beginning
1964 * of its buffer. The function may only be used when the buffer's tail is
1965 * empty.
1966 */
1967void http_message_realign(struct http_msg *msg)
Willy Tarreau83e3af02009-12-28 17:39:57 +01001968{
Willy Tarreau21710ff2012-03-09 13:58:04 +01001969 struct buffer *buf = msg->buf;
Willy Tarreau89fa7062012-03-02 16:13:16 +01001970 int off = buf->data + buf->size - buf->p;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001971
1972 /* two possible cases :
1973 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001974 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001975 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001976 if (buf->i) {
1977 int block1 = buf->i;
Willy Tarreau8096de92010-02-26 11:12:27 +01001978 int block2 = 0;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001979 if (buf->p + buf->i > buf->data + buf->size) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001980 /* non-contiguous block */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001981 block1 = buf->data + buf->size - buf->p;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001982 block2 = buf->p + buf->i - (buf->data + buf->size);
Willy Tarreau8096de92010-02-26 11:12:27 +01001983 }
1984 if (block2)
1985 memcpy(swap_buffer, buf->data, block2);
Willy Tarreau89fa7062012-03-02 16:13:16 +01001986 memmove(buf->data, buf->p, block1);
Willy Tarreau8096de92010-02-26 11:12:27 +01001987 if (block2)
1988 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001989 }
1990
1991 /* adjust all known pointers */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001992 buf->p = buf->data;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001993
Willy Tarreau83e3af02009-12-28 17:39:57 +01001994 if (msg->err_pos >= 0) {
1995 msg->err_pos += off;
1996 if (msg->err_pos >= buf->size)
1997 msg->err_pos -= buf->size;
1998 }
1999
2000 buf->flags &= ~BF_FULL;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002001 if (bi_full(buf))
Willy Tarreau83e3af02009-12-28 17:39:57 +01002002 buf->flags |= BF_FULL;
2003}
2004
Willy Tarreaud787e662009-07-07 10:14:51 +02002005/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2006 * processing can continue on next analysers, or zero if it either needs more
2007 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2008 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2009 * when it has nothing left to do, and may remove any analyser when it wants to
2010 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002011 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002012int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002013{
Willy Tarreau59234e92008-11-30 23:51:27 +01002014 /*
2015 * We will parse the partial (or complete) lines.
2016 * We will check the request syntax, and also join multi-line
2017 * headers. An index of all the lines will be elaborated while
2018 * parsing.
2019 *
2020 * For the parsing, we use a 28 states FSM.
2021 *
2022 * Here is the information we currently have :
Willy Tarreauea1175a2012-03-05 15:52:30 +01002023 * req->p + msg->som = beginning of request
2024 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau83e3af02009-12-28 17:39:57 +01002025 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01002026 * msg->next = first non-visited byte
Willy Tarreau59234e92008-11-30 23:51:27 +01002027 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002028 *
2029 * At end of parsing, we may perform a capture of the error (if any), and
2030 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2031 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2032 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002033 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002034
Willy Tarreau59234e92008-11-30 23:51:27 +01002035 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002036 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002037 struct http_txn *txn = &s->txn;
2038 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002039 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002040
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002041 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau6bf17362009-02-24 10:48:35 +01002042 now_ms, __FUNCTION__,
2043 s,
2044 req,
2045 req->rex, req->wex,
2046 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002047 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002048 req->analysers);
2049
Willy Tarreau52a0c602009-08-16 22:45:38 +02002050 /* we're speaking HTTP here, so let's speak HTTP to the client */
2051 s->srv_error = http_return_srv_error;
2052
Willy Tarreau83e3af02009-12-28 17:39:57 +01002053 /* There's a protected area at the end of the buffer for rewriting
2054 * purposes. We don't want to start to parse the request if the
2055 * protected area is affected, because we may have to move processed
2056 * data later, which is much more complicated.
2057 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002058 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002059 if ((txn->flags & TX_NOT_FIRST) &&
2060 unlikely((req->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002061 bi_end(req) < b_ptr(req, msg->next) ||
2062 bi_end(req) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002063 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002064 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2065 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002066 /* some data has still not left the buffer, wake us once that's done */
2067 buffer_dont_connect(req);
2068 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2069 return 0;
2070 }
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002071 if (bi_end(req) < b_ptr(req, msg->next) ||
2072 bi_end(req) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau21710ff2012-03-09 13:58:04 +01002073 http_message_realign(msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002074 }
2075
Willy Tarreau065e8332010-01-08 00:30:20 +01002076 /* Note that we have the same problem with the response ; we
2077 * may want to send a redirect, error or anything which requires
2078 * some spare space. So we'll ensure that we have at least
2079 * maxrewrite bytes available in the response buffer before
2080 * processing that one. This will only affect pipelined
2081 * keep-alive requests.
2082 */
2083 if ((txn->flags & TX_NOT_FIRST) &&
2084 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02002085 bi_end(s->rep) < b_ptr(s->rep, txn->rsp.next) ||
2086 bi_end(s->rep) > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002087 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002088 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2089 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002090 /* don't let a connection request be initiated */
2091 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002092 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002093 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002094 return 0;
2095 }
2096 }
2097
Willy Tarreaua458b672012-03-05 11:17:50 +01002098 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002099 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002100 }
2101
Willy Tarreau59234e92008-11-30 23:51:27 +01002102 /* 1: we might have to print this header in debug mode */
2103 if (unlikely((global.mode & MODE_DEBUG) &&
2104 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002105 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002106 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002107 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002108
Willy Tarreauea1175a2012-03-05 15:52:30 +01002109 sol = req->p + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002110 eol = sol + msg->sl.rq.l;
2111 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002112
Willy Tarreau59234e92008-11-30 23:51:27 +01002113 sol += hdr_idx_first_pos(&txn->hdr_idx);
2114 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002115
Willy Tarreau59234e92008-11-30 23:51:27 +01002116 while (cur_idx) {
2117 eol = sol + txn->hdr_idx.v[cur_idx].len;
2118 debug_hdr("clihdr", s, sol, eol);
2119 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2120 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002121 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002122 }
2123
Willy Tarreau58f10d72006-12-04 02:26:12 +01002124
Willy Tarreau59234e92008-11-30 23:51:27 +01002125 /*
2126 * Now we quickly check if we have found a full valid request.
2127 * If not so, we check the FD and buffer states before leaving.
2128 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002129 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002130 * requests are checked first. When waiting for a second request
2131 * on a keep-alive session, if we encounter and error, close, t/o,
2132 * we note the error in the session flags but don't set any state.
2133 * Since the error will be noted there, it will not be counted by
2134 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002135 * Last, we may increase some tracked counters' http request errors on
2136 * the cases that are deliberately the client's fault. For instance,
2137 * a timeout or connection reset is not counted as an error. However
2138 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002139 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002140
Willy Tarreau655dce92009-11-08 13:10:58 +01002141 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002142 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002143 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002144 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002145 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002146 session_inc_http_req_ctr(s);
2147 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002148 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002149 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002150 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002151
Willy Tarreau59234e92008-11-30 23:51:27 +01002152 /* 1: Since we are in header mode, if there's no space
2153 * left for headers, we won't be able to free more
2154 * later, so the session will never terminate. We
2155 * must terminate it now.
2156 */
2157 if (unlikely(req->flags & BF_FULL)) {
2158 /* FIXME: check if URI is set and return Status
2159 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002160 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002161 session_inc_http_req_ctr(s);
2162 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002163 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002164 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002165 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 goto return_bad_req;
2167 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002168
Willy Tarreau59234e92008-11-30 23:51:27 +01002169 /* 2: have we encountered a read error ? */
2170 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002171 if (!(s->flags & SN_ERR_MASK))
2172 s->flags |= SN_ERR_CLICL;
2173
Willy Tarreaufcffa692010-01-10 14:21:19 +01002174 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002175 goto failed_keep_alive;
2176
Willy Tarreau59234e92008-11-30 23:51:27 +01002177 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002178 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002179 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002180 session_inc_http_err_ctr(s);
2181 }
2182
Willy Tarreau59234e92008-11-30 23:51:27 +01002183 msg->msg_state = HTTP_MSG_ERROR;
2184 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002185
Willy Tarreauda7ff642010-06-23 11:44:09 +02002186 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002187 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002188 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002189 if (s->listener->counters)
2190 s->listener->counters->failed_req++;
2191
Willy Tarreau59234e92008-11-30 23:51:27 +01002192 if (!(s->flags & SN_FINST_MASK))
2193 s->flags |= SN_FINST_R;
2194 return 0;
2195 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002196
Willy Tarreau59234e92008-11-30 23:51:27 +01002197 /* 3: has the read timeout expired ? */
2198 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002199 if (!(s->flags & SN_ERR_MASK))
2200 s->flags |= SN_ERR_CLITO;
2201
Willy Tarreaufcffa692010-01-10 14:21:19 +01002202 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002203 goto failed_keep_alive;
2204
Willy Tarreau59234e92008-11-30 23:51:27 +01002205 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002206 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002207 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002208 session_inc_http_err_ctr(s);
2209 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002210 txn->status = 408;
2211 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2212 msg->msg_state = HTTP_MSG_ERROR;
2213 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002214
Willy Tarreauda7ff642010-06-23 11:44:09 +02002215 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002216 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002217 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002218 if (s->listener->counters)
2219 s->listener->counters->failed_req++;
2220
Willy Tarreau59234e92008-11-30 23:51:27 +01002221 if (!(s->flags & SN_FINST_MASK))
2222 s->flags |= SN_FINST_R;
2223 return 0;
2224 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002225
Willy Tarreau59234e92008-11-30 23:51:27 +01002226 /* 4: have we encountered a close ? */
2227 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002228 if (!(s->flags & SN_ERR_MASK))
2229 s->flags |= SN_ERR_CLICL;
2230
Willy Tarreaufcffa692010-01-10 14:21:19 +01002231 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002232 goto failed_keep_alive;
2233
Willy Tarreau4076a152009-04-02 15:18:36 +02002234 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002235 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002236 txn->status = 400;
2237 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2238 msg->msg_state = HTTP_MSG_ERROR;
2239 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002240
Willy Tarreauda7ff642010-06-23 11:44:09 +02002241 session_inc_http_err_ctr(s);
2242 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002243 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002244 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002245 if (s->listener->counters)
2246 s->listener->counters->failed_req++;
2247
Willy Tarreau59234e92008-11-30 23:51:27 +01002248 if (!(s->flags & SN_FINST_MASK))
2249 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002250 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002251 }
2252
Willy Tarreau520d95e2009-09-19 21:04:57 +02002253 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002254 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002255 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002256#ifdef TCP_QUICKACK
2257 if (s->listener->options & LI_O_NOQUICKACK) {
2258 /* We need more data, we have to re-enable quick-ack in case we
2259 * previously disabled it, otherwise we might cause the client
2260 * to delay next data.
2261 */
2262 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2263 }
2264#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002265
Willy Tarreaufcffa692010-01-10 14:21:19 +01002266 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2267 /* If the client starts to talk, let's fall back to
2268 * request timeout processing.
2269 */
2270 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002271 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002272 }
2273
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002275 if (!tick_isset(req->analyse_exp)) {
2276 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2277 (txn->flags & TX_WAIT_NEXT_RQ) &&
2278 tick_isset(s->be->timeout.httpka))
2279 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2280 else
2281 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2282 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002283
Willy Tarreau59234e92008-11-30 23:51:27 +01002284 /* we're not ready yet */
2285 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002286
2287 failed_keep_alive:
2288 /* Here we process low-level errors for keep-alive requests. In
2289 * short, if the request is not the first one and it experiences
2290 * a timeout, read error or shutdown, we just silently close so
2291 * that the client can try again.
2292 */
2293 txn->status = 0;
2294 msg->msg_state = HTTP_MSG_RQBEFORE;
2295 req->analysers = 0;
2296 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002297 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002298 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002299 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002300 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002301
Willy Tarreaud787e662009-07-07 10:14:51 +02002302 /* OK now we have a complete HTTP request with indexed headers. Let's
2303 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002304 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2305 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002306 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002307 * byte after the last LF. msg->sov points to the first byte of data.
2308 * msg->eol cannot be trusted because it may have been left uninitialized
2309 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002310 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002311
Willy Tarreauda7ff642010-06-23 11:44:09 +02002312 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002313 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2314
Willy Tarreaub16a5742010-01-10 14:46:16 +01002315 if (txn->flags & TX_WAIT_NEXT_RQ) {
2316 /* kill the pending keep-alive timeout */
2317 txn->flags &= ~TX_WAIT_NEXT_RQ;
2318 req->analyse_exp = TICK_ETERNITY;
2319 }
2320
2321
Willy Tarreaud787e662009-07-07 10:14:51 +02002322 /* Maybe we found in invalid header name while we were configured not
2323 * to block on that, so we have to capture it now.
2324 */
2325 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002326 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002327
Willy Tarreau59234e92008-11-30 23:51:27 +01002328 /*
2329 * 1: identify the method
2330 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002331 txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002332
2333 /* we can make use of server redirect on GET and HEAD */
2334 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2335 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002336
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 /*
2338 * 2: check if the URI matches the monitor_uri.
2339 * We have to do this for every request which gets in, because
2340 * the monitor-uri is defined by the frontend.
2341 */
2342 if (unlikely((s->fe->monitor_uri_len != 0) &&
2343 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002344 !memcmp(req->p + msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 s->fe->monitor_uri,
2346 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002347 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002349 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002350 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002351
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002353 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002354
Willy Tarreau59234e92008-11-30 23:51:27 +01002355 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002356 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2357 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 ret = acl_pass(ret);
2360 if (cond->pol == ACL_COND_UNLESS)
2361 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002362
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 if (ret) {
2364 /* we fail this request, let's return 503 service unavail */
2365 txn->status = 503;
2366 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2367 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002368 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002369 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002370
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 /* nothing to fail, let's reply normaly */
2372 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002373 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002374 goto return_prx_cond;
2375 }
2376
2377 /*
2378 * 3: Maybe we have to copy the original REQURI for the logs ?
2379 * Note: we cannot log anymore if the request has been
2380 * classified as invalid.
2381 */
2382 if (unlikely(s->logs.logwait & LW_REQ)) {
2383 /* we have a complete HTTP request that we must log */
2384 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2385 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002386
Willy Tarreau59234e92008-11-30 23:51:27 +01002387 if (urilen >= REQURI_LEN)
2388 urilen = REQURI_LEN - 1;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002389 memcpy(txn->uri, &req->p[msg->som], urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002391
Willy Tarreau59234e92008-11-30 23:51:27 +01002392 if (!(s->logs.logwait &= ~LW_REQ))
2393 s->do_log(s);
2394 } else {
2395 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002396 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002397 }
Willy Tarreau06619262006-12-17 08:37:22 +01002398
William Lallemanda73203e2012-03-12 12:48:57 +01002399 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2400 s->unique_id = pool_alloc2(pool2_uniqueid);
2401 }
2402
Willy Tarreau59234e92008-11-30 23:51:27 +01002403 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002404 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002405 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002406
Willy Tarreau5b154472009-12-21 20:11:07 +01002407 /* ... and check if the request is HTTP/1.1 or above */
2408 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002409 ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') ||
2410 ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') &&
2411 (req->p[msg->sol + msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002412 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002413
2414 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002415 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002416
Willy Tarreau88d349d2010-01-25 12:15:43 +01002417 /* if the frontend has "option http-use-proxy-header", we'll check if
2418 * we have what looks like a proxied connection instead of a connection,
2419 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2420 * Note that this is *not* RFC-compliant, however browsers and proxies
2421 * happen to do that despite being non-standard :-(
2422 * We consider that a request not beginning with either '/' or '*' is
2423 * a proxied connection, which covers both "scheme://location" and
2424 * CONNECT ip:port.
2425 */
2426 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002427 req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002428 txn->flags |= TX_USE_PX_CONN;
2429
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002430 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002431 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002432
Willy Tarreau59234e92008-11-30 23:51:27 +01002433 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002434 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01002435 capture_headers(req->p + msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002436 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002437
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002438 /* 6: determine the transfer-length.
2439 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2440 * the presence of a message-body in a REQUEST and its transfer length
2441 * must be determined that way (in order of precedence) :
2442 * 1. The presence of a message-body in a request is signaled by the
2443 * inclusion of a Content-Length or Transfer-Encoding header field
2444 * in the request's header fields. When a request message contains
2445 * both a message-body of non-zero length and a method that does
2446 * not define any semantics for that request message-body, then an
2447 * origin server SHOULD either ignore the message-body or respond
2448 * with an appropriate error message (e.g., 413). A proxy or
2449 * gateway, when presented the same request, SHOULD either forward
2450 * the request inbound with the message- body or ignore the
2451 * message-body when determining a response.
2452 *
2453 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2454 * and the "chunked" transfer-coding (Section 6.2) is used, the
2455 * transfer-length is defined by the use of this transfer-coding.
2456 * If a Transfer-Encoding header field is present and the "chunked"
2457 * transfer-coding is not present, the transfer-length is defined
2458 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002459 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002460 * 3. If a Content-Length header field is present, its decimal value in
2461 * OCTETs represents both the entity-length and the transfer-length.
2462 * If a message is received with both a Transfer-Encoding header
2463 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002464 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002465 * 4. By the server closing the connection. (Closing the connection
2466 * cannot be used to indicate the end of a request body, since that
2467 * would leave no possibility for the server to send back a response.)
2468 *
2469 * Whenever a transfer-coding is applied to a message-body, the set of
2470 * transfer-codings MUST include "chunked", unless the message indicates
2471 * it is terminated by closing the connection. When the "chunked"
2472 * transfer-coding is used, it MUST be the last transfer-coding applied
2473 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002474 */
2475
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002476 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002477 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002478 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002479 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002480 http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002481 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002482 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2483 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002484 /* bad transfer-encoding (chunked followed by something else) */
2485 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002486 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002487 break;
2488 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002489 }
2490
Willy Tarreau32b47f42009-10-18 20:55:02 +02002491 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002492 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002493 http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002494 signed long long cl;
2495
Willy Tarreauad14f752011-09-02 20:33:27 +02002496 if (!ctx.vlen) {
2497 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002498 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002499 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002500
Willy Tarreauad14f752011-09-02 20:33:27 +02002501 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2502 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002503 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002504 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002505
Willy Tarreauad14f752011-09-02 20:33:27 +02002506 if (cl < 0) {
2507 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002508 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002509 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002510
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002511 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02002512 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002513 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002514 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002515
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002516 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002517 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002518 }
2519
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002520 /* bodyless requests have a known length */
2521 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002522 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002523
Willy Tarreaud787e662009-07-07 10:14:51 +02002524 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002525 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002526 req->analyse_exp = TICK_ETERNITY;
2527 return 1;
2528
2529 return_bad_req:
2530 /* We centralize bad requests processing here */
2531 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2532 /* we detected a parsing error. We want to archive this request
2533 * in the dedicated proxy area for later troubleshooting.
2534 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002535 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002536 }
2537
2538 txn->req.msg_state = HTTP_MSG_ERROR;
2539 txn->status = 400;
2540 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002541
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002542 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002543 if (s->listener->counters)
2544 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002545
2546 return_prx_cond:
2547 if (!(s->flags & SN_ERR_MASK))
2548 s->flags |= SN_ERR_PRXCOND;
2549 if (!(s->flags & SN_FINST_MASK))
2550 s->flags |= SN_FINST_R;
2551
2552 req->analysers = 0;
2553 req->analyse_exp = TICK_ETERNITY;
2554 return 0;
2555}
2556
Cyril Bonté70be45d2010-10-12 00:14:35 +02002557/* We reached the stats page through a POST request.
2558 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002559 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002560 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002561int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002562{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002563 struct proxy *px = NULL;
2564 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002565
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002566 char key[LINESIZE];
2567 int action = ST_ADM_ACTION_NONE;
2568 int reprocess = 0;
2569
2570 int total_servers = 0;
2571 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002572
2573 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002574 char *st_cur_param = NULL;
2575 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002576
Willy Tarreauea1175a2012-03-05 15:52:30 +01002577 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002578 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002579
2580 cur_param = next_param = end_params;
2581
Cyril Bonté23b39d92011-02-10 22:54:44 +01002582 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002583 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002584 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002585 return 1;
2586 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002587 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002588 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002589 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002590 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002591 }
2592
2593 *end_params = '\0';
2594
Willy Tarreau295a8372011-03-10 11:25:07 +01002595 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002596
2597 /*
2598 * Parse the parameters in reverse order to only store the last value.
2599 * From the html form, the backend and the action are at the end.
2600 */
2601 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002602 char *value;
2603 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002604
2605 cur_param--;
2606 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002607 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002608 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002609 poffset = (cur_param != first_param ? 1 : 0);
2610 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2611 if ((plen > 0) && (plen <= sizeof(key))) {
2612 strncpy(key, cur_param + poffset, plen);
2613 key[plen - 1] = '\0';
2614 } else {
2615 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2616 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002617 }
2618
2619 /* Parse the value */
2620 value = key;
2621 while (*value != '\0' && *value != '=') {
2622 value++;
2623 }
2624 if (*value == '=') {
2625 /* Ok, a value is found, we can mark the end of the key */
2626 *value++ = '\0';
2627 }
2628
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002629 if (!url_decode(key) || !url_decode(value))
2630 break;
2631
Cyril Bonté70be45d2010-10-12 00:14:35 +02002632 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002633 if (!px && (strcmp(key, "b") == 0)) {
2634 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2635 /* the backend name is unknown or ambiguous (duplicate names) */
2636 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2637 goto out;
2638 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002639 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002640 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002641 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002642 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002643 }
2644 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002645 action = ST_ADM_ACTION_ENABLE;
2646 }
2647 else {
2648 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2649 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002650 }
2651 }
2652 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002653 if (!(px && action)) {
2654 /*
2655 * Indicates that we'll need to reprocess the parameters
2656 * as soon as backend and action are known
2657 */
2658 if (!reprocess) {
2659 st_cur_param = cur_param;
2660 st_next_param = next_param;
2661 }
2662 reprocess = 1;
2663 }
2664 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002665 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002666 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002667 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002668 /* Not already in maintenance, we can change the server state */
2669 sv->state |= SRV_MAINTAIN;
2670 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002671 altered_servers++;
2672 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002673 }
2674 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002675 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002676 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002677 /* Already in maintenance, we can change the server state */
2678 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002679 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002680 altered_servers++;
2681 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002682 }
2683 break;
2684 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002685 } else {
2686 /* the server name is unknown or ambiguous (duplicate names) */
2687 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002688 }
2689 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002690 if (reprocess && px && action) {
2691 /* Now, we know the backend and the action chosen by the user.
2692 * We can safely restart from the first server parameter
2693 * to reprocess them
2694 */
2695 cur_param = st_cur_param;
2696 next_param = st_next_param;
2697 reprocess = 0;
2698 goto reprocess_servers;
2699 }
2700
Cyril Bonté70be45d2010-10-12 00:14:35 +02002701 next_param = cur_param;
2702 }
2703 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002704
2705 if (total_servers == 0) {
2706 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2707 }
2708 else if (altered_servers == 0) {
2709 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2710 }
2711 else if (altered_servers == total_servers) {
2712 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2713 }
2714 else {
2715 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2716 }
2717 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002718 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002719}
2720
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002721/* returns a pointer to the first rule which forbids access (deny or http_auth),
2722 * or NULL if everything's OK.
2723 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002724static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002725http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2726{
Willy Tarreauff011f22011-01-06 17:51:27 +01002727 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002728
Willy Tarreauff011f22011-01-06 17:51:27 +01002729 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002730 int ret = 1;
2731
Willy Tarreauff011f22011-01-06 17:51:27 +01002732 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002733 continue;
2734
2735 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002736 if (rule->cond) {
2737 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002738 ret = acl_pass(ret);
2739
Willy Tarreauff011f22011-01-06 17:51:27 +01002740 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002741 ret = !ret;
2742 }
2743
2744 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002745 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002746 return NULL; /* no problem */
2747 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002748 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002749 }
2750 }
2751 return NULL;
2752}
2753
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754/* This stream analyser runs all HTTP request processing which is common to
2755 * frontends and backends, which means blocking ACLs, filters, connection-close,
2756 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002757 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002758 * either needs more data or wants to immediately abort the request (eg: deny,
2759 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002760 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002761int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002762{
Willy Tarreaud787e662009-07-07 10:14:51 +02002763 struct http_txn *txn = &s->txn;
2764 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002766 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002767 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002768 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002769 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002770
Willy Tarreau655dce92009-11-08 13:10:58 +01002771 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002772 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002773 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002774 return 0;
2775 }
2776
Willy Tarreau3a816292009-07-07 10:55:49 +02002777 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002778 req->analyse_exp = TICK_ETERNITY;
2779
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002780 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaud787e662009-07-07 10:14:51 +02002781 now_ms, __FUNCTION__,
2782 s,
2783 req,
2784 req->rex, req->wex,
2785 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002786 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002787 req->analysers);
2788
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002789 /* first check whether we have some ACLs set to block this request */
2790 list_for_each_entry(cond, &px->block_cond, list) {
2791 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002792
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002793 ret = acl_pass(ret);
2794 if (cond->pol == ACL_COND_UNLESS)
2795 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002796
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002797 if (ret) {
2798 txn->status = 403;
2799 /* let's log the request time */
2800 s->logs.tv_request = now;
2801 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002802 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002803 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002804 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002805 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002806
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002807 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002808 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002809
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002810 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002811 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002812 do_stats = stats_check_uri(s->rep->prod, txn, px);
2813 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002814 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 +01002815 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002816 else
2817 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002818
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002819 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002820 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002821 txn->status = 403;
2822 s->logs.tv_request = now;
2823 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002824 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002825 s->fe->fe_counters.denied_req++;
2826 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2827 s->be->be_counters.denied_req++;
2828 if (s->listener->counters)
2829 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002830 goto return_prx_cond;
2831 }
2832
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002833 /* try headers filters */
2834 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002835 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002836 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002837
Willy Tarreau59234e92008-11-30 23:51:27 +01002838 /* has the request been denied ? */
2839 if (txn->flags & TX_CLDENY) {
2840 /* no need to go further */
2841 txn->status = 403;
2842 /* let's log the request time */
2843 s->logs.tv_request = now;
2844 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002845 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002846 goto return_prx_cond;
2847 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002848
2849 /* When a connection is tarpitted, we use the tarpit timeout,
2850 * which may be the same as the connect timeout if unspecified.
2851 * If unset, then set it to zero because we really want it to
2852 * eventually expire. We build the tarpit as an analyser.
2853 */
2854 if (txn->flags & TX_CLTARPIT) {
2855 buffer_erase(s->req);
2856 /* wipe the request out so that we can drop the connection early
2857 * if the client closes first.
2858 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002859 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002860 req->analysers = 0; /* remove switching rules etc... */
2861 req->analysers |= AN_REQ_HTTP_TARPIT;
2862 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2863 if (!req->analyse_exp)
2864 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002865 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002866 return 1;
2867 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002868 }
Willy Tarreau06619262006-12-17 08:37:22 +01002869
Willy Tarreau5b154472009-12-21 20:11:07 +01002870 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2871 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002872 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2873 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002874 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2875 * avoid to redo the same work if FE and BE have the same settings (common).
2876 * The method consists in checking if options changed between the two calls
2877 * (implying that either one is non-null, or one of them is non-null and we
2878 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002879 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002880
Willy Tarreaudc008c52010-02-01 16:20:08 +01002881 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2882 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2883 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2884 (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 +01002885 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002886
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002887 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2888 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002889 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002890 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2891 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002892 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002893 tmp = TX_CON_WANT_CLO;
2894
Willy Tarreau5b154472009-12-21 20:11:07 +01002895 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2896 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002897
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002898 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2899 /* parse the Connection header and possibly clean it */
2900 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002901 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002902 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2903 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002904 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002905 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002906 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002907 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002908 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002909
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002910 /* check if client or config asks for explicit close in KAL/SCL */
2911 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2912 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2913 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002914 (!(msg->flags & HTTP_MSGF_VER_11) && !(txn->flags & TX_HDR_CONN_KAL)) || /* no "connection: k-a" in 1.0 */
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002915 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002916 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002917 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002918 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2919 }
Willy Tarreau78599912009-10-17 20:12:21 +02002920
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002921 /* we can be blocked here because the request needs to be authenticated,
2922 * either to pass or to access stats.
2923 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002924 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002925 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002926 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002927
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002928 if (!realm)
2929 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2930
Willy Tarreau844a7e72010-01-31 21:46:18 +01002931 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002932 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2933 txn->status = 401;
2934 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002935 /* on 401 we still count one error, because normal browsing
2936 * won't significantly increase the counter but brute force
2937 * attempts will.
2938 */
2939 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002940 goto return_prx_cond;
2941 }
2942
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002943 /* add request headers from the rule sets in the same order */
2944 list_for_each_entry(wl, &px->req_add, list) {
2945 if (wl->cond) {
2946 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2947 ret = acl_pass(ret);
2948 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2949 ret = !ret;
2950 if (!ret)
2951 continue;
2952 }
2953
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002954 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002955 goto return_bad_req;
2956 }
2957
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002958 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002959 struct stats_admin_rule *stats_admin_rule;
2960
2961 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002962 * FIXME!!! that one is rather dangerous, we want to
2963 * make it follow standard rules (eg: clear req->analysers).
2964 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002965
Cyril Bonté474be412010-10-12 00:14:36 +02002966 /* now check whether we have some admin rules for this request */
2967 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2968 int ret = 1;
2969
2970 if (stats_admin_rule->cond) {
2971 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2972 ret = acl_pass(ret);
2973 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2974 ret = !ret;
2975 }
2976
2977 if (ret) {
2978 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002979 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002980 break;
2981 }
2982 }
2983
Cyril Bonté70be45d2010-10-12 00:14:35 +02002984 /* Was the status page requested with a POST ? */
2985 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002986 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002987 if (msg->msg_state < HTTP_MSG_100_SENT) {
2988 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2989 * send an HTTP/1.1 100 Continue intermediate response.
2990 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002991 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002992 struct hdr_ctx ctx;
2993 ctx.idx = 0;
2994 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002995 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002996 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02002997 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01002998 }
2999 }
3000 msg->msg_state = HTTP_MSG_100_SENT;
3001 s->logs.tv_request = now; /* update the request timer to reflect full request */
3002 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003003 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003004 /* we need more data */
3005 req->analysers |= an_bit;
3006 buffer_dont_connect(req);
3007 return 0;
3008 }
Cyril Bonté474be412010-10-12 00:14:36 +02003009 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003010 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003011 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003012 }
3013
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003014 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003015 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003016 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003017 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003018 s->rep->prod->applet.private = s;
3019 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003020 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003021 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3022 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003023
3024 return 0;
3025
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003026 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003027
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003028 /* check whether we have some ACLs set to redirect this request */
3029 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003030 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003031
Willy Tarreauf285f542010-01-03 20:03:03 +01003032 if (rule->cond) {
3033 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3034 ret = acl_pass(ret);
3035 if (rule->cond->pol == ACL_COND_UNLESS)
3036 ret = !ret;
3037 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003038
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003039 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003040 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003041 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003042
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003043 /* build redirect message */
3044 switch(rule->code) {
3045 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003046 msg_fmt = HTTP_303;
3047 break;
3048 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 msg_fmt = HTTP_301;
3050 break;
3051 case 302:
3052 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003053 msg_fmt = HTTP_302;
3054 break;
3055 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003056
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003057 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003058 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003059
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003060 switch(rule->type) {
3061 case REDIRECT_TYPE_PREFIX: {
3062 const char *path;
3063 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003064
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003065 path = http_get_path(txn);
3066 /* build message using path */
3067 if (path) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003068 pathlen = txn->req.sl.rq.u_l + (req->p + txn->req.sol + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003069 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3070 int qs = 0;
3071 while (qs < pathlen) {
3072 if (path[qs] == '?') {
3073 pathlen = qs;
3074 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003075 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003076 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003077 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003078 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003079 } else {
3080 path = "/";
3081 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003082 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003083
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003084 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003085 goto return_bad_req;
3086
3087 /* add prefix. Note that if prefix == "/", we don't want to
3088 * add anything, otherwise it makes it hard for the user to
3089 * configure a self-redirection.
3090 */
3091 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003092 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3093 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003094 }
3095
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003096 /* add path */
3097 memcpy(rdr.str + rdr.len, path, pathlen);
3098 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003099
3100 /* append a slash at the end of the location is needed and missing */
3101 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3102 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3103 if (rdr.len > rdr.size - 5)
3104 goto return_bad_req;
3105 rdr.str[rdr.len] = '/';
3106 rdr.len++;
3107 }
3108
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003109 break;
3110 }
3111 case REDIRECT_TYPE_LOCATION:
3112 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003113 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003114 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003115
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003116 /* add location */
3117 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3118 rdr.len += rule->rdr_len;
3119 break;
3120 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003121
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003122 if (rule->cookie_len) {
3123 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3124 rdr.len += 14;
3125 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3126 rdr.len += rule->cookie_len;
3127 memcpy(rdr.str + rdr.len, "\r\n", 2);
3128 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003129 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003130
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003131 /* add end of headers and the keep-alive/close status.
3132 * We may choose to set keep-alive if the Location begins
3133 * with a slash, because the client will come back to the
3134 * same server.
3135 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003136 txn->status = rule->code;
3137 /* let's log the request time */
3138 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003139
3140 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003141 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3142 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003143 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3144 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3145 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003146 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003147 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3148 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3149 rdr.len += 30;
3150 } else {
3151 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3152 rdr.len += 24;
3153 }
Willy Tarreau75661452010-01-10 10:35:01 +01003154 }
3155 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3156 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003157 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003158 /* "eat" the request */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003159 bi_fast_delete(req, msg->sov - msg->som);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003160 msg->som = msg->sov;
3161 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003162 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3163 txn->req.msg_state = HTTP_MSG_CLOSED;
3164 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003165 break;
3166 } else {
3167 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003168 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3169 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3170 rdr.len += 29;
3171 } else {
3172 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3173 rdr.len += 23;
3174 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003175 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003176 goto return_prx_cond;
3177 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003178 }
3179 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003180
Willy Tarreau2be39392010-01-03 17:24:51 +01003181 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3182 * If this happens, then the data will not come immediately, so we must
3183 * send all what we have without waiting. Note that due to the small gain
3184 * in waiting for the body of the request, it's easier to simply put the
3185 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3186 * itself once used.
3187 */
3188 req->flags |= BF_SEND_DONTWAIT;
3189
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003190 /* that's OK for us now, let's move on to next analysers */
3191 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003192
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003193 return_bad_req:
3194 /* We centralize bad requests processing here */
3195 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3196 /* we detected a parsing error. We want to archive this request
3197 * in the dedicated proxy area for later troubleshooting.
3198 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003199 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003200 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003201
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003202 txn->req.msg_state = HTTP_MSG_ERROR;
3203 txn->status = 400;
3204 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003205
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003206 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003207 if (s->listener->counters)
3208 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003209
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003210 return_prx_cond:
3211 if (!(s->flags & SN_ERR_MASK))
3212 s->flags |= SN_ERR_PRXCOND;
3213 if (!(s->flags & SN_FINST_MASK))
3214 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003215
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003216 req->analysers = 0;
3217 req->analyse_exp = TICK_ETERNITY;
3218 return 0;
3219}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003220
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003221/* This function performs all the processing enabled for the current request.
3222 * It returns 1 if the processing can continue on next analysers, or zero if it
3223 * needs more data, encounters an error, or wants to immediately abort the
3224 * request. It relies on buffers flags, and updates s->req->analysers.
3225 */
3226int http_process_request(struct session *s, struct buffer *req, int an_bit)
3227{
3228 struct http_txn *txn = &s->txn;
3229 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003230
Willy Tarreau655dce92009-11-08 13:10:58 +01003231 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003232 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003233 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003234 return 0;
3235 }
3236
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003237 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003238 now_ms, __FUNCTION__,
3239 s,
3240 req,
3241 req->rex, req->wex,
3242 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003243 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003244 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003245
Willy Tarreau59234e92008-11-30 23:51:27 +01003246 /*
3247 * Right now, we know that we have processed the entire headers
3248 * and that unwanted requests have been filtered out. We can do
3249 * whatever we want with the remaining request. Also, now we
3250 * may have separate values for ->fe, ->be.
3251 */
Willy Tarreau06619262006-12-17 08:37:22 +01003252
Willy Tarreau59234e92008-11-30 23:51:27 +01003253 /*
3254 * If HTTP PROXY is set we simply get remote server address
3255 * parsing incoming request.
3256 */
3257 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003258 url2sa(req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003260
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003262 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003263 * Note that doing so might move headers in the request, but
3264 * the fields will stay coherent and the URI will not move.
3265 * This should only be performed in the backend.
3266 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003267 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003268 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3269 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003270
Willy Tarreau59234e92008-11-30 23:51:27 +01003271 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003272 * 8: the appsession cookie was looked up very early in 1.2,
3273 * so let's do the same now.
3274 */
3275
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003276 /* It needs to look into the URI unless persistence must be ignored */
3277 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003278 get_srv_from_appsession(s, req->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003279 }
3280
William Lallemanda73203e2012-03-12 12:48:57 +01003281 /* add unique-id if "header-unique-id" is specified */
3282
3283 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3284 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3285
3286 if (s->fe->header_unique_id && s->unique_id) {
3287 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3288 if (ret < 0 || ret > global.tune.bufsize)
3289 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003290 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003291 goto return_bad_req;
3292 }
3293
Cyril Bontéb21570a2009-11-29 20:04:48 +01003294 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003295 * 9: add X-Forwarded-For if either the frontend or the backend
3296 * asks for it.
3297 */
3298 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003299 struct hdr_ctx ctx = { .idx = 0 };
3300
3301 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01003302 http_find_header2(s->be->fwdfor_hdr_name, s->be->fwdfor_hdr_len, req->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003303 /* The header is set to be added only if none is present
3304 * and we found it, so don't do anything.
3305 */
3306 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003307 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003308 /* Add an X-Forwarded-For header unless the source IP is
3309 * in the 'except' network range.
3310 */
3311 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003312 (((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 +01003313 != s->fe->except_net.s_addr) &&
3314 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003315 (((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 +01003316 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003317 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003318 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003319 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003320
3321 /* 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.
3325 */
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);
Ross Westaf72a1d2008-08-03 10:51:45 +02003329 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003330 len = s->fe->fwdfor_hdr_len;
3331 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003332 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003333 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003334
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003335 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003336 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003337 }
3338 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003339 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003340 /* FIXME: for the sake of completeness, we should also support
3341 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003342 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003343 int len;
3344 char pn[INET6_ADDRSTRLEN];
3345 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003346 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003347 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003348
Willy Tarreau59234e92008-11-30 23:51:27 +01003349 /* Note: we rely on the backend to get the header name to be used for
3350 * x-forwarded-for, because the header is really meant for the backends.
3351 * However, if the backend did not specify any option, we have to rely
3352 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003353 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003354 if (s->be->fwdfor_hdr_len) {
3355 len = s->be->fwdfor_hdr_len;
3356 memcpy(trash, s->be->fwdfor_hdr_name, len);
3357 } else {
3358 len = s->fe->fwdfor_hdr_len;
3359 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003360 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003361 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003362
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003363 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003364 goto return_bad_req;
3365 }
3366 }
3367
3368 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003369 * 10: add X-Original-To if either the frontend or the backend
3370 * asks for it.
3371 */
3372 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3373
3374 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003375 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003376 /* Add an X-Original-To header unless the destination IP is
3377 * in the 'except' network range.
3378 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003379 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003380
Willy Tarreau6471afb2011-09-23 10:54:59 +02003381 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003382 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003383 (((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 +02003384 != s->fe->except_to.s_addr) &&
3385 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003386 (((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 +02003387 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003388 int len;
3389 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003390 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003391
3392 /* Note: we rely on the backend to get the header name to be used for
3393 * x-original-to, because the header is really meant for the backends.
3394 * However, if the backend did not specify any option, we have to rely
3395 * on the frontend's header name.
3396 */
3397 if (s->be->orgto_hdr_len) {
3398 len = s->be->orgto_hdr_len;
3399 memcpy(trash, s->be->orgto_hdr_name, len);
3400 } else {
3401 len = s->fe->orgto_hdr_len;
3402 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003403 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003404 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3405
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003406 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003407 goto return_bad_req;
3408 }
3409 }
3410 }
3411
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003412 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3413 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003414 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003415 unsigned int want_flags = 0;
3416
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003417 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003418 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3419 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3420 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003421 want_flags |= TX_CON_CLO_SET;
3422 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003423 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3424 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3425 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003426 want_flags |= TX_CON_KAL_SET;
3427 }
3428
3429 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003430 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003431 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003432
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003433
Willy Tarreau522d6c02009-12-06 18:49:18 +01003434 /* If we have no server assigned yet and we're balancing on url_param
3435 * with a POST request, we may be interested in checking the body for
3436 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003437 */
3438 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3439 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003440 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003441 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003442 buffer_dont_connect(req);
3443 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003444 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003445
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003446 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003447 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003448#ifdef TCP_QUICKACK
3449 /* We expect some data from the client. Unless we know for sure
3450 * we already have a full request, we have to re-enable quick-ack
3451 * in case we previously disabled it, otherwise we might cause
3452 * the client to delay further data.
3453 */
3454 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003455 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003456 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003457 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3458#endif
3459 }
Willy Tarreau03945942009-12-22 16:50:27 +01003460
Willy Tarreau59234e92008-11-30 23:51:27 +01003461 /*************************************************************
3462 * OK, that's finished for the headers. We have done what we *
3463 * could. Let's switch to the DATA state. *
3464 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003465 req->analyse_exp = TICK_ETERNITY;
3466 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003467
Willy Tarreau59234e92008-11-30 23:51:27 +01003468 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003469 /* OK let's go on with the BODY now */
3470 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003471
Willy Tarreau59234e92008-11-30 23:51:27 +01003472 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003473 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003474 /* we detected a parsing error. We want to archive this request
3475 * in the dedicated proxy area for later troubleshooting.
3476 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003477 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003478 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003479
Willy Tarreau59234e92008-11-30 23:51:27 +01003480 txn->req.msg_state = HTTP_MSG_ERROR;
3481 txn->status = 400;
3482 req->analysers = 0;
3483 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003484
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003485 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003486 if (s->listener->counters)
3487 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003488
Willy Tarreau59234e92008-11-30 23:51:27 +01003489 if (!(s->flags & SN_ERR_MASK))
3490 s->flags |= SN_ERR_PRXCOND;
3491 if (!(s->flags & SN_FINST_MASK))
3492 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003493 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003494}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003495
Willy Tarreau60b85b02008-11-30 23:28:40 +01003496/* This function is an analyser which processes the HTTP tarpit. It always
3497 * returns zero, at the beginning because it prevents any other processing
3498 * from occurring, and at the end because it terminates the request.
3499 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003500int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003501{
3502 struct http_txn *txn = &s->txn;
3503
3504 /* This connection is being tarpitted. The CLIENT side has
3505 * already set the connect expiration date to the right
3506 * timeout. We just have to check that the client is still
3507 * there and that the timeout has not expired.
3508 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003509 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003510 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3511 !tick_is_expired(req->analyse_exp, now_ms))
3512 return 0;
3513
3514 /* We will set the queue timer to the time spent, just for
3515 * logging purposes. We fake a 500 server error, so that the
3516 * attacker will not suspect his connection has been tarpitted.
3517 * It will not cause trouble to the logs because we can exclude
3518 * the tarpitted connections by filtering on the 'PT' status flags.
3519 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003520 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3521
3522 txn->status = 500;
3523 if (req->flags != BF_READ_ERROR)
3524 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3525
3526 req->analysers = 0;
3527 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003528
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003529 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003530 if (s->listener->counters)
3531 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003532
Willy Tarreau60b85b02008-11-30 23:28:40 +01003533 if (!(s->flags & SN_ERR_MASK))
3534 s->flags |= SN_ERR_PRXCOND;
3535 if (!(s->flags & SN_FINST_MASK))
3536 s->flags |= SN_FINST_T;
3537 return 0;
3538}
3539
Willy Tarreaud34af782008-11-30 23:36:37 +01003540/* This function is an analyser which processes the HTTP request body. It looks
3541 * for parameters to be used for the load balancing algorithm (url_param). It
3542 * must only be called after the standard HTTP request processing has occurred,
3543 * because it expects the request to be parsed. It returns zero if it needs to
3544 * read more data, or 1 once it has completed its analysis.
3545 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003546int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003547{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003548 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003549 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003550 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003551
3552 /* We have to parse the HTTP request body to find any required data.
3553 * "balance url_param check_post" should have been the only way to get
3554 * into this. We were brought here after HTTP header analysis, so all
3555 * related structures are ready.
3556 */
3557
Willy Tarreau522d6c02009-12-06 18:49:18 +01003558 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3559 goto missing_data;
3560
3561 if (msg->msg_state < HTTP_MSG_100_SENT) {
3562 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3563 * send an HTTP/1.1 100 Continue intermediate response.
3564 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003565 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003566 struct hdr_ctx ctx;
3567 ctx.idx = 0;
3568 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01003569 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003570 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003571 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003572 }
3573 }
3574 msg->msg_state = HTTP_MSG_100_SENT;
3575 }
3576
3577 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003578 /* we have msg->sov which points to the first byte of message body.
3579 * msg->som still points to the beginning of the message. We must
3580 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003581 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003582 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003583
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003584 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003585 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3586 else
3587 msg->msg_state = HTTP_MSG_DATA;
3588 }
3589
3590 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003591 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003592 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003593 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003594 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003595 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003596
Willy Tarreau115acb92009-12-26 13:56:06 +01003597 if (!ret)
3598 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003599 else if (ret < 0) {
3600 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003601 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003602 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003603 }
3604
Willy Tarreaud98cf932009-12-27 22:54:55 +01003605 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003606 * We have the first data byte is in msg->sov. We're waiting for at
3607 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003608 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003609
Willy Tarreau124d9912011-03-01 20:30:48 +01003610 if (msg->body_len < limit)
3611 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003612
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003613 if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003614 goto http_end;
3615
3616 missing_data:
3617 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003618 if (req->flags & BF_FULL) {
3619 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003620 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003621 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003622
Willy Tarreau522d6c02009-12-06 18:49:18 +01003623 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3624 txn->status = 408;
3625 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003626
3627 if (!(s->flags & SN_ERR_MASK))
3628 s->flags |= SN_ERR_CLITO;
3629 if (!(s->flags & SN_FINST_MASK))
3630 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003631 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003632 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003633
3634 /* we get here if we need to wait for more data */
3635 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003636 /* Not enough data. We'll re-use the http-request
3637 * timeout here. Ideally, we should set the timeout
3638 * relative to the accept() date. We just set the
3639 * request timeout once at the beginning of the
3640 * request.
3641 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003642 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003643 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003644 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003645 return 0;
3646 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003647
3648 http_end:
3649 /* The situation will not evolve, so let's give up on the analysis. */
3650 s->logs.tv_request = now; /* update the request timer to reflect full request */
3651 req->analysers &= ~an_bit;
3652 req->analyse_exp = TICK_ETERNITY;
3653 return 1;
3654
3655 return_bad_req: /* let's centralize all bad requests */
3656 txn->req.msg_state = HTTP_MSG_ERROR;
3657 txn->status = 400;
3658 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3659
Willy Tarreau79ebac62010-06-07 13:47:49 +02003660 if (!(s->flags & SN_ERR_MASK))
3661 s->flags |= SN_ERR_PRXCOND;
3662 if (!(s->flags & SN_FINST_MASK))
3663 s->flags |= SN_FINST_R;
3664
Willy Tarreau522d6c02009-12-06 18:49:18 +01003665 return_err_msg:
3666 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003667 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003668 if (s->listener->counters)
3669 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003670 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003671}
3672
Willy Tarreau45c0d982012-03-09 12:11:57 +01003673/* send a server's name with an outgoing request over an established connection */
3674int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003675
3676 struct hdr_ctx ctx;
3677
Mark Lamourinec2247f02012-01-04 13:02:01 -05003678 char *hdr_name = be->server_id_hdr_name;
3679 int hdr_name_len = be->server_id_hdr_len;
3680
3681 char *hdr_val;
3682
William Lallemandd9e90662012-01-30 17:27:17 +01003683 ctx.idx = 0;
3684
Willy Tarreau45c0d982012-03-09 12:11:57 +01003685 while (http_find_header2(hdr_name, hdr_name_len, txn->req.buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003686 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003687 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003688 }
3689
3690 /* Add the new header requested with the server value */
3691 hdr_val = trash;
3692 memcpy(hdr_val, hdr_name, hdr_name_len);
3693 hdr_val += hdr_name_len;
3694 *hdr_val++ = ':';
3695 *hdr_val++ = ' ';
3696 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003697 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003698
3699 return 0;
3700}
3701
Willy Tarreau610ecce2010-01-04 21:15:02 +01003702/* Terminate current transaction and prepare a new one. This is very tricky
3703 * right now but it works.
3704 */
3705void http_end_txn_clean_session(struct session *s)
3706{
3707 /* FIXME: We need a more portable way of releasing a backend's and a
3708 * server's connections. We need a safer way to reinitialize buffer
3709 * flags. We also need a more accurate method for computing per-request
3710 * data.
3711 */
3712 http_silent_debug(__LINE__, s);
3713
3714 s->req->cons->flags |= SI_FL_NOLINGER;
3715 s->req->cons->shutr(s->req->cons);
3716 s->req->cons->shutw(s->req->cons);
3717
3718 http_silent_debug(__LINE__, s);
3719
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003720 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003721 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003722 if (unlikely(s->srv_conn))
3723 sess_change_server(s, NULL);
3724 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003725
3726 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3727 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003728 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003729
3730 if (s->txn.status) {
3731 int n;
3732
3733 n = s->txn.status / 100;
3734 if (n < 1 || n > 5)
3735 n = 0;
3736
3737 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003738 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003739
Willy Tarreau24657792010-02-26 10:30:28 +01003740 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003741 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003742 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003743 }
3744
3745 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003746 s->logs.bytes_in -= s->req->i;
3747 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003748
3749 /* let's do a final log if we need it */
3750 if (s->logs.logwait &&
3751 !(s->flags & SN_MONITOR) &&
3752 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3753 s->do_log(s);
3754 }
3755
3756 s->logs.accept_date = date; /* user-visible date for logging */
3757 s->logs.tv_accept = now; /* corrected date for internal use */
3758 tv_zero(&s->logs.tv_request);
3759 s->logs.t_queue = -1;
3760 s->logs.t_connect = -1;
3761 s->logs.t_data = -1;
3762 s->logs.t_close = 0;
3763 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3764 s->logs.srv_queue_size = 0; /* we will get this number soon */
3765
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003766 s->logs.bytes_in = s->req->total = s->req->i;
3767 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003768
3769 if (s->pend_pos)
3770 pendconn_free(s->pend_pos);
3771
Willy Tarreau827aee92011-03-10 16:55:02 +01003772 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003773 if (s->flags & SN_CURR_SESS) {
3774 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003775 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003777 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3778 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003779 }
3780
Willy Tarreau9e000c62011-03-10 14:03:36 +01003781 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003782
3783 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3784 s->req->cons->fd = -1; /* just to help with debugging */
3785 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003786 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787 s->req->cons->err_loc = NULL;
3788 s->req->cons->exp = TICK_ETERNITY;
3789 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003790 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3791 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 +02003792 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 +01003793 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3794 s->txn.meth = 0;
3795 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003796 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003797 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003798 s->req->cons->flags |= SI_FL_INDEP_STR;
3799
Willy Tarreau96e31212011-05-30 18:10:30 +02003800 if (s->fe->options2 & PR_O2_NODELAY) {
3801 s->req->flags |= BF_NEVER_WAIT;
3802 s->rep->flags |= BF_NEVER_WAIT;
3803 }
3804
Willy Tarreau610ecce2010-01-04 21:15:02 +01003805 /* if the request buffer is not empty, it means we're
3806 * about to process another request, so send pending
3807 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003808 * Just don't do this if the buffer is close to be full,
3809 * because the request will wait for it to flush a little
3810 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003811 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003812 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003813 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003814 !(s->rep->flags & BF_FULL) &&
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02003815 bi_end(s->rep) <= s->rep->data + s->rep->size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003816 s->rep->flags |= BF_EXPECT_MORE;
3817 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003818
3819 /* we're removing the analysers, we MUST re-enable events detection */
3820 buffer_auto_read(s->req);
3821 buffer_auto_close(s->req);
3822 buffer_auto_read(s->rep);
3823 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003824
Willy Tarreau342b11c2010-11-24 16:22:09 +01003825 s->req->analysers = s->listener->analysers;
3826 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003827 s->rep->analysers = 0;
3828
3829 http_silent_debug(__LINE__, s);
3830}
3831
3832
3833/* This function updates the request state machine according to the response
3834 * state machine and buffer flags. It returns 1 if it changes anything (flag
3835 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3836 * it is only used to find when a request/response couple is complete. Both
3837 * this function and its equivalent should loop until both return zero. It
3838 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3839 */
3840int http_sync_req_state(struct session *s)
3841{
3842 struct buffer *buf = s->req;
3843 struct http_txn *txn = &s->txn;
3844 unsigned int old_flags = buf->flags;
3845 unsigned int old_state = txn->req.msg_state;
3846
3847 http_silent_debug(__LINE__, s);
3848 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3849 return 0;
3850
3851 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003852 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003853 * We can shut the read side unless we want to abort_on_close,
3854 * or we have a POST request. The issue with POST requests is
3855 * that some browsers still send a CRLF after the request, and
3856 * this CRLF must be read so that it does not remain in the kernel
3857 * buffers, otherwise a close could cause an RST on some systems
3858 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003859 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003860 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003861 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003862
3863 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3864 goto wait_other_side;
3865
3866 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3867 /* The server has not finished to respond, so we
3868 * don't want to move in order not to upset it.
3869 */
3870 goto wait_other_side;
3871 }
3872
3873 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3874 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003875 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003876 txn->req.msg_state = HTTP_MSG_TUNNEL;
3877 goto wait_other_side;
3878 }
3879
3880 /* When we get here, it means that both the request and the
3881 * response have finished receiving. Depending on the connection
3882 * mode, we'll have to wait for the last bytes to leave in either
3883 * direction, and sometimes for a close to be effective.
3884 */
3885
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003886 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3887 /* Server-close mode : queue a connection close to the server */
3888 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003889 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003890 }
3891 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3892 /* Option forceclose is set, or either side wants to close,
3893 * let's enforce it now that we're not expecting any new
3894 * data to come. The caller knows the session is complete
3895 * once both states are CLOSED.
3896 */
3897 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898 buffer_shutr_now(buf);
3899 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003900 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003901 }
3902 else {
3903 /* The last possible modes are keep-alive and tunnel. Since tunnel
3904 * mode does not set the body analyser, we can't reach this place
3905 * in tunnel mode, so we're left with keep-alive only.
3906 * This mode is currently not implemented, we switch to tunnel mode.
3907 */
3908 buffer_auto_read(buf);
3909 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003910 }
3911
3912 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3913 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003914 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3915
Willy Tarreau610ecce2010-01-04 21:15:02 +01003916 if (!(buf->flags & BF_OUT_EMPTY)) {
3917 txn->req.msg_state = HTTP_MSG_CLOSING;
3918 goto http_msg_closing;
3919 }
3920 else {
3921 txn->req.msg_state = HTTP_MSG_CLOSED;
3922 goto http_msg_closed;
3923 }
3924 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003925 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003926 }
3927
3928 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3929 http_msg_closing:
3930 /* nothing else to forward, just waiting for the output buffer
3931 * to be empty and for the shutw_now to take effect.
3932 */
3933 if (buf->flags & BF_OUT_EMPTY) {
3934 txn->req.msg_state = HTTP_MSG_CLOSED;
3935 goto http_msg_closed;
3936 }
3937 else if (buf->flags & BF_SHUTW) {
3938 txn->req.msg_state = HTTP_MSG_ERROR;
3939 goto wait_other_side;
3940 }
3941 }
3942
3943 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3944 http_msg_closed:
3945 goto wait_other_side;
3946 }
3947
3948 wait_other_side:
3949 http_silent_debug(__LINE__, s);
3950 return txn->req.msg_state != old_state || buf->flags != old_flags;
3951}
3952
3953
3954/* This function updates the response state machine according to the request
3955 * state machine and buffer flags. It returns 1 if it changes anything (flag
3956 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3957 * it is only used to find when a request/response couple is complete. Both
3958 * this function and its equivalent should loop until both return zero. It
3959 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3960 */
3961int http_sync_res_state(struct session *s)
3962{
3963 struct buffer *buf = s->rep;
3964 struct http_txn *txn = &s->txn;
3965 unsigned int old_flags = buf->flags;
3966 unsigned int old_state = txn->rsp.msg_state;
3967
3968 http_silent_debug(__LINE__, s);
3969 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3970 return 0;
3971
3972 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3973 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003974 * still monitor the server connection for a possible close
3975 * while the request is being uploaded, so we don't disable
3976 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003977 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003978 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003979
3980 if (txn->req.msg_state == HTTP_MSG_ERROR)
3981 goto wait_other_side;
3982
3983 if (txn->req.msg_state < HTTP_MSG_DONE) {
3984 /* The client seems to still be sending data, probably
3985 * because we got an error response during an upload.
3986 * We have the choice of either breaking the connection
3987 * or letting it pass through. Let's do the later.
3988 */
3989 goto wait_other_side;
3990 }
3991
3992 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3993 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003994 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003995 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3996 goto wait_other_side;
3997 }
3998
3999 /* When we get here, it means that both the request and the
4000 * response have finished receiving. Depending on the connection
4001 * mode, we'll have to wait for the last bytes to leave in either
4002 * direction, and sometimes for a close to be effective.
4003 */
4004
4005 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4006 /* Server-close mode : shut read and wait for the request
4007 * side to close its output buffer. The caller will detect
4008 * when we're in DONE and the other is in CLOSED and will
4009 * catch that for the final cleanup.
4010 */
4011 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4012 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004014 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4015 /* Option forceclose is set, or either side wants to close,
4016 * let's enforce it now that we're not expecting any new
4017 * data to come. The caller knows the session is complete
4018 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004020 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4021 buffer_shutr_now(buf);
4022 buffer_shutw_now(buf);
4023 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004024 }
4025 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004026 /* The last possible modes are keep-alive and tunnel. Since tunnel
4027 * mode does not set the body analyser, we can't reach this place
4028 * in tunnel mode, so we're left with keep-alive only.
4029 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004030 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004031 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004032 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 }
4034
4035 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4036 /* if we've just closed an output, let's switch */
4037 if (!(buf->flags & BF_OUT_EMPTY)) {
4038 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4039 goto http_msg_closing;
4040 }
4041 else {
4042 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4043 goto http_msg_closed;
4044 }
4045 }
4046 goto wait_other_side;
4047 }
4048
4049 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4050 http_msg_closing:
4051 /* nothing else to forward, just waiting for the output buffer
4052 * to be empty and for the shutw_now to take effect.
4053 */
4054 if (buf->flags & BF_OUT_EMPTY) {
4055 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4056 goto http_msg_closed;
4057 }
4058 else if (buf->flags & BF_SHUTW) {
4059 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004060 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004061 if (target_srv(&s->target))
4062 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004063 goto wait_other_side;
4064 }
4065 }
4066
4067 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4068 http_msg_closed:
4069 /* drop any pending data */
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004070 bi_erase(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004071 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004072 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004073 goto wait_other_side;
4074 }
4075
4076 wait_other_side:
4077 http_silent_debug(__LINE__, s);
4078 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4079}
4080
4081
4082/* Resync the request and response state machines. Return 1 if either state
4083 * changes.
4084 */
4085int http_resync_states(struct session *s)
4086{
4087 struct http_txn *txn = &s->txn;
4088 int old_req_state = txn->req.msg_state;
4089 int old_res_state = txn->rsp.msg_state;
4090
4091 http_silent_debug(__LINE__, s);
4092 http_sync_req_state(s);
4093 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004094 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004095 if (!http_sync_res_state(s))
4096 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004097 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004098 if (!http_sync_req_state(s))
4099 break;
4100 }
4101 http_silent_debug(__LINE__, s);
4102 /* OK, both state machines agree on a compatible state.
4103 * There are a few cases we're interested in :
4104 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4105 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4106 * directions, so let's simply disable both analysers.
4107 * - HTTP_MSG_CLOSED on the response only means we must abort the
4108 * request.
4109 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4110 * with server-close mode means we've completed one request and we
4111 * must re-initialize the server connection.
4112 */
4113
4114 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4115 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4116 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4117 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4118 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004119 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004120 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004121 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004122 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004123 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004124 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004125 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4126 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004127 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004128 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004129 s->rep->analysers = 0;
4130 buffer_auto_close(s->rep);
4131 buffer_auto_read(s->rep);
4132 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004133 buffer_abort(s->req);
4134 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004135 buffer_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004136 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004137 }
4138 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4139 txn->rsp.msg_state == HTTP_MSG_DONE &&
4140 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4141 /* server-close: terminate this server connection and
4142 * reinitialize a fresh-new transaction.
4143 */
4144 http_end_txn_clean_session(s);
4145 }
4146
4147 http_silent_debug(__LINE__, s);
4148 return txn->req.msg_state != old_req_state ||
4149 txn->rsp.msg_state != old_res_state;
4150}
4151
Willy Tarreaud98cf932009-12-27 22:54:55 +01004152/* This function is an analyser which forwards request body (including chunk
4153 * sizes if any). It is called as soon as we must forward, even if we forward
4154 * zero byte. The only situation where it must not be called is when we're in
4155 * tunnel mode and we want to forward till the close. It's used both to forward
4156 * remaining data and to resync after end of body. It expects the msg_state to
4157 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4158 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004159 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004160 * bytes of pending data + the headers if not already done (between som and sov).
4161 * It eventually adjusts som to match sov after the data in between have been sent.
4162 */
4163int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4164{
4165 struct http_txn *txn = &s->txn;
4166 struct http_msg *msg = &s->txn.req;
4167
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004168 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4169 return 0;
4170
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004171 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004172 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004173 /* Output closed while we were sending data. We must abort and
4174 * wake the other side up.
4175 */
4176 msg->msg_state = HTTP_MSG_ERROR;
4177 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004178 return 1;
4179 }
4180
Willy Tarreau4fe41902010-06-07 22:27:41 +02004181 /* in most states, we should abort in case of early close */
4182 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004183
4184 /* Note that we don't have to send 100-continue back because we don't
4185 * need the data to complete our job, and it's up to the server to
4186 * decide whether to return 100, 417 or anything else in return of
4187 * an "Expect: 100-continue" header.
4188 */
4189
4190 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004191 /* we have msg->sov which points to the first byte of message body.
4192 * msg->som still points to the beginning of the message. We must
4193 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004194 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004195 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004196
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004197 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004198 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4199 else {
4200 msg->msg_state = HTTP_MSG_DATA;
4201 }
4202 }
4203
Willy Tarreaud98cf932009-12-27 22:54:55 +01004204 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004205 int bytes;
4206
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004208 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004209 bytes = msg->sov - msg->som;
4210 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004211 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004212 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4213 bytes += req->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01004214 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004215 msg->chunk_len += (unsigned int)bytes;
4216 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004217 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004218
Willy Tarreaucaabe412010-01-03 23:08:28 +01004219 if (msg->msg_state == HTTP_MSG_DATA) {
4220 /* must still forward */
4221 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004222 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004223
4224 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004225 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004226 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004227 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004228 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004229 }
4230 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004231 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004232 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004233 * TRAILERS state.
4234 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004235 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004236
4237 if (!ret)
4238 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004239 else if (ret < 0) {
4240 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004241 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004242 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004243 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004244 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004245 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004246 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004247 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4248 /* we want the CRLF after the data */
4249 int ret;
4250
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004251 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004252
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)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004258 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, 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 MSG_CHUNK_SIZE now */
4262 }
4263 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004264 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004265
4266 if (ret == 0)
4267 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004268 else if (ret < 0) {
4269 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004270 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004271 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004272 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004273 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004274 /* we're in HTTP_MSG_DONE now */
4275 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004276 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004277 int old_state = msg->msg_state;
4278
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004280 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004281 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4282 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4283 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004284 if (http_resync_states(s)) {
4285 /* some state changes occurred, maybe the analyser
4286 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004287 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004288 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4289 if (req->flags & BF_SHUTW) {
4290 /* request errors are most likely due to
4291 * the server aborting the transfer.
4292 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004293 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004294 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004295 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004296 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004298 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004300 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004301
4302 /* If "option abortonclose" is set on the backend, we
4303 * want to monitor the client's connection and forward
4304 * any shutdown notification to the server, which will
4305 * decide whether to close or to go on processing the
4306 * request.
4307 */
4308 if (s->be->options & PR_O_ABRT_CLOSE) {
4309 buffer_auto_read(req);
4310 buffer_auto_close(req);
4311 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004312 else if (s->txn.meth == HTTP_METH_POST) {
4313 /* POST requests may require to read extra CRLF
4314 * sent by broken browsers and which could cause
4315 * an RST to be sent upon close on some systems
4316 * (eg: Linux).
4317 */
4318 buffer_auto_read(req);
4319 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004320
Willy Tarreau610ecce2010-01-04 21:15:02 +01004321 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004322 }
4323 }
4324
Willy Tarreaud98cf932009-12-27 22:54:55 +01004325 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004326 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004327 if (req->flags & BF_SHUTR) {
4328 if (!(s->flags & SN_ERR_MASK))
4329 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004330 if (!(s->flags & SN_FINST_MASK)) {
4331 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4332 s->flags |= SN_FINST_H;
4333 else
4334 s->flags |= SN_FINST_D;
4335 }
4336
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004337 s->fe->fe_counters.cli_aborts++;
4338 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004339 if (target_srv(&s->target))
4340 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004341
4342 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004343 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004344
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004345 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004346 if (req->flags & BF_SHUTW)
4347 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004348
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004349 /* When TE: chunked is used, we need to get there again to parse remaining
4350 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4351 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004352 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004353 buffer_dont_close(req);
4354
Willy Tarreau5c620922011-05-11 19:56:11 +02004355 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004356 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4357 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004358 * modes are already handled by the stream sock layer. We must not do
4359 * this in content-length mode because it could present the MSG_MORE
4360 * flag with the last block of forwarded data, which would cause an
4361 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004362 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004363 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004364 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004365
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004367 return 0;
4368
Willy Tarreaud98cf932009-12-27 22:54:55 +01004369 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004370 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004371 if (s->listener->counters)
4372 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004373 return_bad_req_stats_ok:
4374 txn->req.msg_state = HTTP_MSG_ERROR;
4375 if (txn->status) {
4376 /* Note: we don't send any error if some data were already sent */
4377 stream_int_retnclose(req->prod, NULL);
4378 } else {
4379 txn->status = 400;
4380 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4381 }
4382 req->analysers = 0;
4383 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004384
4385 if (!(s->flags & SN_ERR_MASK))
4386 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004387 if (!(s->flags & SN_FINST_MASK)) {
4388 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4389 s->flags |= SN_FINST_H;
4390 else
4391 s->flags |= SN_FINST_D;
4392 }
4393 return 0;
4394
4395 aborted_xfer:
4396 txn->req.msg_state = HTTP_MSG_ERROR;
4397 if (txn->status) {
4398 /* Note: we don't send any error if some data were already sent */
4399 stream_int_retnclose(req->prod, NULL);
4400 } else {
4401 txn->status = 502;
4402 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4403 }
4404 req->analysers = 0;
4405 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4406
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004407 s->fe->fe_counters.srv_aborts++;
4408 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004409 if (target_srv(&s->target))
4410 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004411
4412 if (!(s->flags & SN_ERR_MASK))
4413 s->flags |= SN_ERR_SRVCL;
4414 if (!(s->flags & SN_FINST_MASK)) {
4415 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4416 s->flags |= SN_FINST_H;
4417 else
4418 s->flags |= SN_FINST_D;
4419 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004420 return 0;
4421}
4422
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004423/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4424 * processing can continue on next analysers, or zero if it either needs more
4425 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4426 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4427 * when it has nothing left to do, and may remove any analyser when it wants to
4428 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004429 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004430int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004431{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004432 struct http_txn *txn = &s->txn;
4433 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004434 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004435 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004436 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004437 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004438
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004439 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaufa7e1022008-10-19 07:30:41 +02004440 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004441 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004442 rep,
4443 rep->rex, rep->wex,
4444 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004445 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004446 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004447
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004448 /*
4449 * Now parse the partial (or complete) lines.
4450 * We will check the response syntax, and also join multi-line
4451 * headers. An index of all the lines will be elaborated while
4452 * parsing.
4453 *
4454 * For the parsing, we use a 28 states FSM.
4455 *
4456 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004457 * rep->data + msg->som = beginning of response
4458 * rep->data + msg->eoh = end of processed headers / start of current one
4459 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01004460 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004461 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004462 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004463 */
4464
Willy Tarreau83e3af02009-12-28 17:39:57 +01004465 /* There's a protected area at the end of the buffer for rewriting
4466 * purposes. We don't want to start to parse the request if the
4467 * protected area is affected, because we may have to move processed
4468 * data later, which is much more complicated.
4469 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004470 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004471 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02004472 bi_end(rep) < b_ptr(rep, msg->next) ||
4473 bi_end(rep) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004474 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004475 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004476 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4477 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004478 buffer_dont_close(rep);
4479 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4480 return 0;
4481 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004482 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreau21710ff2012-03-09 13:58:04 +01004483 http_message_realign(msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004484 }
4485
Willy Tarreaua458b672012-03-05 11:17:50 +01004486 if (likely(msg->next < rep->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004487 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004488 }
4489
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004490 /* 1: we might have to print this header in debug mode */
4491 if (unlikely((global.mode & MODE_DEBUG) &&
4492 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004493 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004494 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004495 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004496
Willy Tarreauea1175a2012-03-05 15:52:30 +01004497 sol = rep->p + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004498 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004499 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004500
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004501 sol += hdr_idx_first_pos(&txn->hdr_idx);
4502 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004503
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004504 while (cur_idx) {
4505 eol = sol + txn->hdr_idx.v[cur_idx].len;
4506 debug_hdr("srvhdr", s, sol, eol);
4507 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4508 cur_idx = txn->hdr_idx.v[cur_idx].next;
4509 }
4510 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004511
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004512 /*
4513 * Now we quickly check if we have found a full valid response.
4514 * If not so, we check the FD and buffer states before leaving.
4515 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004516 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004517 * responses are checked first.
4518 *
4519 * Depending on whether the client is still there or not, we
4520 * may send an error response back or not. Note that normally
4521 * we should only check for HTTP status there, and check I/O
4522 * errors somewhere else.
4523 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004524
Willy Tarreau655dce92009-11-08 13:10:58 +01004525 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004526 /* Invalid response */
4527 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4528 /* we detected a parsing error. We want to archive this response
4529 * in the dedicated proxy area for later troubleshooting.
4530 */
4531 hdr_response_bad:
4532 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004533 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004534
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004535 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004536 if (target_srv(&s->target)) {
4537 target_srv(&s->target)->counters.failed_resp++;
4538 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004539 }
Willy Tarreau64648412010-03-05 10:41:54 +01004540 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004541 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004542 rep->analysers = 0;
4543 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004544 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004545 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004546 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4547
4548 if (!(s->flags & SN_ERR_MASK))
4549 s->flags |= SN_ERR_PRXCOND;
4550 if (!(s->flags & SN_FINST_MASK))
4551 s->flags |= SN_FINST_H;
4552
4553 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004554 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004555
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004556 /* too large response does not fit in buffer. */
4557 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004558 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004559 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004560 goto hdr_response_bad;
4561 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004562
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004563 /* read error */
4564 else if (rep->flags & BF_READ_ERROR) {
4565 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004566 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004567
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004568 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004569 if (target_srv(&s->target)) {
4570 target_srv(&s->target)->counters.failed_resp++;
4571 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004572 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004573
Willy Tarreau90deb182010-01-07 00:20:41 +01004574 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004575 rep->analysers = 0;
4576 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004577 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004578 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004579 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004580
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004581 if (!(s->flags & SN_ERR_MASK))
4582 s->flags |= SN_ERR_SRVCL;
4583 if (!(s->flags & SN_FINST_MASK))
4584 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004585 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004586 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004587
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004588 /* read timeout : return a 504 to the client. */
4589 else if (rep->flags & BF_READ_TIMEOUT) {
4590 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004591 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004592
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004593 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004594 if (target_srv(&s->target)) {
4595 target_srv(&s->target)->counters.failed_resp++;
4596 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004597 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004598
Willy Tarreau90deb182010-01-07 00:20:41 +01004599 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004600 rep->analysers = 0;
4601 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004602 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004603 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004604 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004605
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004606 if (!(s->flags & SN_ERR_MASK))
4607 s->flags |= SN_ERR_SRVTO;
4608 if (!(s->flags & SN_FINST_MASK))
4609 s->flags |= SN_FINST_H;
4610 return 0;
4611 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004612
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004613 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004614 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004615 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004616 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004617
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004618 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004619 if (target_srv(&s->target)) {
4620 target_srv(&s->target)->counters.failed_resp++;
4621 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004622 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004623
Willy Tarreau90deb182010-01-07 00:20:41 +01004624 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004625 rep->analysers = 0;
4626 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004627 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004628 bi_erase(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004630
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004631 if (!(s->flags & SN_ERR_MASK))
4632 s->flags |= SN_ERR_SRVCL;
4633 if (!(s->flags & SN_FINST_MASK))
4634 s->flags |= SN_FINST_H;
4635 return 0;
4636 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004637
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004638 /* write error to client (we don't send any message then) */
4639 else if (rep->flags & BF_WRITE_ERROR) {
4640 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004641 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004642
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004643 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004644 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004645 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004646
4647 if (!(s->flags & SN_ERR_MASK))
4648 s->flags |= SN_ERR_CLICL;
4649 if (!(s->flags & SN_FINST_MASK))
4650 s->flags |= SN_FINST_H;
4651
4652 /* process_session() will take care of the error */
4653 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004654 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004655
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004656 buffer_dont_close(rep);
4657 return 0;
4658 }
4659
4660 /* More interesting part now : we know that we have a complete
4661 * response which at least looks like HTTP. We have an indicator
4662 * of each header's length, so we can parse them quickly.
4663 */
4664
4665 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004666 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004667
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004668 /*
4669 * 1: get the status code
4670 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01004671 n = rep->p[msg->sol + msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004672 if (n < 1 || n > 5)
4673 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004674 /* when the client triggers a 4xx from the server, it's most often due
4675 * to a missing object or permission. These events should be tracked
4676 * because if they happen often, it may indicate a brute force or a
4677 * vulnerability scan.
4678 */
4679 if (n == 4)
4680 session_inc_http_err_ctr(s);
4681
Willy Tarreau827aee92011-03-10 16:55:02 +01004682 if (target_srv(&s->target))
4683 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004684
Willy Tarreau5b154472009-12-21 20:11:07 +01004685 /* check if the response is HTTP/1.1 or above */
4686 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004687 ((rep->p[msg->sol + 5] > '1') ||
4688 ((rep->p[msg->sol + 5] == '1') &&
4689 (rep->p[msg->sol + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004690 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004691
4692 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004693 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 +01004694
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004695 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004696 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004697
Willy Tarreau3a215be2012-03-09 21:39:51 +01004698 txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004699
Willy Tarreau39650402010-03-15 19:44:39 +01004700 /* Adjust server's health based on status code. Note: status codes 501
4701 * and 505 are triggered on demand by client request, so we must not
4702 * count them as server failures.
4703 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004704 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004705 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004706 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004707 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004708 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004709 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004710
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004711 /*
4712 * 2: check for cacheability.
4713 */
4714
4715 switch (txn->status) {
4716 case 200:
4717 case 203:
4718 case 206:
4719 case 300:
4720 case 301:
4721 case 410:
4722 /* RFC2616 @13.4:
4723 * "A response received with a status code of
4724 * 200, 203, 206, 300, 301 or 410 MAY be stored
4725 * by a cache (...) unless a cache-control
4726 * directive prohibits caching."
4727 *
4728 * RFC2616 @9.5: POST method :
4729 * "Responses to this method are not cacheable,
4730 * unless the response includes appropriate
4731 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004732 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004733 if (likely(txn->meth != HTTP_METH_POST) &&
4734 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4735 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4736 break;
4737 default:
4738 break;
4739 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004740
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004741 /*
4742 * 3: we may need to capture headers
4743 */
4744 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004745 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01004746 capture_headers(rep->p + msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004747 txn->rsp.cap, s->fe->rsp_cap);
4748
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004749 /* 4: determine the transfer-length.
4750 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4751 * the presence of a message-body in a RESPONSE and its transfer length
4752 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004753 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004754 * All responses to the HEAD request method MUST NOT include a
4755 * message-body, even though the presence of entity-header fields
4756 * might lead one to believe they do. All 1xx (informational), 204
4757 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4758 * message-body. All other responses do include a message-body,
4759 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004760 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004761 * 1. Any response which "MUST NOT" include a message-body (such as the
4762 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4763 * always terminated by the first empty line after the header fields,
4764 * regardless of the entity-header fields present in the message.
4765 *
4766 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4767 * the "chunked" transfer-coding (Section 6.2) is used, the
4768 * transfer-length is defined by the use of this transfer-coding.
4769 * If a Transfer-Encoding header field is present and the "chunked"
4770 * transfer-coding is not present, the transfer-length is defined by
4771 * the sender closing the connection.
4772 *
4773 * 3. If a Content-Length header field is present, its decimal value in
4774 * OCTETs represents both the entity-length and the transfer-length.
4775 * If a message is received with both a Transfer-Encoding header
4776 * field and a Content-Length header field, the latter MUST be ignored.
4777 *
4778 * 4. If the message uses the media type "multipart/byteranges", and
4779 * the transfer-length is not otherwise specified, then this self-
4780 * delimiting media type defines the transfer-length. This media
4781 * type MUST NOT be used unless the sender knows that the recipient
4782 * can parse it; the presence in a request of a Range header with
4783 * multiple byte-range specifiers from a 1.1 client implies that the
4784 * client can parse multipart/byteranges responses.
4785 *
4786 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004787 */
4788
4789 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004790 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004791 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004792 * FIXME: should we parse anyway and return an error on chunked encoding ?
4793 */
4794 if (txn->meth == HTTP_METH_HEAD ||
4795 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004796 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004797 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004798 goto skip_content_length;
4799 }
4800
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004801 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004802 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004803 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004804 http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004805 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004806 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4807 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004808 /* bad transfer-encoding (chunked followed by something else) */
4809 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004810 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004811 break;
4812 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004813 }
4814
4815 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4816 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004817 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004818 http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004819 signed long long cl;
4820
Willy Tarreauad14f752011-09-02 20:33:27 +02004821 if (!ctx.vlen) {
4822 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004823 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004824 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004825
Willy Tarreauad14f752011-09-02 20:33:27 +02004826 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4827 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004828 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004829 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004830
Willy Tarreauad14f752011-09-02 20:33:27 +02004831 if (cl < 0) {
4832 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004833 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004834 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004835
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004836 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02004837 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004838 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004839 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004840
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004841 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004842 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004843 }
4844
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004845 /* FIXME: we should also implement the multipart/byterange method.
4846 * For now on, we resort to close mode in this case (unknown length).
4847 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004848skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004849
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004850 /* end of job, return OK */
4851 rep->analysers &= ~an_bit;
4852 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004853 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004854 return 1;
4855}
4856
4857/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004858 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4859 * and updates t->rep->analysers. It might make sense to explode it into several
4860 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004861 */
4862int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4863{
4864 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004865 struct http_msg *msg = &txn->rsp;
4866 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004867 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004868
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004869 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004870 now_ms, __FUNCTION__,
4871 t,
4872 rep,
4873 rep->rex, rep->wex,
4874 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004875 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004876 rep->analysers);
4877
Willy Tarreau655dce92009-11-08 13:10:58 +01004878 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004879 return 0;
4880
4881 rep->analysers &= ~an_bit;
4882 rep->analyse_exp = TICK_ETERNITY;
4883
Willy Tarreau5b154472009-12-21 20:11:07 +01004884 /* Now we have to check if we need to modify the Connection header.
4885 * This is more difficult on the response than it is on the request,
4886 * because we can have two different HTTP versions and we don't know
4887 * how the client will interprete a response. For instance, let's say
4888 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4889 * HTTP/1.1 response without any header. Maybe it will bound itself to
4890 * HTTP/1.0 because it only knows about it, and will consider the lack
4891 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4892 * the lack of header as a keep-alive. Thus we will use two flags
4893 * indicating how a request MAY be understood by the client. In case
4894 * of multiple possibilities, we'll fix the header to be explicit. If
4895 * ambiguous cases such as both close and keepalive are seen, then we
4896 * will fall back to explicit close. Note that we won't take risks with
4897 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004898 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004899 */
4900
Willy Tarreaudc008c52010-02-01 16:20:08 +01004901 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4902 txn->status == 101)) {
4903 /* Either we've established an explicit tunnel, or we're
4904 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004905 * to understand the next protocols. We have to switch to tunnel
4906 * mode, so that we transfer the request and responses then let
4907 * this protocol pass unmodified. When we later implement specific
4908 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004909 * header which contains information about that protocol for
4910 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004911 */
4912 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4913 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004914 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4915 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4916 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004917 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004918
Willy Tarreau60466522010-01-18 19:08:45 +01004919 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004920 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004921 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4922 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004923
Willy Tarreau60466522010-01-18 19:08:45 +01004924 /* now adjust header transformations depending on current state */
4925 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4926 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4927 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004928 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004929 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004930 }
Willy Tarreau60466522010-01-18 19:08:45 +01004931 else { /* SCL / KAL */
4932 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004933 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004934 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004935 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004936
Willy Tarreau60466522010-01-18 19:08:45 +01004937 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004938 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004939
Willy Tarreau60466522010-01-18 19:08:45 +01004940 /* Some keep-alive responses are converted to Server-close if
4941 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004942 */
Willy Tarreau60466522010-01-18 19:08:45 +01004943 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4944 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004945 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004946 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004947 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004948 }
4949
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004950 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004951 /*
4952 * 3: we will have to evaluate the filters.
4953 * As opposed to version 1.2, now they will be evaluated in the
4954 * filters order and not in the header order. This means that
4955 * each filter has to be validated among all headers.
4956 *
4957 * Filters are tried with ->be first, then with ->fe if it is
4958 * different from ->be.
4959 */
4960
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004961 cur_proxy = t->be;
4962 while (1) {
4963 struct proxy *rule_set = cur_proxy;
4964
4965 /* try headers filters */
4966 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004967 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004968 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004969 if (target_srv(&t->target)) {
4970 target_srv(&t->target)->counters.failed_resp++;
4971 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004972 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004973 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004974 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004975 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004976 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004977 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004978 bi_erase(rep);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004979 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004980 if (!(t->flags & SN_ERR_MASK))
4981 t->flags |= SN_ERR_PRXCOND;
4982 if (!(t->flags & SN_FINST_MASK))
4983 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004984 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004985 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004986 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004987
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004988 /* has the response been denied ? */
4989 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004990 if (target_srv(&t->target))
4991 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004992
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004993 t->be->be_counters.denied_resp++;
4994 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004995 if (t->listener->counters)
4996 t->listener->counters->denied_resp++;
4997
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004998 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004999 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005000
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005001 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005002 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005003 if (txn->status < 200)
5004 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005005 if (wl->cond) {
5006 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5007 ret = acl_pass(ret);
5008 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5009 ret = !ret;
5010 if (!ret)
5011 continue;
5012 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005013 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005014 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005015 }
5016
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005017 /* check whether we're already working on the frontend */
5018 if (cur_proxy == t->fe)
5019 break;
5020 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005021 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005022
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005023 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005024 * We may be facing a 100-continue response, in which case this
5025 * is not the right response, and we're waiting for the next one.
5026 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005027 * next one.
5028 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005029 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005030 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau3a215be2012-03-09 21:39:51 +01005031 msg->next -= buffer_forward(rep, msg->next - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005032 msg->msg_state = HTTP_MSG_RPBEFORE;
5033 txn->status = 0;
5034 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5035 return 1;
5036 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005037 else if (unlikely(txn->status < 200))
5038 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005039
5040 /* we don't have any 1xx status code now */
5041
5042 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005043 * 4: check for server cookie.
5044 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005045 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5046 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005047 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005048
Willy Tarreaubaaee002006-06-26 02:48:02 +02005049
Willy Tarreaua15645d2007-03-18 16:22:39 +01005050 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005051 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005052 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005053 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005054 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005055
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005056 /*
5057 * 6: add server cookie in the response if needed
5058 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005059 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005060 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005061 (!(t->flags & SN_DIRECT) ||
5062 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5063 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5064 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5065 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005066 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5067 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005068 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005069 /* the server is known, it's not the one the client requested, or the
5070 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005071 * insert a set-cookie here, except if we want to insert only on POST
5072 * requests and this one isn't. Note that servers which don't have cookies
5073 * (eg: some backup servers) will return a full cookie removal request.
5074 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005075 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005076 len = sprintf(trash,
5077 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5078 t->be->cookie_name);
5079 }
5080 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005081 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005082
5083 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5084 /* emit last_date, which is mandatory */
5085 trash[len++] = COOKIE_DELIM_DATE;
5086 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5087 if (t->be->cookie_maxlife) {
5088 /* emit first_date, which is either the original one or
5089 * the current date.
5090 */
5091 trash[len++] = COOKIE_DELIM_DATE;
5092 s30tob64(txn->cookie_first_date ?
5093 txn->cookie_first_date >> 2 :
5094 (date.tv_sec+3) >> 2, trash + len);
5095 len += 5;
5096 }
5097 }
5098 len += sprintf(trash + len, "; path=/");
5099 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005100
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005101 if (t->be->cookie_domain)
5102 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005103
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005104 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005105 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005106
Willy Tarreauf1348312010-10-07 15:54:11 +02005107 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005108 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005109 /* the server did not change, only the date was updated */
5110 txn->flags |= TX_SCK_UPDATED;
5111 else
5112 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005113
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005114 /* Here, we will tell an eventual cache on the client side that we don't
5115 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5116 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5117 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5118 */
5119 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005120
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005121 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5122
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005123 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005124 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005125 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005126 }
5127 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005128
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005129 /*
5130 * 7: check if result will be cacheable with a cookie.
5131 * We'll block the response if security checks have caught
5132 * nasty things such as a cacheable cookie.
5133 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005134 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5135 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005136 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005137
5138 /* we're in presence of a cacheable response containing
5139 * a set-cookie header. We'll block it as requested by
5140 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005141 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005142 if (target_srv(&t->target))
5143 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005144
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005145 t->be->be_counters.denied_resp++;
5146 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005147 if (t->listener->counters)
5148 t->listener->counters->denied_resp++;
5149
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005150 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005151 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005152 send_log(t->be, LOG_ALERT,
5153 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005154 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005155 goto return_srv_prx_502;
5156 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005157
5158 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005159 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005160 */
Willy Tarreau60466522010-01-18 19:08:45 +01005161 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5162 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5163 unsigned int want_flags = 0;
5164
5165 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5166 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5167 /* we want a keep-alive response here. Keep-alive header
5168 * required if either side is not 1.1.
5169 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005170 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005171 want_flags |= TX_CON_KAL_SET;
5172 }
5173 else {
5174 /* we want a close response here. Close header required if
5175 * the server is 1.1, regardless of the client.
5176 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005177 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005178 want_flags |= TX_CON_CLO_SET;
5179 }
5180
5181 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005182 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005183 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005184
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005185 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005186 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005187 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005188 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005189
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005190 /*************************************************************
5191 * OK, that's finished for the headers. We have done what we *
5192 * could. Let's switch to the DATA state. *
5193 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005194
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005196
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005197 /* if the user wants to log as soon as possible, without counting
5198 * bytes from the server, then this is the right moment. We have
5199 * to temporarily assign bytes_out to log what we currently have.
5200 */
5201 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5202 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5203 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005204 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005205 t->logs.bytes_out = 0;
5206 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005207
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005208 /* Note: we must not try to cheat by jumping directly to DATA,
5209 * otherwise we would not let the client side wake up.
5210 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005211
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005212 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005213 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005214 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005215}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005216
Willy Tarreaud98cf932009-12-27 22:54:55 +01005217/* This function is an analyser which forwards response body (including chunk
5218 * sizes if any). It is called as soon as we must forward, even if we forward
5219 * zero byte. The only situation where it must not be called is when we're in
5220 * tunnel mode and we want to forward till the close. It's used both to forward
5221 * remaining data and to resync after end of body. It expects the msg_state to
5222 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5223 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005224 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005225 * bytes of pending data + the headers if not already done (between som and sov).
5226 * It eventually adjusts som to match sov after the data in between have been sent.
5227 */
5228int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5229{
5230 struct http_txn *txn = &s->txn;
5231 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005232 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005233
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005234 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5235 return 0;
5236
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005237 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005238 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005239 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005240 /* Output closed while we were sending data. We must abort and
5241 * wake the other side up.
5242 */
5243 msg->msg_state = HTTP_MSG_ERROR;
5244 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005245 return 1;
5246 }
5247
Willy Tarreau4fe41902010-06-07 22:27:41 +02005248 /* in most states, we should abort in case of early close */
5249 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005250
Willy Tarreaud98cf932009-12-27 22:54:55 +01005251 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005252 /* we have msg->sov which points to the first byte of message body.
5253 * msg->som still points to the beginning of the message. We must
5254 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005255 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005256 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005257
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005258 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005259 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5260 else {
5261 msg->msg_state = HTTP_MSG_DATA;
5262 }
5263 }
5264
Willy Tarreaud98cf932009-12-27 22:54:55 +01005265 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005266 int bytes;
5267
Willy Tarreau610ecce2010-01-04 21:15:02 +01005268 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005269 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005270 bytes = msg->sov - msg->som;
5271 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005272 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005273 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5274 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005275 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005276 msg->chunk_len += (unsigned int)bytes;
5277 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005278 }
5279
Willy Tarreaucaabe412010-01-03 23:08:28 +01005280 if (msg->msg_state == HTTP_MSG_DATA) {
5281 /* must still forward */
5282 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005283 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005284
5285 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005286 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005287 msg->msg_state = HTTP_MSG_DATA_CRLF;
5288 else
5289 msg->msg_state = HTTP_MSG_DONE;
5290 }
5291 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005292 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005293 * set ->sov and ->next to point to the body and switch to DATA or
5294 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005295 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005296 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005297
5298 if (!ret)
5299 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005300 else if (ret < 0) {
5301 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005302 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005303 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005304 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005305 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005306 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005307 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5308 /* we want the CRLF after the data */
5309 int ret;
5310
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005311 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005312
5313 if (!ret)
5314 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005315 else if (ret < 0) {
5316 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005317 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005318 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005319 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005320 /* we're in MSG_CHUNK_SIZE now */
5321 }
5322 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005323 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005324
Willy Tarreaud98cf932009-12-27 22:54:55 +01005325 if (ret == 0)
5326 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005327 else if (ret < 0) {
5328 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005329 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005330 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005331 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005332 /* we're in HTTP_MSG_DONE now */
5333 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005334 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005335 int old_state = msg->msg_state;
5336
Willy Tarreau610ecce2010-01-04 21:15:02 +01005337 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005338 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005339 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5340 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5341 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005342 if (http_resync_states(s)) {
5343 http_silent_debug(__LINE__, s);
5344 /* some state changes occurred, maybe the analyser
5345 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005346 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005347 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5348 if (res->flags & BF_SHUTW) {
5349 /* response errors are most likely due to
5350 * the client aborting the transfer.
5351 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005352 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005353 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005354 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005355 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005356 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005357 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005358 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005359 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005360 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005361 }
5362 }
5363
Willy Tarreaud98cf932009-12-27 22:54:55 +01005364 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005365 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005366 if (res->flags & BF_SHUTR) {
5367 if (!(s->flags & SN_ERR_MASK))
5368 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005369 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005370 if (target_srv(&s->target))
5371 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005372 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005373 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005374
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005375 if (res->flags & BF_SHUTW)
5376 goto aborted_xfer;
5377
Willy Tarreau40dba092010-03-04 18:14:51 +01005378 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005379 if (!s->req->analysers)
5380 goto return_bad_res;
5381
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005382 /* forward any pending data */
5383 bytes = msg->sov - msg->som;
5384 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005385 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005386 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5387 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005388 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005389 msg->chunk_len += (unsigned int)bytes;
5390 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005391 }
5392
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005393 /* When TE: chunked is used, we need to get there again to parse remaining
5394 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5395 * Similarly, with keep-alive on the client side, we don't want to forward a
5396 * close.
5397 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005398 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005399 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5400 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5401 buffer_dont_close(res);
5402
Willy Tarreau5c620922011-05-11 19:56:11 +02005403 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005404 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5405 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005406 * modes are already handled by the stream sock layer. We must not do
5407 * this in content-length mode because it could present the MSG_MORE
5408 * flag with the last block of forwarded data, which would cause an
5409 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005410 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005411 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005412 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005413
Willy Tarreaud98cf932009-12-27 22:54:55 +01005414 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005415 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005416 return 0;
5417
Willy Tarreau40dba092010-03-04 18:14:51 +01005418 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005419 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005420 if (target_srv(&s->target))
5421 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005422
5423 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005424 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005425 /* don't send any error message as we're in the body */
5426 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005427 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005428 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005429 if (target_srv(&s->target))
5430 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005431
5432 if (!(s->flags & SN_ERR_MASK))
5433 s->flags |= SN_ERR_PRXCOND;
5434 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005435 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005436 return 0;
5437
5438 aborted_xfer:
5439 txn->rsp.msg_state = HTTP_MSG_ERROR;
5440 /* don't send any error message as we're in the body */
5441 stream_int_retnclose(res->cons, NULL);
5442 res->analysers = 0;
5443 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5444
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005445 s->fe->fe_counters.cli_aborts++;
5446 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005447 if (target_srv(&s->target))
5448 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005449
5450 if (!(s->flags & SN_ERR_MASK))
5451 s->flags |= SN_ERR_CLICL;
5452 if (!(s->flags & SN_FINST_MASK))
5453 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005454 return 0;
5455}
5456
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005457/* Iterate the same filter through all request headers.
5458 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005459 * Since it can manage the switch to another backend, it updates the per-proxy
5460 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005461 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005462int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005463{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005464 char term;
5465 char *cur_ptr, *cur_end, *cur_next;
5466 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005467 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005468 struct hdr_idx_elem *cur_hdr;
5469 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005470
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005471 last_hdr = 0;
5472
Willy Tarreau3a215be2012-03-09 21:39:51 +01005473 cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005474 old_idx = 0;
5475
5476 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005477 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005478 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005479 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005480 (exp->action == ACT_ALLOW ||
5481 exp->action == ACT_DENY ||
5482 exp->action == ACT_TARPIT))
5483 return 0;
5484
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005485 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005486 if (!cur_idx)
5487 break;
5488
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005489 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005490 cur_ptr = cur_next;
5491 cur_end = cur_ptr + cur_hdr->len;
5492 cur_next = cur_end + cur_hdr->cr + 1;
5493
5494 /* Now we have one header between cur_ptr and cur_end,
5495 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005496 */
5497
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005498 /* The annoying part is that pattern matching needs
5499 * that we modify the contents to null-terminate all
5500 * strings before testing them.
5501 */
5502
5503 term = *cur_end;
5504 *cur_end = '\0';
5505
5506 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5507 switch (exp->action) {
5508 case ACT_SETBE:
5509 /* It is not possible to jump a second time.
5510 * FIXME: should we return an HTTP/500 here so that
5511 * the admin knows there's a problem ?
5512 */
5513 if (t->be != t->fe)
5514 break;
5515
5516 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005517 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005518 last_hdr = 1;
5519 break;
5520
5521 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005522 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005523 last_hdr = 1;
5524 break;
5525
5526 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005527 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005528 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005529
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005530 t->fe->fe_counters.denied_req++;
5531 if (t->fe != t->be)
5532 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005533 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005534 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005535
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005536 break;
5537
5538 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005539 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005540 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005541
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005542 t->fe->fe_counters.denied_req++;
5543 if (t->fe != t->be)
5544 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005545 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005546 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005547
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005548 break;
5549
5550 case ACT_REPLACE:
5551 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5552 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5553 /* FIXME: if the user adds a newline in the replacement, the
5554 * index will not be recalculated for now, and the new line
5555 * will not be counted as a new header.
5556 */
5557
5558 cur_end += delta;
5559 cur_next += delta;
5560 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005561 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005562 break;
5563
5564 case ACT_REMOVE:
5565 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5566 cur_next += delta;
5567
Willy Tarreaufa355d42009-11-29 18:12:29 +01005568 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005569 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5570 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005571 cur_hdr->len = 0;
5572 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005573 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005574 break;
5575
5576 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005577 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005578 if (cur_end)
5579 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005581 /* keep the link from this header to next one in case of later
5582 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005583 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005584 old_idx = cur_idx;
5585 }
5586 return 0;
5587}
5588
5589
5590/* Apply the filter to the request line.
5591 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5592 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005593 * Since it can manage the switch to another backend, it updates the per-proxy
5594 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005595 */
5596int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5597{
5598 char term;
5599 char *cur_ptr, *cur_end;
5600 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005601 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005602 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005603
Willy Tarreau58f10d72006-12-04 02:26:12 +01005604
Willy Tarreau3d300592007-03-18 18:34:41 +01005605 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005606 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005607 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005608 (exp->action == ACT_ALLOW ||
5609 exp->action == ACT_DENY ||
5610 exp->action == ACT_TARPIT))
5611 return 0;
5612 else if (exp->action == ACT_REMOVE)
5613 return 0;
5614
5615 done = 0;
5616
Willy Tarreau3a215be2012-03-09 21:39:51 +01005617 cur_ptr = req->p + txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005618 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005619
5620 /* Now we have the request line between cur_ptr and cur_end */
5621
5622 /* The annoying part is that pattern matching needs
5623 * that we modify the contents to null-terminate all
5624 * strings before testing them.
5625 */
5626
5627 term = *cur_end;
5628 *cur_end = '\0';
5629
5630 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5631 switch (exp->action) {
5632 case ACT_SETBE:
5633 /* It is not possible to jump a second time.
5634 * FIXME: should we return an HTTP/500 here so that
5635 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005636 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005637 if (t->be != t->fe)
5638 break;
5639
5640 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005641 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005642 done = 1;
5643 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005644
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005645 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005646 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005647 done = 1;
5648 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005649
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005650 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005651 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005652
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005653 t->fe->fe_counters.denied_req++;
5654 if (t->fe != t->be)
5655 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005656 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005657 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005658
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005659 done = 1;
5660 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005661
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005662 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005663 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005664
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005665 t->fe->fe_counters.denied_req++;
5666 if (t->fe != t->be)
5667 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005668 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005669 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005670
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005671 done = 1;
5672 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005673
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005674 case ACT_REPLACE:
5675 *cur_end = term; /* restore the string terminator */
5676 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5677 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5678 /* FIXME: if the user adds a newline in the replacement, the
5679 * index will not be recalculated for now, and the new line
5680 * will not be counted as a new header.
5681 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005682
Willy Tarreaufa355d42009-11-29 18:12:29 +01005683 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005684 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01005685 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 HTTP_MSG_RQMETH,
5687 cur_ptr, cur_end + 1,
5688 NULL, NULL);
5689 if (unlikely(!cur_end))
5690 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005691
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005692 /* we have a full request and we know that we have either a CR
5693 * or an LF at <ptr>.
5694 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005695 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5696 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005697 /* there is no point trying this regex on headers */
5698 return 1;
5699 }
5700 }
5701 *cur_end = term; /* restore the string terminator */
5702 return done;
5703}
Willy Tarreau97de6242006-12-27 17:18:38 +01005704
Willy Tarreau58f10d72006-12-04 02:26:12 +01005705
Willy Tarreau58f10d72006-12-04 02:26:12 +01005706
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005708 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005709 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005710 * unparsable request. Since it can manage the switch to another backend, it
5711 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005713int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005714{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005715 struct http_txn *txn = &s->txn;
5716 struct hdr_exp *exp;
5717
5718 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005719 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005720
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005721 /*
5722 * The interleaving of transformations and verdicts
5723 * makes it difficult to decide to continue or stop
5724 * the evaluation.
5725 */
5726
Willy Tarreau6c123b12010-01-28 20:22:06 +01005727 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5728 break;
5729
Willy Tarreau3d300592007-03-18 18:34:41 +01005730 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005731 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005732 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005733 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005734
5735 /* if this filter had a condition, evaluate it now and skip to
5736 * next filter if the condition does not match.
5737 */
5738 if (exp->cond) {
5739 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5740 ret = acl_pass(ret);
5741 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5742 ret = !ret;
5743
5744 if (!ret)
5745 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005746 }
5747
5748 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005749 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005750 if (unlikely(ret < 0))
5751 return -1;
5752
5753 if (likely(ret == 0)) {
5754 /* The filter did not match the request, it can be
5755 * iterated through all headers.
5756 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005757 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005758 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005759 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005760 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005761}
5762
5763
Willy Tarreaua15645d2007-03-18 16:22:39 +01005764
Willy Tarreau58f10d72006-12-04 02:26:12 +01005765/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005766 * Try to retrieve the server associated to the appsession.
5767 * If the server is found, it's assigned to the session.
5768 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005769void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005770 struct http_txn *txn = &t->txn;
5771 appsess *asession = NULL;
5772 char *sessid_temp = NULL;
5773
Cyril Bontéb21570a2009-11-29 20:04:48 +01005774 if (len > t->be->appsession_len) {
5775 len = t->be->appsession_len;
5776 }
5777
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005778 if (t->be->options2 & PR_O2_AS_REQL) {
5779 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005780 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005781 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005782 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005783 }
5784
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005785 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005786 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
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005791 memcpy(txn->sessid, buf, len);
5792 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005793 }
5794
5795 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5796 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5797 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5798 return;
5799 }
5800
Cyril Bontéb21570a2009-11-29 20:04:48 +01005801 memcpy(sessid_temp, buf, len);
5802 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005803
5804 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5805 /* free previously allocated memory */
5806 pool_free2(apools.sessid, sessid_temp);
5807
5808 if (asession != NULL) {
5809 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5810 if (!(t->be->options2 & PR_O2_AS_REQL))
5811 asession->request_count++;
5812
5813 if (asession->serverid != NULL) {
5814 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005815
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005816 while (srv) {
5817 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005818 if ((srv->state & SRV_RUNNING) ||
5819 (t->be->options & PR_O_PERSIST) ||
5820 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005821 /* we found the server and it's usable */
5822 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005823 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005824 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005825 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005826
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005827 break;
5828 } else {
5829 txn->flags &= ~TX_CK_MASK;
5830 txn->flags |= TX_CK_DOWN;
5831 }
5832 }
5833 srv = srv->next;
5834 }
5835 }
5836 }
5837}
5838
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005839/* Find the end of a cookie value contained between <s> and <e>. It works the
5840 * same way as with headers above except that the semi-colon also ends a token.
5841 * See RFC2965 for more information. Note that it requires a valid header to
5842 * return a valid result.
5843 */
5844char *find_cookie_value_end(char *s, const char *e)
5845{
5846 int quoted, qdpair;
5847
5848 quoted = qdpair = 0;
5849 for (; s < e; s++) {
5850 if (qdpair) qdpair = 0;
5851 else if (quoted) {
5852 if (*s == '\\') qdpair = 1;
5853 else if (*s == '"') quoted = 0;
5854 }
5855 else if (*s == '"') quoted = 1;
5856 else if (*s == ',' || *s == ';') return s;
5857 }
5858 return s;
5859}
5860
5861/* Delete a value in a header between delimiters <from> and <next> in buffer
5862 * <buf>. The number of characters displaced is returned, and the pointer to
5863 * the first delimiter is updated if required. The function tries as much as
5864 * possible to respect the following principles :
5865 * - replace <from> delimiter by the <next> one unless <from> points to a
5866 * colon, in which case <next> is simply removed
5867 * - set exactly one space character after the new first delimiter, unless
5868 * there are not enough characters in the block being moved to do so.
5869 * - remove unneeded spaces before the previous delimiter and after the new
5870 * one.
5871 *
5872 * It is the caller's responsibility to ensure that :
5873 * - <from> points to a valid delimiter or the colon ;
5874 * - <next> points to a valid delimiter or the final CR/LF ;
5875 * - there are non-space chars before <from> ;
5876 * - there is a CR/LF at or after <next>.
5877 */
5878int del_hdr_value(struct buffer *buf, char **from, char *next)
5879{
5880 char *prev = *from;
5881
5882 if (*prev == ':') {
5883 /* We're removing the first value, preserve the colon and add a
5884 * space if possible.
5885 */
5886 if (!http_is_crlf[(unsigned char)*next])
5887 next++;
5888 prev++;
5889 if (prev < next)
5890 *prev++ = ' ';
5891
5892 while (http_is_spht[(unsigned char)*next])
5893 next++;
5894 } else {
5895 /* Remove useless spaces before the old delimiter. */
5896 while (http_is_spht[(unsigned char)*(prev-1)])
5897 prev--;
5898 *from = prev;
5899
5900 /* copy the delimiter and if possible a space if we're
5901 * not at the end of the line.
5902 */
5903 if (!http_is_crlf[(unsigned char)*next]) {
5904 *prev++ = *next++;
5905 if (prev + 1 < next)
5906 *prev++ = ' ';
5907 while (http_is_spht[(unsigned char)*next])
5908 next++;
5909 }
5910 }
5911 return buffer_replace2(buf, prev, next, NULL, 0);
5912}
5913
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005914/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005915 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005916 * desirable to call it only when needed. This code is quite complex because
5917 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5918 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005919 */
5920void manage_client_side_cookies(struct session *t, struct buffer *req)
5921{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005922 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005923 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005924 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005925 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5926 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005927
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005928 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005929 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01005930 hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005931
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005932 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005934 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005935
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005936 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005937 hdr_beg = hdr_next;
5938 hdr_end = hdr_beg + cur_hdr->len;
5939 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005940
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005941 /* We have one full header between hdr_beg and hdr_end, and the
5942 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005943 * "Cookie:" headers.
5944 */
5945
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005946 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005947 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005948 old_idx = cur_idx;
5949 continue;
5950 }
5951
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005952 del_from = NULL; /* nothing to be deleted */
5953 preserve_hdr = 0; /* assume we may kill the whole header */
5954
Willy Tarreau58f10d72006-12-04 02:26:12 +01005955 /* Now look for cookies. Conforming to RFC2109, we have to support
5956 * attributes whose name begin with a '$', and associate them with
5957 * the right cookie, if we want to delete this cookie.
5958 * So there are 3 cases for each cookie read :
5959 * 1) it's a special attribute, beginning with a '$' : ignore it.
5960 * 2) it's a server id cookie that we *MAY* want to delete : save
5961 * some pointers on it (last semi-colon, beginning of cookie...)
5962 * 3) it's an application cookie : we *MAY* have to delete a previous
5963 * "special" cookie.
5964 * At the end of loop, if a "special" cookie remains, we may have to
5965 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005966 * *MUST* delete it.
5967 *
5968 * Note: RFC2965 is unclear about the processing of spaces around
5969 * the equal sign in the ATTR=VALUE form. A careful inspection of
5970 * the RFC explicitly allows spaces before it, and not within the
5971 * tokens (attrs or values). An inspection of RFC2109 allows that
5972 * too but section 10.1.3 lets one think that spaces may be allowed
5973 * after the equal sign too, resulting in some (rare) buggy
5974 * implementations trying to do that. So let's do what servers do.
5975 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5976 * allowed quoted strings in values, with any possible character
5977 * after a backslash, including control chars and delimitors, which
5978 * causes parsing to become ambiguous. Browsers also allow spaces
5979 * within values even without quotes.
5980 *
5981 * We have to keep multiple pointers in order to support cookie
5982 * removal at the beginning, middle or end of header without
5983 * corrupting the header. All of these headers are valid :
5984 *
5985 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5986 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5987 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5988 * | | | | | | | | |
5989 * | | | | | | | | hdr_end <--+
5990 * | | | | | | | +--> next
5991 * | | | | | | +----> val_end
5992 * | | | | | +-----------> val_beg
5993 * | | | | +--------------> equal
5994 * | | | +----------------> att_end
5995 * | | +---------------------> att_beg
5996 * | +--------------------------> prev
5997 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005998 */
5999
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006000 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6001 /* Iterate through all cookies on this line */
6002
6003 /* find att_beg */
6004 att_beg = prev + 1;
6005 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6006 att_beg++;
6007
6008 /* find att_end : this is the first character after the last non
6009 * space before the equal. It may be equal to hdr_end.
6010 */
6011 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006012
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006013 while (equal < hdr_end) {
6014 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006015 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006016 if (http_is_spht[(unsigned char)*equal++])
6017 continue;
6018 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006019 }
6020
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006021 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6022 * is between <att_beg> and <equal>, both may be identical.
6023 */
6024
6025 /* look for end of cookie if there is an equal sign */
6026 if (equal < hdr_end && *equal == '=') {
6027 /* look for the beginning of the value */
6028 val_beg = equal + 1;
6029 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6030 val_beg++;
6031
6032 /* find the end of the value, respecting quotes */
6033 next = find_cookie_value_end(val_beg, hdr_end);
6034
6035 /* make val_end point to the first white space or delimitor after the value */
6036 val_end = next;
6037 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6038 val_end--;
6039 } else {
6040 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006041 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006042
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006043 /* We have nothing to do with attributes beginning with '$'. However,
6044 * they will automatically be removed if a header before them is removed,
6045 * since they're supposed to be linked together.
6046 */
6047 if (*att_beg == '$')
6048 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006049
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006050 /* Ignore cookies with no equal sign */
6051 if (equal == next) {
6052 /* This is not our cookie, so we must preserve it. But if we already
6053 * scheduled another cookie for removal, we cannot remove the
6054 * complete header, but we can remove the previous block itself.
6055 */
6056 preserve_hdr = 1;
6057 if (del_from != NULL) {
6058 int delta = del_hdr_value(req, &del_from, prev);
6059 val_end += delta;
6060 next += delta;
6061 hdr_end += delta;
6062 hdr_next += delta;
6063 cur_hdr->len += delta;
6064 http_msg_move_end(&txn->req, delta);
6065 prev = del_from;
6066 del_from = NULL;
6067 }
6068 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006069 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006070
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006071 /* if there are spaces around the equal sign, we need to
6072 * strip them otherwise we'll get trouble for cookie captures,
6073 * or even for rewrites. Since this happens extremely rarely,
6074 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006075 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006076 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6077 int stripped_before = 0;
6078 int stripped_after = 0;
6079
6080 if (att_end != equal) {
6081 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6082 equal += stripped_before;
6083 val_beg += stripped_before;
6084 }
6085
6086 if (val_beg > equal + 1) {
6087 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6088 val_beg += stripped_after;
6089 stripped_before += stripped_after;
6090 }
6091
6092 val_end += stripped_before;
6093 next += stripped_before;
6094 hdr_end += stripped_before;
6095 hdr_next += stripped_before;
6096 cur_hdr->len += stripped_before;
6097 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006098 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006099 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006100
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006101 /* First, let's see if we want to capture this cookie. We check
6102 * that we don't already have a client side cookie, because we
6103 * can only capture one. Also as an optimisation, we ignore
6104 * cookies shorter than the declared name.
6105 */
6106 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6107 (val_end - att_beg >= t->fe->capture_namelen) &&
6108 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6109 int log_len = val_end - att_beg;
6110
6111 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6112 Alert("HTTP logging : out of memory.\n");
6113 } else {
6114 if (log_len > t->fe->capture_len)
6115 log_len = t->fe->capture_len;
6116 memcpy(txn->cli_cookie, att_beg, log_len);
6117 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006118 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006119 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006120
Willy Tarreaubca99692010-10-06 19:25:55 +02006121 /* Persistence cookies in passive, rewrite or insert mode have the
6122 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006123 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006124 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006125 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006126 * For cookies in prefix mode, the form is :
6127 *
6128 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006129 */
6130 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6131 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6132 struct server *srv = t->be->srv;
6133 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006134
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006135 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6136 * have the server ID between val_beg and delim, and the original cookie between
6137 * delim+1 and val_end. Otherwise, delim==val_end :
6138 *
6139 * Cookie: NAME=SRV; # in all but prefix modes
6140 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6141 * | || || | |+-> next
6142 * | || || | +--> val_end
6143 * | || || +---------> delim
6144 * | || |+------------> val_beg
6145 * | || +-------------> att_end = equal
6146 * | |+-----------------> att_beg
6147 * | +------------------> prev
6148 * +-------------------------> hdr_beg
6149 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006150
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006151 if (t->be->options & PR_O_COOK_PFX) {
6152 for (delim = val_beg; delim < val_end; delim++)
6153 if (*delim == COOKIE_DELIM)
6154 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006155 } else {
6156 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006157 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006158 /* Now check if the cookie contains a date field, which would
6159 * appear after a vertical bar ('|') just after the server name
6160 * and before the delimiter.
6161 */
6162 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6163 if (vbar1) {
6164 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006165 * right is the last seen date. It is a base64 encoded
6166 * 30-bit value representing the UNIX date since the
6167 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006168 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006169 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006170 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006171 if (val_end - vbar1 >= 5) {
6172 val = b64tos30(vbar1);
6173 if (val > 0)
6174 txn->cookie_last_date = val << 2;
6175 }
6176 /* look for a second vertical bar */
6177 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6178 if (vbar1 && (val_end - vbar1 > 5)) {
6179 val = b64tos30(vbar1 + 1);
6180 if (val > 0)
6181 txn->cookie_first_date = val << 2;
6182 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006183 }
6184 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006185
Willy Tarreauf64d1412010-10-07 20:06:11 +02006186 /* if the cookie has an expiration date and the proxy wants to check
6187 * it, then we do that now. We first check if the cookie is too old,
6188 * then only if it has expired. We detect strict overflow because the
6189 * time resolution here is not great (4 seconds). Cookies with dates
6190 * in the future are ignored if their offset is beyond one day. This
6191 * allows an admin to fix timezone issues without expiring everyone
6192 * and at the same time avoids keeping unwanted side effects for too
6193 * long.
6194 */
6195 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006196 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6197 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006198 txn->flags &= ~TX_CK_MASK;
6199 txn->flags |= TX_CK_OLD;
6200 delim = val_beg; // let's pretend we have not found the cookie
6201 txn->cookie_first_date = 0;
6202 txn->cookie_last_date = 0;
6203 }
6204 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006205 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6206 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006207 txn->flags &= ~TX_CK_MASK;
6208 txn->flags |= TX_CK_EXPIRED;
6209 delim = val_beg; // let's pretend we have not found the cookie
6210 txn->cookie_first_date = 0;
6211 txn->cookie_last_date = 0;
6212 }
6213
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006214 /* Here, we'll look for the first running server which supports the cookie.
6215 * This allows to share a same cookie between several servers, for example
6216 * to dedicate backup servers to specific servers only.
6217 * However, to prevent clients from sticking to cookie-less backup server
6218 * when they have incidentely learned an empty cookie, we simply ignore
6219 * empty cookies and mark them as invalid.
6220 * The same behaviour is applied when persistence must be ignored.
6221 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006222 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006223 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006224
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 while (srv) {
6226 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6227 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6228 if ((srv->state & SRV_RUNNING) ||
6229 (t->be->options & PR_O_PERSIST) ||
6230 (t->flags & SN_FORCE_PRST)) {
6231 /* we found the server and we can use it */
6232 txn->flags &= ~TX_CK_MASK;
6233 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6234 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006235 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006236 break;
6237 } else {
6238 /* we found a server, but it's down,
6239 * mark it as such and go on in case
6240 * another one is available.
6241 */
6242 txn->flags &= ~TX_CK_MASK;
6243 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006244 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006245 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 srv = srv->next;
6247 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006248
Willy Tarreauf64d1412010-10-07 20:06:11 +02006249 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006250 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006252 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6253 txn->flags |= TX_CK_UNUSED;
6254 else
6255 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006257
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 /* depending on the cookie mode, we may have to either :
6259 * - delete the complete cookie if we're in insert+indirect mode, so that
6260 * the server never sees it ;
6261 * - remove the server id from the cookie value, and tag the cookie as an
6262 * application cookie so that it does not get accidentely removed later,
6263 * if we're in cookie prefix mode
6264 */
6265 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6266 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006267
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6269 val_end += delta;
6270 next += delta;
6271 hdr_end += delta;
6272 hdr_next += delta;
6273 cur_hdr->len += delta;
6274 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006275
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006276 del_from = NULL;
6277 preserve_hdr = 1; /* we want to keep this cookie */
6278 }
6279 else if (del_from == NULL &&
6280 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6281 del_from = prev;
6282 }
6283 } else {
6284 /* This is not our cookie, so we must preserve it. But if we already
6285 * scheduled another cookie for removal, we cannot remove the
6286 * complete header, but we can remove the previous block itself.
6287 */
6288 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006289
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006290 if (del_from != NULL) {
6291 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006292 if (att_beg >= del_from)
6293 att_beg += delta;
6294 if (att_end >= del_from)
6295 att_end += delta;
6296 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006297 val_end += delta;
6298 next += delta;
6299 hdr_end += delta;
6300 hdr_next += delta;
6301 cur_hdr->len += delta;
6302 http_msg_move_end(&txn->req, delta);
6303 prev = del_from;
6304 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006305 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006306 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006307
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006308 /* Look for the appsession cookie unless persistence must be ignored */
6309 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6310 int cmp_len, value_len;
6311 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006312
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006313 if (t->be->options2 & PR_O2_AS_PFX) {
6314 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6315 value_begin = att_beg + t->be->appsession_name_len;
6316 value_len = val_end - att_beg - t->be->appsession_name_len;
6317 } else {
6318 cmp_len = att_end - att_beg;
6319 value_begin = val_beg;
6320 value_len = val_end - val_beg;
6321 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006322
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 /* let's see if the cookie is our appcookie */
6324 if (cmp_len == t->be->appsession_name_len &&
6325 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6326 manage_client_side_appsession(t, value_begin, value_len);
6327 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006328 }
6329
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006330 /* continue with next cookie on this header line */
6331 att_beg = next;
6332 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006333
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006334 /* There are no more cookies on this line.
6335 * We may still have one (or several) marked for deletion at the
6336 * end of the line. We must do this now in two ways :
6337 * - if some cookies must be preserved, we only delete from the
6338 * mark to the end of line ;
6339 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006341 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006343 if (preserve_hdr) {
6344 delta = del_hdr_value(req, &del_from, hdr_end);
6345 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346 cur_hdr->len += delta;
6347 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006348 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006349
6350 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006351 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6352 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006354 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006355 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006356 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006357 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006358 }
6359
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006360 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006361 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006362 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006363}
6364
6365
Willy Tarreaua15645d2007-03-18 16:22:39 +01006366/* Iterate the same filter through all response headers contained in <rtr>.
6367 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6368 */
6369int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6370{
6371 char term;
6372 char *cur_ptr, *cur_end, *cur_next;
6373 int cur_idx, old_idx, last_hdr;
6374 struct http_txn *txn = &t->txn;
6375 struct hdr_idx_elem *cur_hdr;
6376 int len, delta;
6377
6378 last_hdr = 0;
6379
Willy Tarreau3a215be2012-03-09 21:39:51 +01006380 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006381 old_idx = 0;
6382
6383 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006384 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006385 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006386 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006387 (exp->action == ACT_ALLOW ||
6388 exp->action == ACT_DENY))
6389 return 0;
6390
6391 cur_idx = txn->hdr_idx.v[old_idx].next;
6392 if (!cur_idx)
6393 break;
6394
6395 cur_hdr = &txn->hdr_idx.v[cur_idx];
6396 cur_ptr = cur_next;
6397 cur_end = cur_ptr + cur_hdr->len;
6398 cur_next = cur_end + cur_hdr->cr + 1;
6399
6400 /* Now we have one header between cur_ptr and cur_end,
6401 * and the next header starts at cur_next.
6402 */
6403
6404 /* The annoying part is that pattern matching needs
6405 * that we modify the contents to null-terminate all
6406 * strings before testing them.
6407 */
6408
6409 term = *cur_end;
6410 *cur_end = '\0';
6411
6412 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6413 switch (exp->action) {
6414 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006415 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006416 last_hdr = 1;
6417 break;
6418
6419 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006420 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006421 last_hdr = 1;
6422 break;
6423
6424 case ACT_REPLACE:
6425 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6426 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6427 /* FIXME: if the user adds a newline in the replacement, the
6428 * index will not be recalculated for now, and the new line
6429 * will not be counted as a new header.
6430 */
6431
6432 cur_end += delta;
6433 cur_next += delta;
6434 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006435 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006436 break;
6437
6438 case ACT_REMOVE:
6439 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6440 cur_next += delta;
6441
Willy Tarreaufa355d42009-11-29 18:12:29 +01006442 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006443 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6444 txn->hdr_idx.used--;
6445 cur_hdr->len = 0;
6446 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006447 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006448 break;
6449
6450 }
6451 }
6452 if (cur_end)
6453 *cur_end = term; /* restore the string terminator */
6454
6455 /* keep the link from this header to next one in case of later
6456 * removal of next header.
6457 */
6458 old_idx = cur_idx;
6459 }
6460 return 0;
6461}
6462
6463
6464/* Apply the filter to the status line in the response buffer <rtr>.
6465 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6466 * or -1 if a replacement resulted in an invalid status line.
6467 */
6468int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6469{
6470 char term;
6471 char *cur_ptr, *cur_end;
6472 int done;
6473 struct http_txn *txn = &t->txn;
6474 int len, delta;
6475
6476
Willy Tarreau3d300592007-03-18 18:34:41 +01006477 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006478 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006479 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006480 (exp->action == ACT_ALLOW ||
6481 exp->action == ACT_DENY))
6482 return 0;
6483 else if (exp->action == ACT_REMOVE)
6484 return 0;
6485
6486 done = 0;
6487
Willy Tarreau3a215be2012-03-09 21:39:51 +01006488 cur_ptr = rtr->p + txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006489 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006490
6491 /* Now we have the status line between cur_ptr and cur_end */
6492
6493 /* The annoying part is that pattern matching needs
6494 * that we modify the contents to null-terminate all
6495 * strings before testing them.
6496 */
6497
6498 term = *cur_end;
6499 *cur_end = '\0';
6500
6501 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6502 switch (exp->action) {
6503 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006504 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006505 done = 1;
6506 break;
6507
6508 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006509 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006510 done = 1;
6511 break;
6512
6513 case ACT_REPLACE:
6514 *cur_end = term; /* restore the string terminator */
6515 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6516 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6517 /* FIXME: if the user adds a newline in the replacement, the
6518 * index will not be recalculated for now, and the new line
6519 * will not be counted as a new header.
6520 */
6521
Willy Tarreaufa355d42009-11-29 18:12:29 +01006522 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006523 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01006524 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006525 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006526 cur_ptr, cur_end + 1,
6527 NULL, NULL);
6528 if (unlikely(!cur_end))
6529 return -1;
6530
6531 /* we have a full respnse and we know that we have either a CR
6532 * or an LF at <ptr>.
6533 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01006534 txn->status = strl2ui(rtr->p + txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006535 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006536 /* there is no point trying this regex on headers */
6537 return 1;
6538 }
6539 }
6540 *cur_end = term; /* restore the string terminator */
6541 return done;
6542}
6543
6544
6545
6546/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006547 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006548 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6549 * unparsable response.
6550 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006551int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006552{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006553 struct http_txn *txn = &s->txn;
6554 struct hdr_exp *exp;
6555
6556 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006557 int ret;
6558
6559 /*
6560 * The interleaving of transformations and verdicts
6561 * makes it difficult to decide to continue or stop
6562 * the evaluation.
6563 */
6564
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006565 if (txn->flags & TX_SVDENY)
6566 break;
6567
Willy Tarreau3d300592007-03-18 18:34:41 +01006568 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006569 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6570 exp->action == ACT_PASS)) {
6571 exp = exp->next;
6572 continue;
6573 }
6574
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006575 /* if this filter had a condition, evaluate it now and skip to
6576 * next filter if the condition does not match.
6577 */
6578 if (exp->cond) {
6579 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6580 ret = acl_pass(ret);
6581 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6582 ret = !ret;
6583 if (!ret)
6584 continue;
6585 }
6586
Willy Tarreaua15645d2007-03-18 16:22:39 +01006587 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006588 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006589 if (unlikely(ret < 0))
6590 return -1;
6591
6592 if (likely(ret == 0)) {
6593 /* The filter did not match the response, it can be
6594 * iterated through all headers.
6595 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006596 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006597 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598 }
6599 return 0;
6600}
6601
6602
Willy Tarreaua15645d2007-03-18 16:22:39 +01006603/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006604 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006605 * desirable to call it only when needed. This function is also used when we
6606 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006608void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006609{
6610 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006611 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006612 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006613 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006614 char *hdr_beg, *hdr_end, *hdr_next;
6615 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006616
Willy Tarreaua15645d2007-03-18 16:22:39 +01006617 /* Iterate through the headers.
6618 * we start with the start line.
6619 */
6620 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006621 hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006622
6623 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6624 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006625 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006626
6627 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006628 hdr_beg = hdr_next;
6629 hdr_end = hdr_beg + cur_hdr->len;
6630 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006631
Willy Tarreau24581ba2010-08-31 22:39:35 +02006632 /* We have one full header between hdr_beg and hdr_end, and the
6633 * next header starts at hdr_next. We're only interested in
6634 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006635 */
6636
Willy Tarreau24581ba2010-08-31 22:39:35 +02006637 is_cookie2 = 0;
6638 prev = hdr_beg + 10;
6639 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006640 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006641 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6642 if (!val) {
6643 old_idx = cur_idx;
6644 continue;
6645 }
6646 is_cookie2 = 1;
6647 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006648 }
6649
Willy Tarreau24581ba2010-08-31 22:39:35 +02006650 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6651 * <prev> points to the colon.
6652 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006653 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006654
Willy Tarreau24581ba2010-08-31 22:39:35 +02006655 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6656 * check-cache is enabled) and we are not interested in checking
6657 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006658 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006659 if (t->be->cookie_name == NULL &&
6660 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006661 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006662 return;
6663
Willy Tarreau24581ba2010-08-31 22:39:35 +02006664 /* OK so now we know we have to process this response cookie.
6665 * The format of the Set-Cookie header is slightly different
6666 * from the format of the Cookie header in that it does not
6667 * support the comma as a cookie delimiter (thus the header
6668 * cannot be folded) because the Expires attribute described in
6669 * the original Netscape's spec may contain an unquoted date
6670 * with a comma inside. We have to live with this because
6671 * many browsers don't support Max-Age and some browsers don't
6672 * support quoted strings. However the Set-Cookie2 header is
6673 * clean.
6674 *
6675 * We have to keep multiple pointers in order to support cookie
6676 * removal at the beginning, middle or end of header without
6677 * corrupting the header (in case of set-cookie2). A special
6678 * pointer, <scav> points to the beginning of the set-cookie-av
6679 * fields after the first semi-colon. The <next> pointer points
6680 * either to the end of line (set-cookie) or next unquoted comma
6681 * (set-cookie2). All of these headers are valid :
6682 *
6683 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6684 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6685 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6686 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6687 * | | | | | | | | | |
6688 * | | | | | | | | +-> next hdr_end <--+
6689 * | | | | | | | +------------> scav
6690 * | | | | | | +--------------> val_end
6691 * | | | | | +--------------------> val_beg
6692 * | | | | +----------------------> equal
6693 * | | | +------------------------> att_end
6694 * | | +----------------------------> att_beg
6695 * | +------------------------------> prev
6696 * +-----------------------------------------> hdr_beg
6697 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006698
Willy Tarreau24581ba2010-08-31 22:39:35 +02006699 for (; prev < hdr_end; prev = next) {
6700 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701
Willy Tarreau24581ba2010-08-31 22:39:35 +02006702 /* find att_beg */
6703 att_beg = prev + 1;
6704 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6705 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706
Willy Tarreau24581ba2010-08-31 22:39:35 +02006707 /* find att_end : this is the first character after the last non
6708 * space before the equal. It may be equal to hdr_end.
6709 */
6710 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006711
Willy Tarreau24581ba2010-08-31 22:39:35 +02006712 while (equal < hdr_end) {
6713 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6714 break;
6715 if (http_is_spht[(unsigned char)*equal++])
6716 continue;
6717 att_end = equal;
6718 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719
Willy Tarreau24581ba2010-08-31 22:39:35 +02006720 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6721 * is between <att_beg> and <equal>, both may be identical.
6722 */
6723
6724 /* look for end of cookie if there is an equal sign */
6725 if (equal < hdr_end && *equal == '=') {
6726 /* look for the beginning of the value */
6727 val_beg = equal + 1;
6728 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6729 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006730
Willy Tarreau24581ba2010-08-31 22:39:35 +02006731 /* find the end of the value, respecting quotes */
6732 next = find_cookie_value_end(val_beg, hdr_end);
6733
6734 /* make val_end point to the first white space or delimitor after the value */
6735 val_end = next;
6736 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6737 val_end--;
6738 } else {
6739 /* <equal> points to next comma, semi-colon or EOL */
6740 val_beg = val_end = next = equal;
6741 }
6742
6743 if (next < hdr_end) {
6744 /* Set-Cookie2 supports multiple cookies, and <next> points to
6745 * a colon or semi-colon before the end. So skip all attr-value
6746 * pairs and look for the next comma. For Set-Cookie, since
6747 * commas are permitted in values, skip to the end.
6748 */
6749 if (is_cookie2)
6750 next = find_hdr_value_end(next, hdr_end);
6751 else
6752 next = hdr_end;
6753 }
6754
6755 /* Now everything is as on the diagram above */
6756
6757 /* Ignore cookies with no equal sign */
6758 if (equal == val_end)
6759 continue;
6760
6761 /* If there are spaces around the equal sign, we need to
6762 * strip them otherwise we'll get trouble for cookie captures,
6763 * or even for rewrites. Since this happens extremely rarely,
6764 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006765 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006766 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6767 int stripped_before = 0;
6768 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769
Willy Tarreau24581ba2010-08-31 22:39:35 +02006770 if (att_end != equal) {
6771 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6772 equal += stripped_before;
6773 val_beg += stripped_before;
6774 }
6775
6776 if (val_beg > equal + 1) {
6777 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6778 val_beg += stripped_after;
6779 stripped_before += stripped_after;
6780 }
6781
6782 val_end += stripped_before;
6783 next += stripped_before;
6784 hdr_end += stripped_before;
6785 hdr_next += stripped_before;
6786 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006787 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006788 }
6789
6790 /* First, let's see if we want to capture this cookie. We check
6791 * that we don't already have a server side cookie, because we
6792 * can only capture one. Also as an optimisation, we ignore
6793 * cookies shorter than the declared name.
6794 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006795 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006796 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006797 (val_end - att_beg >= t->fe->capture_namelen) &&
6798 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6799 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006800 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801 Alert("HTTP logging : out of memory.\n");
6802 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006803 else {
6804 if (log_len > t->fe->capture_len)
6805 log_len = t->fe->capture_len;
6806 memcpy(txn->srv_cookie, att_beg, log_len);
6807 txn->srv_cookie[log_len] = 0;
6808 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809 }
6810
Willy Tarreau827aee92011-03-10 16:55:02 +01006811 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006812 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006813 if (!(t->flags & SN_IGNORE_PRST) &&
6814 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6815 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006816 /* assume passive cookie by default */
6817 txn->flags &= ~TX_SCK_MASK;
6818 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006819
6820 /* If the cookie is in insert mode on a known server, we'll delete
6821 * this occurrence because we'll insert another one later.
6822 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006823 * a direct access.
6824 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006825 if (t->be->options2 & PR_O2_COOK_PSV) {
6826 /* The "preserve" flag was set, we don't want to touch the
6827 * server's cookie.
6828 */
6829 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006830 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006831 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006832 /* this cookie must be deleted */
6833 if (*prev == ':' && next == hdr_end) {
6834 /* whole header */
6835 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6836 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6837 txn->hdr_idx.used--;
6838 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006839 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006840 hdr_next += delta;
6841 http_msg_move_end(&txn->rsp, delta);
6842 /* note: while both invalid now, <next> and <hdr_end>
6843 * are still equal, so the for() will stop as expected.
6844 */
6845 } else {
6846 /* just remove the value */
6847 int delta = del_hdr_value(res, &prev, next);
6848 next = prev;
6849 hdr_end += delta;
6850 hdr_next += delta;
6851 cur_hdr->len += delta;
6852 http_msg_move_end(&txn->rsp, delta);
6853 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006854 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006855 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006856 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006857 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006858 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006859 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860 * with this server since we know it.
6861 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006862 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006863 next += delta;
6864 hdr_end += delta;
6865 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006866 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006867 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868
Willy Tarreauf1348312010-10-07 15:54:11 +02006869 txn->flags &= ~TX_SCK_MASK;
6870 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006871 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006872 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006874 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006876 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006877 next += delta;
6878 hdr_end += delta;
6879 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006881 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006882
Willy Tarreau827aee92011-03-10 16:55:02 +01006883 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006884 txn->flags &= ~TX_SCK_MASK;
6885 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886 }
6887 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006888 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6889 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006890 int cmp_len, value_len;
6891 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892
Cyril Bontéb21570a2009-11-29 20:04:48 +01006893 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006894 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6895 value_begin = att_beg + t->be->appsession_name_len;
6896 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006897 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006898 cmp_len = att_end - att_beg;
6899 value_begin = val_beg;
6900 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006901 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006902
Cyril Bonté17530c32010-04-06 21:11:10 +02006903 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006904 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6905 /* free a possibly previously allocated memory */
6906 pool_free2(apools.sessid, txn->sessid);
6907
Cyril Bontéb21570a2009-11-29 20:04:48 +01006908 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006909 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006910 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6911 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6912 return;
6913 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006914 memcpy(txn->sessid, value_begin, value_len);
6915 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006916 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006917 }
6918 /* that's done for this cookie, check the next one on the same
6919 * line when next != hdr_end (only if is_cookie2).
6920 */
6921 }
6922 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006923 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006924 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006925
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006926 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006927 appsess *asession = NULL;
6928 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006929 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006930 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006931 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006932 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6933 Alert("Not enough Memory process_srv():asession:calloc().\n");
6934 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6935 return;
6936 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006937 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6938
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006939 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6940 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6941 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006942 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006943 return;
6944 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006945 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006946 asession->sessid[t->be->appsession_len] = 0;
6947
Willy Tarreau827aee92011-03-10 16:55:02 +01006948 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006949 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006950 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006951 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006952 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006953 return;
6954 }
6955 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006956 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006957
6958 asession->request_count = 0;
6959 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6960 }
6961
6962 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6963 asession->request_count++;
6964 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006965}
6966
6967
Willy Tarreaua15645d2007-03-18 16:22:39 +01006968/*
6969 * Check if response is cacheable or not. Updates t->flags.
6970 */
6971void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6972{
6973 struct http_txn *txn = &t->txn;
6974 char *p1, *p2;
6975
6976 char *cur_ptr, *cur_end, *cur_next;
6977 int cur_idx;
6978
Willy Tarreau5df51872007-11-25 16:20:08 +01006979 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980 return;
6981
6982 /* Iterate through the headers.
6983 * we start with the start line.
6984 */
6985 cur_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006986 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006987
6988 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6989 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006990 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006991
6992 cur_hdr = &txn->hdr_idx.v[cur_idx];
6993 cur_ptr = cur_next;
6994 cur_end = cur_ptr + cur_hdr->len;
6995 cur_next = cur_end + cur_hdr->cr + 1;
6996
6997 /* We have one full header between cur_ptr and cur_end, and the
6998 * next header starts at cur_next. We're only interested in
6999 * "Cookie:" headers.
7000 */
7001
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007002 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7003 if (val) {
7004 if ((cur_end - (cur_ptr + val) >= 8) &&
7005 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7006 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7007 return;
7008 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007009 }
7010
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007011 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7012 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007013 continue;
7014
7015 /* OK, right now we know we have a cache-control header at cur_ptr */
7016
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007017 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007018
7019 if (p1 >= cur_end) /* no more info */
7020 continue;
7021
7022 /* p1 is at the beginning of the value */
7023 p2 = p1;
7024
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007025 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007026 p2++;
7027
7028 /* we have a complete value between p1 and p2 */
7029 if (p2 < cur_end && *p2 == '=') {
7030 /* we have something of the form no-cache="set-cookie" */
7031 if ((cur_end - p1 >= 21) &&
7032 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7033 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007034 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007035 continue;
7036 }
7037
7038 /* OK, so we know that either p2 points to the end of string or to a comma */
7039 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7040 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7041 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7042 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007043 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007044 return;
7045 }
7046
7047 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007048 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007049 continue;
7050 }
7051 }
7052}
7053
7054
Willy Tarreau58f10d72006-12-04 02:26:12 +01007055/*
7056 * Try to retrieve a known appsession in the URI, then the associated server.
7057 * If the server is found, it's assigned to the session.
7058 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007059void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007060{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007061 char *end_params, *first_param, *cur_param, *next_param;
7062 char separator;
7063 int value_len;
7064
7065 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007066
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007067 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007068 (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 +01007069 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007070 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007071
Cyril Bontéb21570a2009-11-29 20:04:48 +01007072 first_param = NULL;
7073 switch (mode) {
7074 case PR_O2_AS_M_PP:
7075 first_param = memchr(begin, ';', len);
7076 break;
7077 case PR_O2_AS_M_QS:
7078 first_param = memchr(begin, '?', len);
7079 break;
7080 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081
Cyril Bontéb21570a2009-11-29 20:04:48 +01007082 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007084 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007085
Cyril Bontéb21570a2009-11-29 20:04:48 +01007086 switch (mode) {
7087 case PR_O2_AS_M_PP:
7088 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7089 end_params = (char *) begin + len;
7090 }
7091 separator = ';';
7092 break;
7093 case PR_O2_AS_M_QS:
7094 end_params = (char *) begin + len;
7095 separator = '&';
7096 break;
7097 default:
7098 /* unknown mode, shouldn't happen */
7099 return;
7100 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007101
Cyril Bontéb21570a2009-11-29 20:04:48 +01007102 cur_param = next_param = end_params;
7103 while (cur_param > first_param) {
7104 cur_param--;
7105 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7106 /* let's see if this is the appsession parameter */
7107 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7108 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7109 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7110 /* Cool... it's the right one */
7111 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7112 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7113 if (value_len > 0) {
7114 manage_client_side_appsession(t, cur_param, value_len);
7115 }
7116 break;
7117 }
7118 next_param = cur_param;
7119 }
7120 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007121#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007122 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007123 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007124#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007125}
7126
Willy Tarreaub2513902006-12-17 14:52:38 +01007127/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007128 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007129 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007130 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007131 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007132 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007133 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007134 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007135 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007136int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007137{
7138 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007139 struct http_msg *msg = &txn->req;
7140 const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u;
7141 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007142
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007143 if (!uri_auth)
7144 return 0;
7145
Cyril Bonté70be45d2010-10-12 00:14:35 +02007146 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007147 return 0;
7148
Willy Tarreau295a8372011-03-10 11:25:07 +01007149 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007150 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007151
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007152 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007153 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007154 return 0;
7155
Willy Tarreau3a215be2012-03-09 21:39:51 +01007156 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007157 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007158 return 0;
7159
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007160 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007161 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007162 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007163 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007164 break;
7165 }
7166 h++;
7167 }
7168
7169 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007170 h = uri + uri_auth->uri_len;
7171 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007172 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007173 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007174 break;
7175 }
7176 h++;
7177 }
7178 }
7179
Willy Tarreau3a215be2012-03-09 21:39:51 +01007180 h = uri + uri_auth->uri_len;
7181 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007182 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007183 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007184 break;
7185 }
7186 h++;
7187 }
7188
Willy Tarreau3a215be2012-03-09 21:39:51 +01007189 h = uri + uri_auth->uri_len;
7190 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007191 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007192 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007193 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007194 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7195 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7196 si->applet.ctx.stats.st_code = i;
7197 break;
7198 }
7199 }
7200 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007201 break;
7202 }
7203 h++;
7204 }
7205
Willy Tarreau295a8372011-03-10 11:25:07 +01007206 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007207
Willy Tarreaub2513902006-12-17 14:52:38 +01007208 return 1;
7209}
7210
Willy Tarreau4076a152009-04-02 15:18:36 +02007211/*
7212 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007213 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007214 * assume that msg->sol = msg->buf->p + msg->som. Also, while HTTP requests
7215 * or response messages cannot wrap, this function may also be used with chunks
7216 * which may wrap.
Willy Tarreau4076a152009-04-02 15:18:36 +02007217 */
7218void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007219 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007220 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007221{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007222 struct buffer *buf = msg->buf;
7223
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02007224 if (bi_end(buf) <= (buf->p + msg->som)) { /* message wraps */
Willy Tarreauea1175a2012-03-05 15:52:30 +01007225 int len1 = buf->size - msg->som - (buf->p - buf->data);
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02007226 es->len = bi_end(buf) - (buf->p + msg->som) + buf->size;
Willy Tarreauea1175a2012-03-05 15:52:30 +01007227 memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007228 if (es->len > len1 && len1 < sizeof(es->buf))
7229 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7230 }
7231 else {
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02007232 es->len = bi_end(buf) - (buf->p + msg->som);
Willy Tarreauea1175a2012-03-05 15:52:30 +01007233 memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007234 }
7235
Willy Tarreau4076a152009-04-02 15:18:36 +02007236 if (msg->err_pos >= 0)
Willy Tarreauea1175a2012-03-05 15:52:30 +01007237 es->pos = msg->err_pos - msg->som - (buf->p - buf->data);
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02007238 else if (b_ptr(buf, msg->next) >= (buf->p + msg->som))
7239 es->pos = b_ptr(buf, msg->next) - (buf->p + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007240 else
Willy Tarreaucc5cfcb2012-05-04 21:35:27 +02007241 es->pos = b_ptr(buf, msg->next) - (buf->p + msg->som) + buf->size;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007242
Willy Tarreau4076a152009-04-02 15:18:36 +02007243 es->when = date; // user-visible date
7244 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007245 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007246 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007247 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007248 es->state = state;
7249 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007250 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007251}
Willy Tarreaub2513902006-12-17 14:52:38 +01007252
Willy Tarreau294c4732011-12-16 21:35:50 +01007253/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7254 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7255 * performed over the whole headers. Otherwise it must contain a valid header
7256 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7257 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7258 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7259 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7260 * -1.
7261 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007262 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007263unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7264 struct hdr_idx *idx, int occ,
7265 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007266{
Willy Tarreau294c4732011-12-16 21:35:50 +01007267 struct hdr_ctx local_ctx;
7268 char *ptr_hist[MAX_HDR_HISTORY];
7269 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007270 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007271 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007272
Willy Tarreau294c4732011-12-16 21:35:50 +01007273 if (!ctx) {
7274 local_ctx.idx = 0;
7275 ctx = &local_ctx;
7276 }
7277
Willy Tarreaubce70882009-09-07 11:51:47 +02007278 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007279 /* search from the beginning */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007280 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007281 occ--;
7282 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007283 *vptr = ctx->line + ctx->val;
7284 *vlen = ctx->vlen;
7285 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007286 }
7287 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007288 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007289 }
7290
7291 /* negative occurrence, we scan all the list then walk back */
7292 if (-occ > MAX_HDR_HISTORY)
7293 return 0;
7294
Willy Tarreau294c4732011-12-16 21:35:50 +01007295 found = hist_ptr = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007296 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007297 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7298 len_hist[hist_ptr] = ctx->vlen;
7299 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007300 hist_ptr = 0;
7301 found++;
7302 }
7303 if (-occ > found)
7304 return 0;
7305 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7306 * find occurrence -occ, so we have to check [hist_ptr+occ].
7307 */
7308 hist_ptr += occ;
7309 if (hist_ptr >= MAX_HDR_HISTORY)
7310 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007311 *vptr = ptr_hist[hist_ptr];
7312 *vlen = len_hist[hist_ptr];
7313 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007314}
7315
Willy Tarreaubaaee002006-06-26 02:48:02 +02007316/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007317 * Print a debug line with a header
7318 */
7319void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7320{
7321 int len, max;
7322 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007323 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007324 max = end - start;
7325 UBOUND(max, sizeof(trash) - len - 1);
7326 len += strlcpy2(trash + len, start, max + 1);
7327 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007328 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007329}
7330
Willy Tarreau0937bc42009-12-22 15:03:09 +01007331/*
7332 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7333 * the required fields are properly allocated and that we only need to (re)init
7334 * them. This should be used before processing any new request.
7335 */
7336void http_init_txn(struct session *s)
7337{
7338 struct http_txn *txn = &s->txn;
7339 struct proxy *fe = s->fe;
7340
7341 txn->flags = 0;
7342 txn->status = -1;
7343
William Lallemand5f232402012-04-05 18:02:55 +02007344 global.req_count++;
7345
Willy Tarreauf64d1412010-10-07 20:06:11 +02007346 txn->cookie_first_date = 0;
7347 txn->cookie_last_date = 0;
7348
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007349 txn->req.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007350 txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007351 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007352 txn->rsp.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007353 txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007354 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007355 txn->req.chunk_len = 0LL;
7356 txn->req.body_len = 0LL;
7357 txn->rsp.chunk_len = 0LL;
7358 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007359 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7360 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007361 txn->req.buf = s->req;
7362 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007363
7364 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007365
7366 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7367 if (fe->options2 & PR_O2_REQBUG_OK)
7368 txn->req.err_pos = -1; /* let buggy requests pass */
7369
Willy Tarreau46023632010-01-07 22:51:47 +01007370 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007371 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7372
Willy Tarreau46023632010-01-07 22:51:47 +01007373 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007374 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7375
7376 if (txn->hdr_idx.v)
7377 hdr_idx_init(&txn->hdr_idx);
7378}
7379
7380/* to be used at the end of a transaction */
7381void http_end_txn(struct session *s)
7382{
7383 struct http_txn *txn = &s->txn;
7384
7385 /* these ones will have been dynamically allocated */
7386 pool_free2(pool2_requri, txn->uri);
7387 pool_free2(pool2_capture, txn->cli_cookie);
7388 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007389 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007390 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007391
William Lallemanda73203e2012-03-12 12:48:57 +01007392 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007393 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007394 txn->uri = NULL;
7395 txn->srv_cookie = NULL;
7396 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007397
7398 if (txn->req.cap) {
7399 struct cap_hdr *h;
7400 for (h = s->fe->req_cap; h; h = h->next)
7401 pool_free2(h->pool, txn->req.cap[h->index]);
7402 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7403 }
7404
7405 if (txn->rsp.cap) {
7406 struct cap_hdr *h;
7407 for (h = s->fe->rsp_cap; h; h = h->next)
7408 pool_free2(h->pool, txn->rsp.cap[h->index]);
7409 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7410 }
7411
Willy Tarreau0937bc42009-12-22 15:03:09 +01007412}
7413
7414/* to be used at the end of a transaction to prepare a new one */
7415void http_reset_txn(struct session *s)
7416{
7417 http_end_txn(s);
7418 http_init_txn(s);
7419
7420 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007421 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007422 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007423 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007424 /* re-init store persistence */
7425 s->store_count = 0;
7426
Willy Tarreau0937bc42009-12-22 15:03:09 +01007427 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007428
7429 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7430
Willy Tarreau739cfba2010-01-25 23:11:14 +01007431 /* We must trim any excess data from the response buffer, because we
7432 * may have blocked an invalid response from a server that we don't
7433 * want to accidentely forward once we disable the analysers, nor do
7434 * we want those data to come along with next response. A typical
7435 * example of such data would be from a buggy server responding to
7436 * a HEAD with some data, or sending more than the advertised
7437 * content-length.
7438 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007439 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007440 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007441
Willy Tarreau0937bc42009-12-22 15:03:09 +01007442 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007443 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007444
Willy Tarreaud04e8582010-05-31 12:31:35 +02007445 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007446 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007447
7448 s->req->rex = TICK_ETERNITY;
7449 s->req->wex = TICK_ETERNITY;
7450 s->req->analyse_exp = TICK_ETERNITY;
7451 s->rep->rex = TICK_ETERNITY;
7452 s->rep->wex = TICK_ETERNITY;
7453 s->rep->analyse_exp = TICK_ETERNITY;
7454}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007455
Willy Tarreauff011f22011-01-06 17:51:27 +01007456void free_http_req_rules(struct list *r) {
7457 struct http_req_rule *tr, *pr;
7458
7459 list_for_each_entry_safe(pr, tr, r, list) {
7460 LIST_DEL(&pr->list);
7461 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7462 free(pr->http_auth.realm);
7463
7464 free(pr);
7465 }
7466}
7467
7468struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7469{
7470 struct http_req_rule *rule;
7471 int cur_arg;
7472
7473 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7474 if (!rule) {
7475 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7476 return NULL;
7477 }
7478
7479 if (!*args[0]) {
7480 goto req_error_parsing;
7481 } else if (!strcmp(args[0], "allow")) {
7482 rule->action = HTTP_REQ_ACT_ALLOW;
7483 cur_arg = 1;
7484 } else if (!strcmp(args[0], "deny")) {
7485 rule->action = HTTP_REQ_ACT_DENY;
7486 cur_arg = 1;
7487 } else if (!strcmp(args[0], "auth")) {
7488 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7489 cur_arg = 1;
7490
7491 while(*args[cur_arg]) {
7492 if (!strcmp(args[cur_arg], "realm")) {
7493 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7494 cur_arg+=2;
7495 continue;
7496 } else
7497 break;
7498 }
7499 } else {
7500req_error_parsing:
7501 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7502 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7503 return NULL;
7504 }
7505
7506 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7507 struct acl_cond *cond;
7508
7509 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7510 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7511 file, linenum, args[0]);
7512 return NULL;
7513 }
7514 rule->cond = cond;
7515 }
7516 else if (*args[cur_arg]) {
7517 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7518 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7519 file, linenum, args[0], args[cur_arg]);
7520 return NULL;
7521 }
7522
7523 return rule;
7524}
7525
Willy Tarreau8797c062007-05-07 00:55:35 +02007526/************************************************************************/
7527/* The code below is dedicated to ACL parsing and matching */
7528/************************************************************************/
7529
7530
Willy Tarreau14174bc2012-04-16 14:34:04 +02007531/* This function ensures that the prerequisites for an L7 fetch are ready,
7532 * which means that a request or response is ready. If some data is missing,
7533 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
7534 * to extract data from L7.
7535 *
7536 * The function returns :
7537 * 0 if some data is missing or if the requested data cannot be fetched
7538 * -1 if it is certain that we'll never have any HTTP message there
7539 * 1 if an HTTP message is ready
7540 */
7541static int
7542acl_prefetch_http(struct proxy *px, struct session *s, void *l7, int dir,
7543 struct acl_expr *expr, struct acl_test *test)
7544{
7545 struct http_txn *txn = l7;
7546 struct http_msg *msg = &txn->req;
7547
7548 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7549 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7550 */
7551
7552 if (unlikely(!s || !txn))
7553 return 0;
7554
7555 /* Check for a dependency on a request */
7556
7557 if (expr->kw->requires & ACL_USE_L7REQ_ANY) {
7558 if (unlikely(!s->req))
7559 return 0;
7560
7561 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
7562 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
7563 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7564 return -1;
7565 }
7566
7567 /* Try to decode HTTP request */
7568 if (likely(msg->next < s->req->i))
7569 http_msg_analyzer(msg, &txn->hdr_idx);
7570
7571 /* Still no valid request ? */
7572 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
7573 if ((msg->msg_state == HTTP_MSG_ERROR) || (s->req->flags & BF_FULL)) {
7574 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7575 return -1;
7576 }
7577 /* wait for final state */
7578 test->flags |= ACL_TEST_F_MAY_CHANGE;
7579 return 0;
7580 }
7581
7582 /* OK we just got a valid HTTP request. We have some minor
7583 * preparation to perform so that further checks can rely
7584 * on HTTP tests.
7585 */
7586 txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l);
7587 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7588 s->flags |= SN_REDIRECTABLE;
7589
7590 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
7591 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7592 return -1;
7593 }
7594 }
7595
7596 if ((expr->kw->requires & ACL_USE_L7REQ_VOLATILE) &&
7597 txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7598 return 0; /* data might have moved and indexes changed */
7599
7600 /* otherwise everything's ready for the request */
7601 }
7602
7603 /* Check for a dependency on a response */
7604 if (expr->kw->requires & ACL_USE_L7RTR_ANY) {
7605 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7606 return 0;
7607 }
7608
7609 /* everything's OK */
7610 return 1;
7611}
Willy Tarreau8797c062007-05-07 00:55:35 +02007612
Willy Tarreauc0239e02012-04-16 14:42:55 +02007613#define CHECK_HTTP_MESSAGE_FIRST() \
7614 do { int r = acl_prefetch_http(px, l4, l7, dir, expr, test); if (r <= 0) return r; } while (0)
7615
Willy Tarreau8797c062007-05-07 00:55:35 +02007616
7617/* 1. Check on METHOD
7618 * We use the pre-parsed method if it is known, and store its number as an
7619 * integer. If it is unknown, we use the pointer and the length.
7620 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007621static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007622{
7623 int len, meth;
7624
Willy Tarreauae8b7962007-06-09 23:10:04 +02007625 len = strlen(*text);
7626 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007627
7628 pattern->val.i = meth;
7629 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007630 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007631 if (!pattern->ptr.str)
7632 return 0;
7633 pattern->len = len;
7634 }
7635 return 1;
7636}
7637
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007638/* This function fetches the method of current HTTP request and stores
7639 * it in the global pattern struct as a chunk. There are two possibilities :
7640 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7641 * in <len> and <ptr> is NULL ;
7642 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7643 * <len> to its length.
7644 * This is intended to be used with acl_match_meth() only.
7645 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007646static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007647acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7648 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007649{
7650 int meth;
7651 struct http_txn *txn = l7;
7652
Willy Tarreauc0239e02012-04-16 14:42:55 +02007653 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007654
Willy Tarreau8797c062007-05-07 00:55:35 +02007655 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007656 temp_pattern.data.str.len = meth;
7657 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007658 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007659 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7660 /* ensure the indexes are not affected */
7661 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007662 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007663 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007664 }
7665 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7666 return 1;
7667}
7668
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007669/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007670static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7671{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007672 int icase;
7673
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007674
7675 if (temp_pattern.data.str.str == NULL) {
7676 /* well-known method */
7677 if (temp_pattern.data.str.len == pattern->val.i)
7678 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007679 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007680 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007681
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007682 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7683 if (pattern->val.i != HTTP_METH_OTHER)
7684 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007685
7686 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007687 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007688 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007689
7690 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007691 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7692 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007693 return ACL_PAT_FAIL;
7694 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007695}
7696
7697/* 2. Check on Request/Status Version
7698 * We simply compare strings here.
7699 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007700static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007701{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007702 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007703 if (!pattern->ptr.str)
7704 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007705 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007706 return 1;
7707}
7708
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007709static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007710acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7711 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007712{
7713 struct http_txn *txn = l7;
7714 char *ptr;
7715 int len;
7716
Willy Tarreauc0239e02012-04-16 14:42:55 +02007717 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007718
Willy Tarreau8797c062007-05-07 00:55:35 +02007719 len = txn->req.sl.rq.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007720 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007721
7722 while ((len-- > 0) && (*ptr++ != '/'));
7723 if (len <= 0)
7724 return 0;
7725
Willy Tarreau664092c2011-12-16 19:11:42 +01007726 temp_pattern.data.str.str = ptr;
7727 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007728
7729 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7730 return 1;
7731}
7732
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007733static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007734acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7735 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007736{
7737 struct http_txn *txn = l7;
7738 char *ptr;
7739 int len;
7740
Willy Tarreauc0239e02012-04-16 14:42:55 +02007741 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007742
Willy Tarreau8797c062007-05-07 00:55:35 +02007743 len = txn->rsp.sl.st.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007744 ptr = txn->rsp.buf->p + txn->rsp.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007745
7746 while ((len-- > 0) && (*ptr++ != '/'));
7747 if (len <= 0)
7748 return 0;
7749
Willy Tarreau664092c2011-12-16 19:11:42 +01007750 temp_pattern.data.str.str = ptr;
7751 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007752
7753 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7754 return 1;
7755}
7756
7757/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007758static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007759acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7760 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007761{
7762 struct http_txn *txn = l7;
7763 char *ptr;
7764 int len;
7765
Willy Tarreauc0239e02012-04-16 14:42:55 +02007766 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007767
Willy Tarreau8797c062007-05-07 00:55:35 +02007768 len = txn->rsp.sl.st.c_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007769 ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007770
Willy Tarreaua5e37562011-12-16 17:06:15 +01007771 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007772 test->flags = ACL_TEST_F_VOL_1ST;
7773 return 1;
7774}
7775
7776/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007777static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007778acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7779 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007780{
7781 struct http_txn *txn = l7;
7782
Willy Tarreauc0239e02012-04-16 14:42:55 +02007783 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007784
Willy Tarreau664092c2011-12-16 19:11:42 +01007785 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007786 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007787
Willy Tarreauf3d25982007-05-08 22:45:09 +02007788 /* we do not need to set READ_ONLY because the data is in a buffer */
7789 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007790 return 1;
7791}
7792
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007793static int
7794acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7795 struct acl_expr *expr, struct acl_test *test)
7796{
7797 struct http_txn *txn = l7;
7798
Willy Tarreauc0239e02012-04-16 14:42:55 +02007799 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007800
7801 /* Parse HTTP request */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007802 url2sa(txn->req.buf->p + 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 +01007803 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7804 return 0;
7805 temp_pattern.type = PATTERN_TYPE_IP;
7806 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007807
7808 /*
7809 * If we are parsing url in frontend space, we prepare backend stage
7810 * to not parse again the same url ! optimization lazyness...
7811 */
7812 if (px->options & PR_O_HTTP_PROXY)
7813 l4->flags |= SN_ADDR_SET;
7814
Willy Tarreauf4362b32011-12-16 17:49:52 +01007815 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007816 return 1;
7817}
7818
7819static int
7820acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7821 struct acl_expr *expr, struct acl_test *test)
7822{
7823 struct http_txn *txn = l7;
7824
Willy Tarreauc0239e02012-04-16 14:42:55 +02007825 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007826
7827 /* Same optimization as url_ip */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007828 url2sa(txn->req.buf->p + 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 +01007829 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007830
7831 if (px->options & PR_O_HTTP_PROXY)
7832 l4->flags |= SN_ADDR_SET;
7833
7834 test->flags = ACL_TEST_F_READ_ONLY;
7835 return 1;
7836}
7837
Willy Tarreauc11416f2007-06-17 16:58:38 +02007838/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02007839 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007840 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007841static int
Willy Tarreaue333ec92012-04-16 16:26:40 +02007842acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007843 struct acl_expr *expr, struct acl_test *test)
7844{
7845 struct http_txn *txn = l7;
7846 struct hdr_idx *idx = &txn->hdr_idx;
7847 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007848 const struct http_msg *msg = ((dir & ACL_DIR_MASK) == ACL_DIR_REQ) ? &txn->req : &txn->rsp;
7849
Willy Tarreau34db1082012-04-19 17:16:54 +02007850 if (!expr->args || expr->args->type != ARGT_STR)
7851 return 0;
7852
Willy Tarreaue333ec92012-04-16 16:26:40 +02007853 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02007854
7855 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7856 /* search for header from the beginning */
7857 ctx->idx = 0;
7858
Willy Tarreau34db1082012-04-19 17:16:54 +02007859 if (http_find_header2(expr->args->data.str.str, expr->args->data.str.len, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreau33a7e692007-06-10 19:45:56 +02007860 test->flags |= ACL_TEST_F_FETCH_MORE;
7861 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007862 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7863 temp_pattern.data.str.len = ctx->vlen;
7864
Willy Tarreau33a7e692007-06-10 19:45:56 +02007865 return 1;
7866 }
7867
7868 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7869 test->flags |= ACL_TEST_F_VOL_HDR;
7870 return 0;
7871}
7872
Willy Tarreauc11416f2007-06-17 16:58:38 +02007873/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02007874 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007875 */
7876static int
Willy Tarreaue333ec92012-04-16 16:26:40 +02007877acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007878 struct acl_expr *expr, struct acl_test *test)
7879{
7880 struct http_txn *txn = l7;
7881 struct hdr_idx *idx = &txn->hdr_idx;
7882 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007883 const struct http_msg *msg = ((dir & ACL_DIR_MASK) == ACL_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007884 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007885
Willy Tarreau34db1082012-04-19 17:16:54 +02007886 if (!expr->args || expr->args->type != ARGT_STR)
7887 return 0;
7888
Willy Tarreaue333ec92012-04-16 16:26:40 +02007889 CHECK_HTTP_MESSAGE_FIRST();
7890
Willy Tarreau33a7e692007-06-10 19:45:56 +02007891 ctx.idx = 0;
7892 cnt = 0;
Willy Tarreau34db1082012-04-19 17:16:54 +02007893 while (http_find_header2(expr->args->data.str.str, expr->args->data.str.len, msg->buf->p + msg->sol, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02007894 cnt++;
7895
Willy Tarreaua5e37562011-12-16 17:06:15 +01007896 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007897 test->flags = ACL_TEST_F_VOL_HDR;
7898 return 1;
7899}
7900
7901/* 7. Check on HTTP header's integer value. The integer value is returned.
7902 * FIXME: the type is 'int', it may not be appropriate for everything.
7903 */
7904static int
Willy Tarreaue333ec92012-04-16 16:26:40 +02007905acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007906 struct acl_expr *expr, struct acl_test *test)
7907{
Willy Tarreaud53e2422012-04-16 17:21:11 +02007908 int ret = acl_fetch_hdr(px, l4, l7, dir, expr, test);
Willy Tarreaue333ec92012-04-16 16:26:40 +02007909
Willy Tarreaud53e2422012-04-16 17:21:11 +02007910 if (ret > 0)
7911 temp_pattern.data.integer = strl2ic(temp_pattern.data.str.str, temp_pattern.data.str.len);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007912
Willy Tarreaud53e2422012-04-16 17:21:11 +02007913 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007914}
7915
Willy Tarreau106f9792009-09-19 07:54:16 +02007916/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
Willy Tarreau106f9792009-09-19 07:54:16 +02007917 */
7918static int
Willy Tarreaue333ec92012-04-16 16:26:40 +02007919acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau106f9792009-09-19 07:54:16 +02007920 struct acl_expr *expr, struct acl_test *test)
7921{
Willy Tarreaud53e2422012-04-16 17:21:11 +02007922 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02007923
Willy Tarreaud53e2422012-04-16 17:21:11 +02007924 while ((ret = acl_fetch_hdr(px, l4, l7, dir, expr, test)) > 0) {
Willy Tarreauf4362b32011-12-16 17:49:52 +01007925 temp_pattern.type = PATTERN_TYPE_IP;
Willy Tarreaud53e2422012-04-16 17:21:11 +02007926 if (url2ipv4((char *)temp_pattern.data.str.str, &temp_pattern.data.ip))
7927 break;
7928 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02007929 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02007930 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02007931}
7932
Willy Tarreau737b0c12007-06-10 21:28:46 +02007933/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
7934 * the first '/' after the possible hostname, and ends before the possible '?'.
7935 */
7936static int
7937acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
7938 struct acl_expr *expr, struct acl_test *test)
7939{
7940 struct http_txn *txn = l7;
7941 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007942
Willy Tarreauc0239e02012-04-16 14:42:55 +02007943 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007944
Willy Tarreau3a215be2012-03-09 21:39:51 +01007945 end = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01007946 ptr = http_get_path(txn);
7947 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02007948 return 0;
7949
7950 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01007951 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007952
7953 while (ptr < end && *ptr != '?')
7954 ptr++;
7955
Willy Tarreau664092c2011-12-16 19:11:42 +01007956 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02007957
7958 /* we do not need to set READ_ONLY because the data is in a buffer */
7959 test->flags = ACL_TEST_F_VOL_1ST;
7960 return 1;
7961}
7962
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007963static int
Willy Tarreauc0239e02012-04-16 14:42:55 +02007964acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, int dir,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007965 struct acl_expr *expr, struct acl_test *test)
7966{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007967 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7968 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7969 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007970
Willy Tarreauc0239e02012-04-16 14:42:55 +02007971 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02007972
7973 test->flags |= ACL_TEST_F_SET_RES_PASS;
7974 return 1;
7975}
7976
Willy Tarreau7f18e522010-10-22 20:04:13 +02007977/* return a valid test if the current request is the first one on the connection */
7978static int
7979acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
7980 struct acl_expr *expr, struct acl_test *test)
7981{
7982 if (!s)
7983 return 0;
7984
7985 if (s->txn.flags & TX_NOT_FIRST)
7986 test->flags |= ACL_TEST_F_SET_RES_FAIL;
7987 else
7988 test->flags |= ACL_TEST_F_SET_RES_PASS;
7989
7990 return 1;
7991}
7992
Willy Tarreau34db1082012-04-19 17:16:54 +02007993/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007994static int
Willy Tarreauc0239e02012-04-16 14:42:55 +02007995acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, int dir,
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007996 struct acl_expr *expr, struct acl_test *test)
7997{
7998
Willy Tarreau34db1082012-04-19 17:16:54 +02007999 if (!expr->args || expr->args->type != ARGT_USR)
8000 return 0;
8001
Willy Tarreauc0239e02012-04-16 14:42:55 +02008002 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008003
Willy Tarreauc0239e02012-04-16 14:42:55 +02008004 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008005 return 0;
8006
Willy Tarreau34db1082012-04-19 17:16:54 +02008007 test->ctx.a[0] = expr->args->data.usr;
Willy Tarreauc0239e02012-04-16 14:42:55 +02008008 test->ctx.a[1] = l4->txn.auth.user;
8009 test->ctx.a[2] = l4->txn.auth.pass;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008010
8011 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8012
8013 return 1;
8014}
Willy Tarreau8797c062007-05-07 00:55:35 +02008015
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008016/* Try to find the next occurrence of a cookie name in a cookie header value.
8017 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8018 * the cookie value is returned into *value and *value_l, and the function
8019 * returns a pointer to the next pointer to search from if the value was found.
8020 * Otherwise if the cookie was not found, NULL is returned and neither value
8021 * nor value_l are touched. The input <hdr> string should first point to the
8022 * header's value, and the <hdr_end> pointer must point to the first character
8023 * not part of the value. <list> must be non-zero if value may represent a list
8024 * of values (cookie headers). This makes it faster to abort parsing when no
8025 * list is expected.
8026 */
8027static char *
8028extract_cookie_value(char *hdr, const char *hdr_end,
8029 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008030 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008031{
8032 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8033 char *next;
8034
8035 /* we search at least a cookie name followed by an equal, and more
8036 * generally something like this :
8037 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8038 */
8039 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8040 /* Iterate through all cookies on this line */
8041
8042 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8043 att_beg++;
8044
8045 /* find att_end : this is the first character after the last non
8046 * space before the equal. It may be equal to hdr_end.
8047 */
8048 equal = att_end = att_beg;
8049
8050 while (equal < hdr_end) {
8051 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8052 break;
8053 if (http_is_spht[(unsigned char)*equal++])
8054 continue;
8055 att_end = equal;
8056 }
8057
8058 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8059 * is between <att_beg> and <equal>, both may be identical.
8060 */
8061
8062 /* look for end of cookie if there is an equal sign */
8063 if (equal < hdr_end && *equal == '=') {
8064 /* look for the beginning of the value */
8065 val_beg = equal + 1;
8066 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8067 val_beg++;
8068
8069 /* find the end of the value, respecting quotes */
8070 next = find_cookie_value_end(val_beg, hdr_end);
8071
8072 /* make val_end point to the first white space or delimitor after the value */
8073 val_end = next;
8074 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8075 val_end--;
8076 } else {
8077 val_beg = val_end = next = equal;
8078 }
8079
8080 /* We have nothing to do with attributes beginning with '$'. However,
8081 * they will automatically be removed if a header before them is removed,
8082 * since they're supposed to be linked together.
8083 */
8084 if (*att_beg == '$')
8085 continue;
8086
8087 /* Ignore cookies with no equal sign */
8088 if (equal == next)
8089 continue;
8090
8091 /* Now we have the cookie name between att_beg and att_end, and
8092 * its value between val_beg and val_end.
8093 */
8094
8095 if (att_end - att_beg == cookie_name_l &&
8096 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8097 /* let's return this value and indicate where to go on from */
8098 *value = val_beg;
8099 *value_l = val_end - val_beg;
8100 return next + 1;
8101 }
8102
8103 /* Set-Cookie headers only have the name in the first attr=value part */
8104 if (!list)
8105 break;
8106 }
8107
8108 return NULL;
8109}
8110
Willy Tarreaue333ec92012-04-16 16:26:40 +02008111/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008112 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
Willy Tarreaue333ec92012-04-16 16:26:40 +02008113 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. Depending on
8114 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau34db1082012-04-19 17:16:54 +02008115 * The cookie name is in expr->arg and the name length in expr->args->data.str.len.
8116 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008117 */
8118static int
Willy Tarreaue333ec92012-04-16 16:26:40 +02008119acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8120 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008121{
8122 struct http_txn *txn = l7;
8123 struct hdr_idx *idx = &txn->hdr_idx;
8124 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008125 const struct http_msg *msg;
8126 const char *hdr_name;
8127 int hdr_name_len;
8128 char *sol;
8129
Willy Tarreau34db1082012-04-19 17:16:54 +02008130 if (!expr->args || expr->args->type != ARGT_STR)
8131 return 0;
8132
Willy Tarreaue333ec92012-04-16 16:26:40 +02008133 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008134
Willy Tarreaue333ec92012-04-16 16:26:40 +02008135 if ((dir & ACL_DIR_MASK) == ACL_DIR_REQ) {
8136 msg = &txn->req;
8137 hdr_name = "Cookie";
8138 hdr_name_len = 6;
8139 } else {
8140 msg = &txn->rsp;
8141 hdr_name = "Set-Cookie";
8142 hdr_name_len = 10;
8143 }
8144
8145 sol = msg->buf->p + msg->sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008146 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8147 /* search for the header from the beginning, we must first initialize
8148 * the search parameters.
8149 */
8150 test->ctx.a[0] = NULL;
8151 ctx->idx = 0;
8152 }
8153
8154 while (1) {
8155 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8156 if (!test->ctx.a[0]) {
8157 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8158 goto out;
8159
Willy Tarreau34db1082012-04-19 17:16:54 +02008160 if (ctx->vlen < expr->args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008161 continue;
8162
8163 test->ctx.a[0] = ctx->line + ctx->val;
8164 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8165 }
8166
8167 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
Willy Tarreau34db1082012-04-19 17:16:54 +02008168 expr->args->data.str.str, expr->args->data.str.len,
Willy Tarreaue333ec92012-04-16 16:26:40 +02008169 (dir & ACL_DIR_MASK) == ACL_DIR_REQ,
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008170 &temp_pattern.data.str.str,
8171 &temp_pattern.data.str.len);
8172 if (test->ctx.a[0]) {
8173 /* one value was returned into temp_pattern.data.str.{str,len} */
8174 test->flags |= ACL_TEST_F_FETCH_MORE;
8175 test->flags |= ACL_TEST_F_VOL_HDR;
8176 return 1;
8177 }
8178 }
8179
8180 out:
8181 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8182 test->flags |= ACL_TEST_F_VOL_HDR;
8183 return 0;
8184}
8185
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008186/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau34db1082012-04-19 17:16:54 +02008187 * match the name in expr->arg and expr->args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008188 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008189 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008190 */
8191static int
Willy Tarreaue333ec92012-04-16 16:26:40 +02008192acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8193 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008194{
8195 struct http_txn *txn = l7;
8196 struct hdr_idx *idx = &txn->hdr_idx;
8197 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008198 const struct http_msg *msg;
8199 const char *hdr_name;
8200 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008201 int cnt;
8202 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008203 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008204
Willy Tarreau34db1082012-04-19 17:16:54 +02008205 if (!expr->args || expr->args->type != ARGT_STR)
8206 return 0;
8207
Willy Tarreaue333ec92012-04-16 16:26:40 +02008208 CHECK_HTTP_MESSAGE_FIRST();
8209
8210 if ((dir & ACL_DIR_MASK) == ACL_DIR_REQ) {
8211 msg = &txn->req;
8212 hdr_name = "Cookie";
8213 hdr_name_len = 6;
8214 } else {
8215 msg = &txn->rsp;
8216 hdr_name = "Set-Cookie";
8217 hdr_name_len = 10;
8218 }
8219
8220 sol = msg->buf->p + msg->sol;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008221 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008222 ctx.idx = 0;
8223 cnt = 0;
8224
8225 while (1) {
8226 /* Note: val_beg == NULL every time we need to fetch a new header */
8227 if (!val_beg) {
8228 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8229 break;
8230
Willy Tarreau34db1082012-04-19 17:16:54 +02008231 if (ctx.vlen < expr->args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008232 continue;
8233
8234 val_beg = ctx.line + ctx.val;
8235 val_end = val_beg + ctx.vlen;
8236 }
8237
8238 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau34db1082012-04-19 17:16:54 +02008239 expr->args->data.str.str, expr->args->data.str.len,
Willy Tarreaue333ec92012-04-16 16:26:40 +02008240 (dir & ACL_DIR_MASK) == ACL_DIR_REQ,
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008241 &temp_pattern.data.str.str,
8242 &temp_pattern.data.str.len))) {
8243 cnt++;
8244 }
8245 }
8246
8247 temp_pattern.data.integer = cnt;
8248 test->flags |= ACL_TEST_F_VOL_HDR;
8249 return 1;
8250}
8251
Willy Tarreau8797c062007-05-07 00:55:35 +02008252/************************************************************************/
8253/* All supported keywords must be declared here. */
8254/************************************************************************/
8255
8256/* Note: must not be declared <const> as its list will be overwritten */
8257static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008258 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8259
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008260 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008261 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8262 { "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 +02008263 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008264
Willy Tarreauc4262962010-05-10 23:42:40 +02008265 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008266 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8267 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8268 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8269 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8270 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8271 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008272 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008273 { "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 +02008274 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008275
Willy Tarreaue333ec92012-04-16 16:26:40 +02008276 { "hdr", acl_parse_str, acl_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8277 { "hdr_reg", acl_parse_reg, acl_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8278 { "hdr_beg", acl_parse_str, acl_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8279 { "hdr_end", acl_parse_str, acl_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8280 { "hdr_sub", acl_parse_str, acl_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8281 { "hdr_dir", acl_parse_str, acl_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8282 { "hdr_dom", acl_parse_str, acl_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8283 { "hdr_cnt", acl_parse_int, acl_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8284 { "hdr_len", acl_parse_int, acl_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8285 { "hdr_val", acl_parse_int, acl_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8286 { "hdr_ip", acl_parse_ip, acl_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreauc11416f2007-06-17 16:58:38 +02008287
Willy Tarreaue333ec92012-04-16 16:26:40 +02008288 { "shdr", acl_parse_str, acl_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8289 { "shdr_reg", acl_parse_reg, acl_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8290 { "shdr_beg", acl_parse_str, acl_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8291 { "shdr_end", acl_parse_str, acl_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8292 { "shdr_sub", acl_parse_str, acl_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8293 { "shdr_dir", acl_parse_str, acl_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8294 { "shdr_dom", acl_parse_str, acl_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8295 { "shdr_cnt", acl_parse_int, acl_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8296 { "shdr_len", acl_parse_int, acl_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8297 { "shdr_val", acl_parse_int, acl_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8298 { "shdr_ip", acl_parse_ip, acl_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008299
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008300 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8301 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8302 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8303 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8304 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8305 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8306 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8307 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8308 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8309
Willy Tarreaue333ec92012-04-16 16:26:40 +02008310 { "scook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8311 { "scook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8312 { "scook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8313 { "scook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8314 { "scook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8315 { "scook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8316 { "scook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8317 { "scook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8318 { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008319
Willy Tarreauc4262962010-05-10 23:42:40 +02008320 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008321 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8322 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8323 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8324 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8325 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8326 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008327 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008328
Willy Tarreau7744e0c2012-04-16 15:00:51 +02008329 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_VOLATILE },
8330 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau7f18e522010-10-22 20:04:13 +02008331 { "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 +02008332 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008333}};
8334
Willy Tarreau4a568972010-05-12 08:08:50 +02008335/************************************************************************/
8336/* The code below is dedicated to pattern fetching and matching */
8337/************************************************************************/
8338
Willy Tarreaue428fb72011-12-16 21:50:30 +01008339/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008340static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008341pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8342 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008343{
8344 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008345
Willy Tarreaue428fb72011-12-16 21:50:30 +01008346 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8347 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008348}
8349
David Cournapeau16023ee2010-12-23 20:55:41 +09008350/*
8351 * Given a path string and its length, find the position of beginning of the
8352 * query string. Returns NULL if no query string is found in the path.
8353 *
8354 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8355 *
8356 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8357 */
8358static inline char *find_query_string(char *path, size_t path_l)
8359{
8360 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008361
David Cournapeau16023ee2010-12-23 20:55:41 +09008362 p = memchr(path, '?', path_l);
8363 return p ? p + 1 : NULL;
8364}
8365
8366static inline int is_param_delimiter(char c)
8367{
8368 return c == '&' || c == ';';
8369}
8370
8371/*
8372 * Given a url parameter, find the starting position of the first occurence,
8373 * or NULL if the parameter is not found.
8374 *
8375 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8376 * the function will return query_string+8.
8377 */
8378static char*
8379find_url_param_pos(char* query_string, size_t query_string_l,
8380 char* url_param_name, size_t url_param_name_l)
8381{
8382 char *pos, *last;
8383
8384 pos = query_string;
8385 last = query_string + query_string_l - url_param_name_l - 1;
8386
8387 while (pos <= last) {
8388 if (pos[url_param_name_l] == '=') {
8389 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8390 return pos;
8391 pos += url_param_name_l + 1;
8392 }
8393 while (pos <= last && !is_param_delimiter(*pos))
8394 pos++;
8395 pos++;
8396 }
8397 return NULL;
8398}
8399
8400/*
8401 * Given a url parameter name, returns its value and size into *value and
8402 * *value_l respectively, and returns non-zero. If the parameter is not found,
8403 * zero is returned and value/value_l are not touched.
8404 */
8405static int
8406find_url_param_value(char* path, size_t path_l,
8407 char* url_param_name, size_t url_param_name_l,
8408 char** value, size_t* value_l)
8409{
8410 char *query_string, *qs_end;
8411 char *arg_start;
8412 char *value_start, *value_end;
8413
8414 query_string = find_query_string(path, path_l);
8415 if (!query_string)
8416 return 0;
8417
8418 qs_end = path + path_l;
8419 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8420 url_param_name, url_param_name_l);
8421 if (!arg_start)
8422 return 0;
8423
8424 value_start = arg_start + url_param_name_l + 1;
8425 value_end = value_start;
8426
8427 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8428 value_end++;
8429
8430 *value = value_start;
8431 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008432 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008433}
8434
8435static int
8436pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8437 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8438{
8439 struct http_txn *txn = l7;
8440 struct http_msg *msg = &txn->req;
8441 char *url_param_value;
8442 size_t url_param_value_l;
8443
Willy Tarreau3a215be2012-03-09 21:39:51 +01008444 if (!find_url_param_value(msg->buf->p + msg->sol + msg->sl.rq.u, msg->sl.rq.u_l,
David Cournapeau16023ee2010-12-23 20:55:41 +09008445 arg_p->data.str.str, arg_p->data.str.len,
8446 &url_param_value, &url_param_value_l))
8447 return 0;
8448
8449 data->str.str = url_param_value;
8450 data->str.len = url_param_value_l;
8451 return 1;
8452}
8453
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008454/* Try to find in request or response message is in <msg> and whose transaction
8455 * is in <txn> the last occurrence of a cookie name in all cookie header values
8456 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8457 * pointer and size of the last occurrence of the cookie value is returned into
8458 * <value> and <value_l>, and the function returns non-zero if the value was
8459 * found. Otherwise if the cookie was not found, zero is returned and neither
8460 * value nor value_l are touched. The input hdr string should begin at the
8461 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8462 * value may represent a list of values (cookie headers).
8463 */
8464
8465static int
8466find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8467 const char *hdr_name, int hdr_name_len,
8468 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008469 char **value, int *value_l)
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008470{
8471 struct hdr_ctx ctx;
8472 int found = 0;
8473
8474 ctx.idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008475 while (http_find_header2(hdr_name, hdr_name_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau4573af92012-04-06 18:20:06 +02008476 char *hdr, *end;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008477 if (ctx.vlen < cookie_name_l + 1)
8478 continue;
8479
Willy Tarreau4573af92012-04-06 18:20:06 +02008480 hdr = ctx.line + ctx.val;
8481 end = hdr + ctx.vlen;
8482 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8483 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008484 }
8485 return found;
8486}
8487
8488static int
8489pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8490 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8491{
8492 struct http_txn *txn = l7;
8493 struct http_msg *msg = &txn->req;
8494 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008495 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008496 int found = 0;
8497
8498 found = find_cookie_value(msg, txn, "Cookie", 6,
8499 arg_p->data.str.str, arg_p->data.str.len, 1,
8500 &cookie_value, &cookie_value_l);
8501 if (found) {
8502 data->str.str = cookie_value;
8503 data->str.len = cookie_value_l;
8504 }
8505
8506 return found;
8507}
8508
8509
8510static int
8511pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8512 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8513{
8514 struct http_txn *txn = l7;
8515 struct http_msg *msg = &txn->rsp;
8516 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008517 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008518 int found = 0;
8519
8520 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8521 arg_p->data.str.str, arg_p->data.str.len, 1,
8522 &cookie_value, &cookie_value_l);
8523 if (found) {
8524 data->str.str = cookie_value;
8525 data->str.len = cookie_value_l;
8526 }
8527
8528 return found;
8529}
8530
Emeric Brun485479d2010-09-23 18:02:19 +02008531
Willy Tarreau4a568972010-05-12 08:08:50 +02008532/************************************************************************/
8533/* All supported keywords must be declared here. */
8534/************************************************************************/
8535/* Note: must not be declared <const> as its list will be overwritten */
8536static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008537 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008538 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008539 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8540 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008541 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008542}};
8543
Willy Tarreau8797c062007-05-07 00:55:35 +02008544
8545__attribute__((constructor))
8546static void __http_protocol_init(void)
8547{
8548 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008549 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008550}
8551
8552
Willy Tarreau58f10d72006-12-04 02:26:12 +01008553/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008554 * Local variables:
8555 * c-indent-level: 8
8556 * c-basic-offset: 8
8557 * End:
8558 */