blob: dccf58c7d6a5e5c5f84cc6245721703471c02946 [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 Tarreau2d3d94c2008-11-30 20:20:08 +0100659 buffer_write(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 Tarreaub326fcc2007-03-03 13:54:32 +01001269 * depending on the initial msg->msg_state. It can be preempted everywhere
Willy Tarreau8973c702007-01-21 23:58:29 +01001270 * when data are missing and recalled at the exact same location with no
1271 * information loss. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001272 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau15de77e2010-01-02 21:59:16 +01001273 * fields. Note that msg->som and msg->sol will be initialized after completing
1274 * the first state, so that none of the msg pointers has to be initialized
1275 * prior to the first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001276 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001277void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001278{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001279 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001280 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaua560c212012-03-09 13:50:57 +01001281 struct buffer *buf = msg->buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001282
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001283 state = msg->msg_state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001284 ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001285 end = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 if (unlikely(ptr >= end))
1288 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001290 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001291 /*
1292 * First, states that are specific to the response only.
1293 * We check them first so that request and headers are
1294 * closer to each other (accessed more often).
1295 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001296 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001297 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001299 /* we have a start of message, but we have to check
1300 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001301 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001302 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001303 char *beg = buf->p;
1304
Willy Tarreau15de77e2010-01-02 21:59:16 +01001305 if (unlikely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001306 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001308 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001309 buffer_ignore(buf, ptr - beg);
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001311 msg->sol = msg->som = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 hdr_idx_init(idx);
1313 state = HTTP_MSG_RPVER;
1314 goto http_msg_rpver;
1315 }
1316
1317 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1318 goto http_msg_invalid;
1319
1320 if (unlikely(*ptr == '\n'))
1321 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1322 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1323 /* stop here */
1324
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001326 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001327 EXPECT_LF_HERE(ptr, http_msg_invalid);
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1329 /* stop here */
1330
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001332 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 case HTTP_MSG_RPVER_SP:
1334 case HTTP_MSG_RPCODE:
1335 case HTTP_MSG_RPCODE_SP:
1336 case HTTP_MSG_RPREASON:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001337 ptr = (char *)http_parse_stsline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001338 state, ptr, end,
1339 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 if (unlikely(!ptr))
1341 return;
1342
1343 /* we have a full response and we know that we have either a CR
1344 * or an LF at <ptr>.
1345 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1347
Willy Tarreau3a215be2012-03-09 21:39:51 +01001348 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 if (likely(*ptr == '\r'))
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1351 goto http_msg_rpline_end;
1352
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001354 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 /* msg->sol must point to the first of CR or LF. */
1356 EXPECT_LF_HERE(ptr, http_msg_invalid);
1357 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1358 /* stop here */
1359
1360 /*
1361 * Second, states that are specific to the request only
1362 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001363 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001364 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001365 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001366 /* we have a start of message, but we have to check
1367 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001368 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001369 */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001370 char *beg = buf->p;
1371
Willy Tarreau15de77e2010-01-02 21:59:16 +01001372 if (likely(ptr != beg)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01001373 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001374 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001375 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 buffer_ignore(buf, ptr - beg);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001377 }
Willy Tarreau3a215be2012-03-09 21:39:51 +01001378 msg->sol = msg->som = ptr - buf->p;
Willy Tarreauf0d058e2007-01-25 12:03:42 +01001379 /* we will need this when keep-alive will be supported
1380 hdr_idx_init(idx);
1381 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001382 state = HTTP_MSG_RQMETH;
1383 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001384 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001385
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001386 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1387 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (unlikely(*ptr == '\n'))
1390 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1391 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001392 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001393
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001394 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001395 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001396 EXPECT_LF_HERE(ptr, http_msg_invalid);
1397 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001398 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001399
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001401 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 case HTTP_MSG_RQMETH_SP:
1403 case HTTP_MSG_RQURI:
1404 case HTTP_MSG_RQURI_SP:
1405 case HTTP_MSG_RQVER:
Willy Tarreau62f791e2012-03-09 11:32:30 +01001406 ptr = (char *)http_parse_reqline(msg, buf->data,
Willy Tarreaua458b672012-03-05 11:17:50 +01001407 state, ptr, end,
1408 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 if (unlikely(!ptr))
1410 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 /* we have a full request and we know that we have either a CR
1413 * or an LF at <ptr>.
1414 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001416
Willy Tarreau3a215be2012-03-09 21:39:51 +01001417 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 if (likely(*ptr == '\r'))
1419 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001421
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001423 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 /* check for HTTP/0.9 request : no version information available.
1425 * msg->sol must point to the first of CR or LF.
1426 */
1427 if (unlikely(msg->sl.rq.v_l == 0))
1428 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 EXPECT_LF_HERE(ptr, http_msg_invalid);
1431 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001432 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001433
Willy Tarreau8973c702007-01-21 23:58:29 +01001434 /*
1435 * Common states below
1436 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001437 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001438 http_msg_hdr_first:
Willy Tarreau3a215be2012-03-09 21:39:51 +01001439 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 if (likely(!HTTP_IS_CRLF(*ptr))) {
1441 goto http_msg_hdr_name;
1442 }
1443
1444 if (likely(*ptr == '\r'))
1445 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1446 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001449 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001450 /* assumes msg->sol points to the first char */
1451 if (likely(HTTP_IS_TOKEN(*ptr)))
1452 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001453
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001454 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001457 if (likely(msg->err_pos < -1) || *ptr == '\n')
1458 goto http_msg_invalid;
1459
1460 if (msg->err_pos == -1) /* capture error pointer */
1461 msg->err_pos = ptr - buf->data; /* >= 0 now */
1462
1463 /* and we still accept this non-token character */
1464 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001468 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 if (likely(HTTP_IS_SPHT(*ptr)))
1470 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001471
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 /* header value can be basically anything except CR/LF */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001473 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 if (likely(!HTTP_IS_CRLF(*ptr))) {
1476 goto http_msg_hdr_val;
1477 }
1478
1479 if (likely(*ptr == '\r'))
1480 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1481 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001483 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001484 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001485 EXPECT_LF_HERE(ptr, http_msg_invalid);
1486 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001487
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001489 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 if (likely(HTTP_IS_SPHT(*ptr))) {
1491 /* replace HT,CR,LF with spaces */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001492 for (; buf->p + msg->sov < ptr; msg->sov++)
1493 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 goto http_msg_hdr_l1_sp;
1495 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001496 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001497 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 goto http_msg_complete_header;
1499
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001500 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001501 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001502 /* assumes msg->sol points to the first char, and msg->sov
1503 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 */
1505 if (likely(!HTTP_IS_CRLF(*ptr)))
1506 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001507
Willy Tarreau12e48b32012-03-05 16:57:34 +01001508 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 /* Note: we could also copy eol into ->eoh so that we have the
1510 * real header end in case it ends with lots of LWS, but is this
1511 * really needed ?
1512 */
1513 if (likely(*ptr == '\r'))
1514 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1515 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001516
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001517 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001518 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001519 EXPECT_LF_HERE(ptr, http_msg_invalid);
1520 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001521
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001523 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001524 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1525 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001526 for (; buf->p + msg->eol < ptr; msg->eol++)
1527 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 goto http_msg_hdr_val;
1529 }
1530 http_msg_complete_header:
1531 /*
1532 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001533 * Assumes msg->sol points to the first char, msg->sov points
1534 * to the first character of the value and msg->eol to the
1535 * first CR or LF so we know how the line ends. We insert last
1536 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001538 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001539 idx, idx->tail) < 0))
1540 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001541
Willy Tarreau3a215be2012-03-09 21:39:51 +01001542 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 if (likely(!HTTP_IS_CRLF(*ptr))) {
1544 goto http_msg_hdr_name;
1545 }
1546
1547 if (likely(*ptr == '\r'))
1548 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1549 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001550
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001551 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001552 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001553 /* Assumes msg->sol points to the first of either CR or LF */
1554 EXPECT_LF_HERE(ptr, http_msg_invalid);
1555 ptr++;
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001556 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001557 msg->eoh = msg->sol;
1558 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001559 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001561
1562 case HTTP_MSG_ERROR:
1563 /* this may only happen if we call http_msg_analyser() twice with an error */
1564 break;
1565
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001566#ifdef DEBUG_FULL
1567 default:
1568 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1569 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001570#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 }
1572 http_msg_ood:
1573 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001574 msg->msg_state = state;
Willy Tarreaua458b672012-03-05 11:17:50 +01001575 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001576 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001577
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 http_msg_invalid:
1579 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001580 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaua458b672012-03-05 11:17:50 +01001581 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 return;
1583}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001584
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001585/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1586 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1587 * nothing is done and 1 is returned.
1588 */
1589static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn)
1590{
1591 int delta;
1592 char *cur_end;
1593
1594 if (msg->sl.rq.v_l != 0)
1595 return 1;
1596
Willy Tarreau3a215be2012-03-09 21:39:51 +01001597 cur_end = req->p + msg->sol + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001598 delta = 0;
1599
1600 if (msg->sl.rq.u_l == 0) {
1601 /* if no URI was set, add "/" */
1602 delta = buffer_replace2(req, cur_end, cur_end, " /", 2);
1603 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001604 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001605 }
1606 /* add HTTP version */
1607 delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001608 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001609 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001610 cur_end = (char *)http_parse_reqline(msg, req->data,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001611 HTTP_MSG_RQMETH,
Willy Tarreau3a215be2012-03-09 21:39:51 +01001612 req->p + msg->sol, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 NULL, NULL);
1614 if (unlikely(!cur_end))
1615 return 0;
1616
1617 /* we have a full HTTP/1.0 request now and we know that
1618 * we have either a CR or an LF at <ptr>.
1619 */
1620 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1621 return 1;
1622}
1623
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001624/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001625 * and "keep-alive" values. If we already know that some headers may safely
1626 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001627 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1628 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1629 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1630 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1631 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001632 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001633 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001634void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001635{
Willy Tarreau5b154472009-12-21 20:11:07 +01001636 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001637 const char *hdr_val = "Connection";
1638 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001639
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001640 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001641 return;
1642
Willy Tarreau88d349d2010-01-25 12:15:43 +01001643 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1644 hdr_val = "Proxy-Connection";
1645 hdr_len = 16;
1646 }
1647
Willy Tarreau5b154472009-12-21 20:11:07 +01001648 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001649 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001650 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001651 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1652 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001653 if (to_del & 2)
1654 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001655 else
1656 txn->flags |= TX_CON_KAL_SET;
1657 }
1658 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1659 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001660 if (to_del & 1)
1661 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001662 else
1663 txn->flags |= TX_CON_CLO_SET;
1664 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001665 }
1666
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001667 txn->flags |= TX_HDR_CONN_PRS;
1668 return;
1669}
Willy Tarreau5b154472009-12-21 20:11:07 +01001670
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671/* Apply desired changes on the Connection: header. Values may be removed and/or
1672 * added depending on the <wanted> flags, which are exclusively composed of
1673 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1674 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1675 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001676void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001677{
1678 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001679 const char *hdr_val = "Connection";
1680 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001681
1682 ctx.idx = 0;
1683
Willy Tarreau88d349d2010-01-25 12:15:43 +01001684
1685 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1686 hdr_val = "Proxy-Connection";
1687 hdr_len = 16;
1688 }
1689
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001690 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001691 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001692 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1693 if (wanted & TX_CON_KAL_SET)
1694 txn->flags |= TX_CON_KAL_SET;
1695 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001696 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001697 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001698 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1699 if (wanted & TX_CON_CLO_SET)
1700 txn->flags |= TX_CON_CLO_SET;
1701 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001702 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001703 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001704 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001705
1706 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1707 return;
1708
1709 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1710 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001711 hdr_val = "Connection: close";
1712 hdr_len = 17;
1713 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1714 hdr_val = "Proxy-Connection: close";
1715 hdr_len = 23;
1716 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001717 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001718 }
1719
1720 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1721 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001722 hdr_val = "Connection: keep-alive";
1723 hdr_len = 22;
1724 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1725 hdr_val = "Proxy-Connection: keep-alive";
1726 hdr_len = 28;
1727 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001728 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001729 }
1730 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001731}
1732
Willy Tarreaua458b672012-03-05 11:17:50 +01001733/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001734 * first byte of body, and increments msg->sov by the number of bytes parsed,
1735 * so that we know we can forward between ->som and ->sov. Note that due to
1736 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1737 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001738 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001739 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001740 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001741int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001742{
Willy Tarreaua458b672012-03-05 11:17:50 +01001743 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
1744 char *ptr_old = ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001745 char *end = buf->data + buf->size;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001746 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau115acb92009-12-26 13:56:06 +01001747 unsigned int chunk = 0;
1748
1749 /* The chunk size is in the following form, though we are only
1750 * interested in the size and CRLF :
1751 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1752 */
1753 while (1) {
1754 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001755 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001756 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001757 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001758 if (c < 0) /* not a hex digit anymore */
1759 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001760 if (++ptr >= end)
1761 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001762 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001763 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001764 chunk = (chunk << 4) + c;
1765 }
1766
Willy Tarreaud98cf932009-12-27 22:54:55 +01001767 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001768 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001769 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001770
1771 while (http_is_spht[(unsigned char)*ptr]) {
1772 if (++ptr >= end)
1773 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001774 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001775 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001776 }
1777
Willy Tarreaud98cf932009-12-27 22:54:55 +01001778 /* Up to there, we know that at least one byte is present at *ptr. Check
1779 * for the end of chunk size.
1780 */
1781 while (1) {
1782 if (likely(HTTP_IS_CRLF(*ptr))) {
1783 /* we now have a CR or an LF at ptr */
1784 if (likely(*ptr == '\r')) {
1785 if (++ptr >= end)
1786 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001787 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001788 return 0;
1789 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001790
Willy Tarreaud98cf932009-12-27 22:54:55 +01001791 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001792 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001793 if (++ptr >= end)
1794 ptr = buf->data;
1795 /* done */
1796 break;
1797 }
1798 else if (*ptr == ';') {
1799 /* chunk extension, ends at next CRLF */
1800 if (++ptr >= end)
1801 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001802 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001803 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001804
1805 while (!HTTP_IS_CRLF(*ptr)) {
1806 if (++ptr >= end)
1807 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001808 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001809 return 0;
1810 }
1811 /* we have a CRLF now, loop above */
1812 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001813 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001814 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001815 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001816 }
1817
Willy Tarreaud98cf932009-12-27 22:54:55 +01001818 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001819 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001820 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001821 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001822 msg->sov += ptr - ptr_old;
1823 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001824 msg->chunk_len = chunk;
1825 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001827 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001828 error:
1829 msg->err_pos = ptr - buf->data;
1830 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001831}
1832
Willy Tarreaud98cf932009-12-27 22:54:55 +01001833/* This function skips trailers in the buffer <buf> associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001834 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835 * the trailers is found, it is automatically scheduled to be forwarded,
1836 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1837 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001838 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001839 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001840 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001841 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1842 * which implies that all non-trailers data have already been scheduled for
1843 * forwarding, and that the difference between msg->som and msg->sov exactly
1844 * matches the length of trailers already parsed and not forwarded. It is also
1845 * important to note that this function is designed to be able to parse wrapped
1846 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001847 */
1848int http_forward_trailers(struct buffer *buf, struct http_msg *msg)
1849{
Willy Tarreaua458b672012-03-05 11:17:50 +01001850 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001851 while (1) {
1852 char *p1 = NULL, *p2 = NULL;
Willy Tarreaua458b672012-03-05 11:17:50 +01001853 char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001854 char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau638cd022010-01-03 07:42:04 +01001855 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856
1857 /* scan current line and stop at LF or CRLF */
1858 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001859 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860 return 0;
1861
1862 if (*ptr == '\n') {
1863 if (!p1)
1864 p1 = ptr;
1865 p2 = ptr;
1866 break;
1867 }
1868
1869 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001870 if (p1) {
1871 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001872 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001873 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001874 p1 = ptr;
1875 }
1876
1877 ptr++;
1878 if (ptr >= buf->data + buf->size)
1879 ptr = buf->data;
1880 }
1881
1882 /* after LF; point to beginning of next line */
1883 p2++;
1884 if (p2 >= buf->data + buf->size)
1885 p2 = buf->data;
1886
Willy Tarreaua458b672012-03-05 11:17:50 +01001887 bytes = p2 - buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001888 if (bytes < 0)
1889 bytes += buf->size;
1890
1891 /* schedule this line for forwarding */
1892 msg->sov += bytes;
1893 if (msg->sov >= buf->size)
1894 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001895
Willy Tarreaua458b672012-03-05 11:17:50 +01001896 if (p1 == buffer_wrap_add(buf, buf->p + msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001897 /* LF/CRLF at beginning of line => end of trailers at p2.
1898 * Everything was scheduled for forwarding, there's nothing
1899 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001900 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001901 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001902 msg->msg_state = HTTP_MSG_DONE;
1903 return 1;
1904 }
1905 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001906 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001907 }
1908}
1909
1910/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1911 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreaua458b672012-03-05 11:17:50 +01001912 * ->som, ->next in order to include this part into the next forwarding phase.
1913 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001914 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1915 * not enough data are available, the function does not change anything and
1916 * returns zero. If a parse error is encountered, the function returns < 0 and
1917 * does not change anything. Note: this function is designed to parse wrapped
1918 * CRLF at the end of the buffer.
1919 */
1920int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg)
1921{
1922 char *ptr;
1923 int bytes;
1924
1925 /* NB: we'll check data availabilty at the end. It's not a
1926 * problem because whatever we match first will be checked
1927 * against the correct length.
1928 */
1929 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001930 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001931 if (*ptr == '\r') {
1932 bytes++;
1933 ptr++;
1934 if (ptr >= buf->data + buf->size)
1935 ptr = buf->data;
1936 }
1937
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001938 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001939 return 0;
1940
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001941 if (*ptr != '\n') {
1942 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001944 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001945
1946 ptr++;
1947 if (ptr >= buf->data + buf->size)
1948 ptr = buf->data;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001949 /* prepare the CRLF to be forwarded (between ->som and ->sov) */
1950 msg->som = 0;
1951 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1953 return 1;
1954}
Willy Tarreau5b154472009-12-21 20:11:07 +01001955
Willy Tarreau89fa7062012-03-02 16:13:16 +01001956/* This function may only be used when the buffer's o is empty */
Willy Tarreau83e3af02009-12-28 17:39:57 +01001957void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg)
1958{
Willy Tarreau89fa7062012-03-02 16:13:16 +01001959 int off = buf->data + buf->size - buf->p;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001960
1961 /* two possible cases :
1962 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001963 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001964 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001965 if (buf->i) {
1966 int block1 = buf->i;
Willy Tarreau8096de92010-02-26 11:12:27 +01001967 int block2 = 0;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001968 if (buf->p + buf->i > buf->data + buf->size) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001969 /* non-contiguous block */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001970 block1 = buf->data + buf->size - buf->p;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001971 block2 = buf->p + buf->i - (buf->data + buf->size);
Willy Tarreau8096de92010-02-26 11:12:27 +01001972 }
1973 if (block2)
1974 memcpy(swap_buffer, buf->data, block2);
Willy Tarreau89fa7062012-03-02 16:13:16 +01001975 memmove(buf->data, buf->p, block1);
Willy Tarreau8096de92010-02-26 11:12:27 +01001976 if (block2)
1977 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001978 }
1979
1980 /* adjust all known pointers */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001981 buf->p = buf->data;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001982
Willy Tarreau83e3af02009-12-28 17:39:57 +01001983 if (msg->err_pos >= 0) {
1984 msg->err_pos += off;
1985 if (msg->err_pos >= buf->size)
1986 msg->err_pos -= buf->size;
1987 }
1988
1989 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001990 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau83e3af02009-12-28 17:39:57 +01001991 buf->flags |= BF_FULL;
1992}
1993
Willy Tarreaud787e662009-07-07 10:14:51 +02001994/* This stream analyser waits for a complete HTTP request. It returns 1 if the
1995 * processing can continue on next analysers, or zero if it either needs more
1996 * data or wants to immediately abort the request (eg: timeout, error, ...). It
1997 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
1998 * when it has nothing left to do, and may remove any analyser when it wants to
1999 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002000 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002001int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002002{
Willy Tarreau59234e92008-11-30 23:51:27 +01002003 /*
2004 * We will parse the partial (or complete) lines.
2005 * We will check the request syntax, and also join multi-line
2006 * headers. An index of all the lines will be elaborated while
2007 * parsing.
2008 *
2009 * For the parsing, we use a 28 states FSM.
2010 *
2011 * Here is the information we currently have :
Willy Tarreauea1175a2012-03-05 15:52:30 +01002012 * req->p + msg->som = beginning of request
2013 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau83e3af02009-12-28 17:39:57 +01002014 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01002015 * msg->next = first non-visited byte
Willy Tarreau59234e92008-11-30 23:51:27 +01002016 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002017 *
2018 * At end of parsing, we may perform a capture of the error (if any), and
2019 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2020 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2021 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002022 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002023
Willy Tarreau59234e92008-11-30 23:51:27 +01002024 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002025 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002026 struct http_txn *txn = &s->txn;
2027 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002028 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002029
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002030 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 +01002031 now_ms, __FUNCTION__,
2032 s,
2033 req,
2034 req->rex, req->wex,
2035 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002036 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002037 req->analysers);
2038
Willy Tarreau52a0c602009-08-16 22:45:38 +02002039 /* we're speaking HTTP here, so let's speak HTTP to the client */
2040 s->srv_error = http_return_srv_error;
2041
Willy Tarreau83e3af02009-12-28 17:39:57 +01002042 /* There's a protected area at the end of the buffer for rewriting
2043 * purposes. We don't want to start to parse the request if the
2044 * protected area is affected, because we may have to move processed
2045 * data later, which is much more complicated.
2046 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002047 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002048 if ((txn->flags & TX_NOT_FIRST) &&
2049 unlikely((req->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002050 buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002051 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002052 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002053 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2054 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002055 /* some data has still not left the buffer, wake us once that's done */
2056 buffer_dont_connect(req);
2057 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2058 return 0;
2059 }
Willy Tarreaua458b672012-03-05 11:17:50 +01002060 if (buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002061 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002062 http_buffer_heavy_realign(req, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002063 }
2064
Willy Tarreau065e8332010-01-08 00:30:20 +01002065 /* Note that we have the same problem with the response ; we
2066 * may want to send a redirect, error or anything which requires
2067 * some spare space. So we'll ensure that we have at least
2068 * maxrewrite bytes available in the response buffer before
2069 * processing that one. This will only affect pipelined
2070 * keep-alive requests.
2071 */
2072 if ((txn->flags & TX_NOT_FIRST) &&
2073 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002074 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) < buffer_wrap_add(s->rep, s->rep->p + txn->rsp.next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002075 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) > s->rep->data + s->rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002076 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002077 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2078 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002079 /* don't let a connection request be initiated */
2080 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002081 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002082 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002083 return 0;
2084 }
2085 }
2086
Willy Tarreaua458b672012-03-05 11:17:50 +01002087 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002088 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002089 }
2090
Willy Tarreau59234e92008-11-30 23:51:27 +01002091 /* 1: we might have to print this header in debug mode */
2092 if (unlikely((global.mode & MODE_DEBUG) &&
2093 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002094 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002095 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002096 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002097
Willy Tarreauea1175a2012-03-05 15:52:30 +01002098 sol = req->p + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002099 eol = sol + msg->sl.rq.l;
2100 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002101
Willy Tarreau59234e92008-11-30 23:51:27 +01002102 sol += hdr_idx_first_pos(&txn->hdr_idx);
2103 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002104
Willy Tarreau59234e92008-11-30 23:51:27 +01002105 while (cur_idx) {
2106 eol = sol + txn->hdr_idx.v[cur_idx].len;
2107 debug_hdr("clihdr", s, sol, eol);
2108 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2109 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002110 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002111 }
2112
Willy Tarreau58f10d72006-12-04 02:26:12 +01002113
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 /*
2115 * Now we quickly check if we have found a full valid request.
2116 * If not so, we check the FD and buffer states before leaving.
2117 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002118 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002119 * requests are checked first. When waiting for a second request
2120 * on a keep-alive session, if we encounter and error, close, t/o,
2121 * we note the error in the session flags but don't set any state.
2122 * Since the error will be noted there, it will not be counted by
2123 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002124 * Last, we may increase some tracked counters' http request errors on
2125 * the cases that are deliberately the client's fault. For instance,
2126 * a timeout or connection reset is not counted as an error. However
2127 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002128 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002129
Willy Tarreau655dce92009-11-08 13:10:58 +01002130 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002131 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002132 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002133 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002134 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002135 session_inc_http_req_ctr(s);
2136 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002137 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002138 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002139 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002140
Willy Tarreau59234e92008-11-30 23:51:27 +01002141 /* 1: Since we are in header mode, if there's no space
2142 * left for headers, we won't be able to free more
2143 * later, so the session will never terminate. We
2144 * must terminate it now.
2145 */
2146 if (unlikely(req->flags & BF_FULL)) {
2147 /* FIXME: check if URI is set and return Status
2148 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002149 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002150 session_inc_http_req_ctr(s);
2151 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002152 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002153 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002154 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 goto return_bad_req;
2156 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002157
Willy Tarreau59234e92008-11-30 23:51:27 +01002158 /* 2: have we encountered a read error ? */
2159 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002160 if (!(s->flags & SN_ERR_MASK))
2161 s->flags |= SN_ERR_CLICL;
2162
Willy Tarreaufcffa692010-01-10 14:21:19 +01002163 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002164 goto failed_keep_alive;
2165
Willy Tarreau59234e92008-11-30 23:51:27 +01002166 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002167 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002168 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002169 session_inc_http_err_ctr(s);
2170 }
2171
Willy Tarreau59234e92008-11-30 23:51:27 +01002172 msg->msg_state = HTTP_MSG_ERROR;
2173 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002174
Willy Tarreauda7ff642010-06-23 11:44:09 +02002175 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002176 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002177 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002178 if (s->listener->counters)
2179 s->listener->counters->failed_req++;
2180
Willy Tarreau59234e92008-11-30 23:51:27 +01002181 if (!(s->flags & SN_FINST_MASK))
2182 s->flags |= SN_FINST_R;
2183 return 0;
2184 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 /* 3: has the read timeout expired ? */
2187 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002188 if (!(s->flags & SN_ERR_MASK))
2189 s->flags |= SN_ERR_CLITO;
2190
Willy Tarreaufcffa692010-01-10 14:21:19 +01002191 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002192 goto failed_keep_alive;
2193
Willy Tarreau59234e92008-11-30 23:51:27 +01002194 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002195 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002196 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002197 session_inc_http_err_ctr(s);
2198 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002199 txn->status = 408;
2200 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2201 msg->msg_state = HTTP_MSG_ERROR;
2202 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002203
Willy Tarreauda7ff642010-06-23 11:44:09 +02002204 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002205 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002206 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002207 if (s->listener->counters)
2208 s->listener->counters->failed_req++;
2209
Willy Tarreau59234e92008-11-30 23:51:27 +01002210 if (!(s->flags & SN_FINST_MASK))
2211 s->flags |= SN_FINST_R;
2212 return 0;
2213 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002214
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 /* 4: have we encountered a close ? */
2216 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002217 if (!(s->flags & SN_ERR_MASK))
2218 s->flags |= SN_ERR_CLICL;
2219
Willy Tarreaufcffa692010-01-10 14:21:19 +01002220 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002221 goto failed_keep_alive;
2222
Willy Tarreau4076a152009-04-02 15:18:36 +02002223 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002224 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002225 txn->status = 400;
2226 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2227 msg->msg_state = HTTP_MSG_ERROR;
2228 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002229
Willy Tarreauda7ff642010-06-23 11:44:09 +02002230 session_inc_http_err_ctr(s);
2231 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002232 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002233 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002234 if (s->listener->counters)
2235 s->listener->counters->failed_req++;
2236
Willy Tarreau59234e92008-11-30 23:51:27 +01002237 if (!(s->flags & SN_FINST_MASK))
2238 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002239 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002240 }
2241
Willy Tarreau520d95e2009-09-19 21:04:57 +02002242 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002243 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002244 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002245#ifdef TCP_QUICKACK
2246 if (s->listener->options & LI_O_NOQUICKACK) {
2247 /* We need more data, we have to re-enable quick-ack in case we
2248 * previously disabled it, otherwise we might cause the client
2249 * to delay next data.
2250 */
2251 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2252 }
2253#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002254
Willy Tarreaufcffa692010-01-10 14:21:19 +01002255 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2256 /* If the client starts to talk, let's fall back to
2257 * request timeout processing.
2258 */
2259 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002260 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002261 }
2262
Willy Tarreau59234e92008-11-30 23:51:27 +01002263 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002264 if (!tick_isset(req->analyse_exp)) {
2265 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2266 (txn->flags & TX_WAIT_NEXT_RQ) &&
2267 tick_isset(s->be->timeout.httpka))
2268 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2269 else
2270 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2271 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002272
Willy Tarreau59234e92008-11-30 23:51:27 +01002273 /* we're not ready yet */
2274 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002275
2276 failed_keep_alive:
2277 /* Here we process low-level errors for keep-alive requests. In
2278 * short, if the request is not the first one and it experiences
2279 * a timeout, read error or shutdown, we just silently close so
2280 * that the client can try again.
2281 */
2282 txn->status = 0;
2283 msg->msg_state = HTTP_MSG_RQBEFORE;
2284 req->analysers = 0;
2285 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002286 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002287 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002288 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002289 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002290
Willy Tarreaud787e662009-07-07 10:14:51 +02002291 /* OK now we have a complete HTTP request with indexed headers. Let's
2292 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002293 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2294 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002295 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002296 * byte after the last LF. msg->sov points to the first byte of data.
2297 * msg->eol cannot be trusted because it may have been left uninitialized
2298 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002299 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002300
Willy Tarreauda7ff642010-06-23 11:44:09 +02002301 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002302 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2303
Willy Tarreaub16a5742010-01-10 14:46:16 +01002304 if (txn->flags & TX_WAIT_NEXT_RQ) {
2305 /* kill the pending keep-alive timeout */
2306 txn->flags &= ~TX_WAIT_NEXT_RQ;
2307 req->analyse_exp = TICK_ETERNITY;
2308 }
2309
2310
Willy Tarreaud787e662009-07-07 10:14:51 +02002311 /* Maybe we found in invalid header name while we were configured not
2312 * to block on that, so we have to capture it now.
2313 */
2314 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002315 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002316
Willy Tarreau59234e92008-11-30 23:51:27 +01002317 /*
2318 * 1: identify the method
2319 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002320 txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002321
2322 /* we can make use of server redirect on GET and HEAD */
2323 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2324 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002325
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 /*
2327 * 2: check if the URI matches the monitor_uri.
2328 * We have to do this for every request which gets in, because
2329 * the monitor-uri is defined by the frontend.
2330 */
2331 if (unlikely((s->fe->monitor_uri_len != 0) &&
2332 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002333 !memcmp(req->p + msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002334 s->fe->monitor_uri,
2335 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002336 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002338 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002340
Willy Tarreau59234e92008-11-30 23:51:27 +01002341 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002342 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002343
Willy Tarreau59234e92008-11-30 23:51:27 +01002344 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002345 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2346 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 ret = acl_pass(ret);
2349 if (cond->pol == ACL_COND_UNLESS)
2350 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002351
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 if (ret) {
2353 /* we fail this request, let's return 503 service unavail */
2354 txn->status = 503;
2355 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2356 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002357 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002358 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002359
Willy Tarreau59234e92008-11-30 23:51:27 +01002360 /* nothing to fail, let's reply normaly */
2361 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002362 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 goto return_prx_cond;
2364 }
2365
2366 /*
2367 * 3: Maybe we have to copy the original REQURI for the logs ?
2368 * Note: we cannot log anymore if the request has been
2369 * classified as invalid.
2370 */
2371 if (unlikely(s->logs.logwait & LW_REQ)) {
2372 /* we have a complete HTTP request that we must log */
2373 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2374 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002375
Willy Tarreau59234e92008-11-30 23:51:27 +01002376 if (urilen >= REQURI_LEN)
2377 urilen = REQURI_LEN - 1;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002378 memcpy(txn->uri, &req->p[msg->som], urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002379 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 if (!(s->logs.logwait &= ~LW_REQ))
2382 s->do_log(s);
2383 } else {
2384 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002385 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 }
Willy Tarreau06619262006-12-17 08:37:22 +01002387
William Lallemanda73203e2012-03-12 12:48:57 +01002388 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2389 s->unique_id = pool_alloc2(pool2_uniqueid);
2390 }
2391
Willy Tarreau59234e92008-11-30 23:51:27 +01002392 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002393 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn))
2394 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002395
Willy Tarreau5b154472009-12-21 20:11:07 +01002396 /* ... and check if the request is HTTP/1.1 or above */
2397 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002398 ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') ||
2399 ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') &&
2400 (req->p[msg->sol + msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002401 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002402
2403 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002404 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002405
Willy Tarreau88d349d2010-01-25 12:15:43 +01002406 /* if the frontend has "option http-use-proxy-header", we'll check if
2407 * we have what looks like a proxied connection instead of a connection,
2408 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2409 * Note that this is *not* RFC-compliant, however browsers and proxies
2410 * happen to do that despite being non-standard :-(
2411 * We consider that a request not beginning with either '/' or '*' is
2412 * a proxied connection, which covers both "scheme://location" and
2413 * CONNECT ip:port.
2414 */
2415 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002416 req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002417 txn->flags |= TX_USE_PX_CONN;
2418
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002419 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002420 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002421
Willy Tarreau59234e92008-11-30 23:51:27 +01002422 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002423 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01002424 capture_headers(req->p + msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002425 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002426
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002427 /* 6: determine the transfer-length.
2428 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2429 * the presence of a message-body in a REQUEST and its transfer length
2430 * must be determined that way (in order of precedence) :
2431 * 1. The presence of a message-body in a request is signaled by the
2432 * inclusion of a Content-Length or Transfer-Encoding header field
2433 * in the request's header fields. When a request message contains
2434 * both a message-body of non-zero length and a method that does
2435 * not define any semantics for that request message-body, then an
2436 * origin server SHOULD either ignore the message-body or respond
2437 * with an appropriate error message (e.g., 413). A proxy or
2438 * gateway, when presented the same request, SHOULD either forward
2439 * the request inbound with the message- body or ignore the
2440 * message-body when determining a response.
2441 *
2442 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2443 * and the "chunked" transfer-coding (Section 6.2) is used, the
2444 * transfer-length is defined by the use of this transfer-coding.
2445 * If a Transfer-Encoding header field is present and the "chunked"
2446 * transfer-coding is not present, the transfer-length is defined
2447 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002448 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002449 * 3. If a Content-Length header field is present, its decimal value in
2450 * OCTETs represents both the entity-length and the transfer-length.
2451 * If a message is received with both a Transfer-Encoding header
2452 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002453 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002454 * 4. By the server closing the connection. (Closing the connection
2455 * cannot be used to indicate the end of a request body, since that
2456 * would leave no possibility for the server to send back a response.)
2457 *
2458 * Whenever a transfer-coding is applied to a message-body, the set of
2459 * transfer-codings MUST include "chunked", unless the message indicates
2460 * it is terminated by closing the connection. When the "chunked"
2461 * transfer-coding is used, it MUST be the last transfer-coding applied
2462 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002463 */
2464
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002465 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002466 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002467 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002468 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002469 http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002470 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002471 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2472 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002473 /* bad transfer-encoding (chunked followed by something else) */
2474 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002475 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002476 break;
2477 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002478 }
2479
Willy Tarreau32b47f42009-10-18 20:55:02 +02002480 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002481 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002482 http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002483 signed long long cl;
2484
Willy Tarreauad14f752011-09-02 20:33:27 +02002485 if (!ctx.vlen) {
2486 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002487 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002488 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002489
Willy Tarreauad14f752011-09-02 20:33:27 +02002490 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2491 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002492 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002493 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002494
Willy Tarreauad14f752011-09-02 20:33:27 +02002495 if (cl < 0) {
2496 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002497 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002498 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002499
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002500 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02002501 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002502 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002503 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002504
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002505 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002506 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002507 }
2508
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002509 /* bodyless requests have a known length */
2510 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002511 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002512
Willy Tarreaud787e662009-07-07 10:14:51 +02002513 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002514 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002515 req->analyse_exp = TICK_ETERNITY;
2516 return 1;
2517
2518 return_bad_req:
2519 /* We centralize bad requests processing here */
2520 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2521 /* we detected a parsing error. We want to archive this request
2522 * in the dedicated proxy area for later troubleshooting.
2523 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002524 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002525 }
2526
2527 txn->req.msg_state = HTTP_MSG_ERROR;
2528 txn->status = 400;
2529 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002530
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002531 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002532 if (s->listener->counters)
2533 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002534
2535 return_prx_cond:
2536 if (!(s->flags & SN_ERR_MASK))
2537 s->flags |= SN_ERR_PRXCOND;
2538 if (!(s->flags & SN_FINST_MASK))
2539 s->flags |= SN_FINST_R;
2540
2541 req->analysers = 0;
2542 req->analyse_exp = TICK_ETERNITY;
2543 return 0;
2544}
2545
Cyril Bonté70be45d2010-10-12 00:14:35 +02002546/* We reached the stats page through a POST request.
2547 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002548 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002549 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002550int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002551{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002552 struct proxy *px = NULL;
2553 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002554
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002555 char key[LINESIZE];
2556 int action = ST_ADM_ACTION_NONE;
2557 int reprocess = 0;
2558
2559 int total_servers = 0;
2560 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002561
2562 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002563 char *st_cur_param = NULL;
2564 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002565
Willy Tarreauea1175a2012-03-05 15:52:30 +01002566 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002567 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002568
2569 cur_param = next_param = end_params;
2570
Cyril Bonté23b39d92011-02-10 22:54:44 +01002571 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002572 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002573 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002574 return 1;
2575 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002576 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002577 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002578 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002579 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002580 }
2581
2582 *end_params = '\0';
2583
Willy Tarreau295a8372011-03-10 11:25:07 +01002584 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002585
2586 /*
2587 * Parse the parameters in reverse order to only store the last value.
2588 * From the html form, the backend and the action are at the end.
2589 */
2590 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002591 char *value;
2592 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002593
2594 cur_param--;
2595 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002596 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002597 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002598 poffset = (cur_param != first_param ? 1 : 0);
2599 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2600 if ((plen > 0) && (plen <= sizeof(key))) {
2601 strncpy(key, cur_param + poffset, plen);
2602 key[plen - 1] = '\0';
2603 } else {
2604 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2605 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002606 }
2607
2608 /* Parse the value */
2609 value = key;
2610 while (*value != '\0' && *value != '=') {
2611 value++;
2612 }
2613 if (*value == '=') {
2614 /* Ok, a value is found, we can mark the end of the key */
2615 *value++ = '\0';
2616 }
2617
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002618 if (!url_decode(key) || !url_decode(value))
2619 break;
2620
Cyril Bonté70be45d2010-10-12 00:14:35 +02002621 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002622 if (!px && (strcmp(key, "b") == 0)) {
2623 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2624 /* the backend name is unknown or ambiguous (duplicate names) */
2625 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2626 goto out;
2627 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002628 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002629 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002630 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002631 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002632 }
2633 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002634 action = ST_ADM_ACTION_ENABLE;
2635 }
2636 else {
2637 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2638 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002639 }
2640 }
2641 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002642 if (!(px && action)) {
2643 /*
2644 * Indicates that we'll need to reprocess the parameters
2645 * as soon as backend and action are known
2646 */
2647 if (!reprocess) {
2648 st_cur_param = cur_param;
2649 st_next_param = next_param;
2650 }
2651 reprocess = 1;
2652 }
2653 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002654 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002655 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002656 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002657 /* Not already in maintenance, we can change the server state */
2658 sv->state |= SRV_MAINTAIN;
2659 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002660 altered_servers++;
2661 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002662 }
2663 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002664 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002665 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002666 /* Already in maintenance, we can change the server state */
2667 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002668 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002669 altered_servers++;
2670 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002671 }
2672 break;
2673 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002674 } else {
2675 /* the server name is unknown or ambiguous (duplicate names) */
2676 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002677 }
2678 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002679 if (reprocess && px && action) {
2680 /* Now, we know the backend and the action chosen by the user.
2681 * We can safely restart from the first server parameter
2682 * to reprocess them
2683 */
2684 cur_param = st_cur_param;
2685 next_param = st_next_param;
2686 reprocess = 0;
2687 goto reprocess_servers;
2688 }
2689
Cyril Bonté70be45d2010-10-12 00:14:35 +02002690 next_param = cur_param;
2691 }
2692 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002693
2694 if (total_servers == 0) {
2695 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2696 }
2697 else if (altered_servers == 0) {
2698 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2699 }
2700 else if (altered_servers == total_servers) {
2701 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2702 }
2703 else {
2704 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2705 }
2706 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002707 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002708}
2709
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002710/* returns a pointer to the first rule which forbids access (deny or http_auth),
2711 * or NULL if everything's OK.
2712 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002713static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002714http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2715{
Willy Tarreauff011f22011-01-06 17:51:27 +01002716 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002717
Willy Tarreauff011f22011-01-06 17:51:27 +01002718 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002719 int ret = 1;
2720
Willy Tarreauff011f22011-01-06 17:51:27 +01002721 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002722 continue;
2723
2724 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002725 if (rule->cond) {
2726 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002727 ret = acl_pass(ret);
2728
Willy Tarreauff011f22011-01-06 17:51:27 +01002729 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002730 ret = !ret;
2731 }
2732
2733 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002734 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002735 return NULL; /* no problem */
2736 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002737 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002738 }
2739 }
2740 return NULL;
2741}
2742
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002743/* This stream analyser runs all HTTP request processing which is common to
2744 * frontends and backends, which means blocking ACLs, filters, connection-close,
2745 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002746 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002747 * either needs more data or wants to immediately abort the request (eg: deny,
2748 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002749 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002750int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002751{
Willy Tarreaud787e662009-07-07 10:14:51 +02002752 struct http_txn *txn = &s->txn;
2753 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002754 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002755 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002756 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002757 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002758 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002759
Willy Tarreau655dce92009-11-08 13:10:58 +01002760 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002761 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002762 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002763 return 0;
2764 }
2765
Willy Tarreau3a816292009-07-07 10:55:49 +02002766 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002767 req->analyse_exp = TICK_ETERNITY;
2768
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002769 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 +02002770 now_ms, __FUNCTION__,
2771 s,
2772 req,
2773 req->rex, req->wex,
2774 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002775 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002776 req->analysers);
2777
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002778 /* first check whether we have some ACLs set to block this request */
2779 list_for_each_entry(cond, &px->block_cond, list) {
2780 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002781
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002782 ret = acl_pass(ret);
2783 if (cond->pol == ACL_COND_UNLESS)
2784 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002785
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002786 if (ret) {
2787 txn->status = 403;
2788 /* let's log the request time */
2789 s->logs.tv_request = now;
2790 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002791 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002792 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002793 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002794 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002795
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002796 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002797 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002798
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002799 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002800 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002801 do_stats = stats_check_uri(s->rep->prod, txn, px);
2802 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002803 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 +01002804 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002805 else
2806 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002807
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002808 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002809 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002810 txn->status = 403;
2811 s->logs.tv_request = now;
2812 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002813 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002814 s->fe->fe_counters.denied_req++;
2815 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2816 s->be->be_counters.denied_req++;
2817 if (s->listener->counters)
2818 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002819 goto return_prx_cond;
2820 }
2821
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002822 /* try headers filters */
2823 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002824 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002825 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002826
Willy Tarreau59234e92008-11-30 23:51:27 +01002827 /* has the request been denied ? */
2828 if (txn->flags & TX_CLDENY) {
2829 /* no need to go further */
2830 txn->status = 403;
2831 /* let's log the request time */
2832 s->logs.tv_request = now;
2833 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002834 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002835 goto return_prx_cond;
2836 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002837
2838 /* When a connection is tarpitted, we use the tarpit timeout,
2839 * which may be the same as the connect timeout if unspecified.
2840 * If unset, then set it to zero because we really want it to
2841 * eventually expire. We build the tarpit as an analyser.
2842 */
2843 if (txn->flags & TX_CLTARPIT) {
2844 buffer_erase(s->req);
2845 /* wipe the request out so that we can drop the connection early
2846 * if the client closes first.
2847 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002848 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002849 req->analysers = 0; /* remove switching rules etc... */
2850 req->analysers |= AN_REQ_HTTP_TARPIT;
2851 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2852 if (!req->analyse_exp)
2853 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002854 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002855 return 1;
2856 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002857 }
Willy Tarreau06619262006-12-17 08:37:22 +01002858
Willy Tarreau5b154472009-12-21 20:11:07 +01002859 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2860 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002861 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2862 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002863 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2864 * avoid to redo the same work if FE and BE have the same settings (common).
2865 * The method consists in checking if options changed between the two calls
2866 * (implying that either one is non-null, or one of them is non-null and we
2867 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002868 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002869
Willy Tarreaudc008c52010-02-01 16:20:08 +01002870 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2871 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2872 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2873 (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 +01002874 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002875
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002876 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2877 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002878 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002879 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2880 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002881 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002882 tmp = TX_CON_WANT_CLO;
2883
Willy Tarreau5b154472009-12-21 20:11:07 +01002884 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2885 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002886
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002887 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2888 /* parse the Connection header and possibly clean it */
2889 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002890 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002891 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2892 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002893 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002894 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002895 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002896 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002897 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002898
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002899 /* check if client or config asks for explicit close in KAL/SCL */
2900 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2901 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2902 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002903 (!(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 +01002904 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002905 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002906 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002907 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2908 }
Willy Tarreau78599912009-10-17 20:12:21 +02002909
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002910 /* we can be blocked here because the request needs to be authenticated,
2911 * either to pass or to access stats.
2912 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002913 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002914 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002915 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002916
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002917 if (!realm)
2918 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2919
Willy Tarreau844a7e72010-01-31 21:46:18 +01002920 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002921 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2922 txn->status = 401;
2923 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002924 /* on 401 we still count one error, because normal browsing
2925 * won't significantly increase the counter but brute force
2926 * attempts will.
2927 */
2928 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002929 goto return_prx_cond;
2930 }
2931
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002932 /* add request headers from the rule sets in the same order */
2933 list_for_each_entry(wl, &px->req_add, list) {
2934 if (wl->cond) {
2935 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2936 ret = acl_pass(ret);
2937 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2938 ret = !ret;
2939 if (!ret)
2940 continue;
2941 }
2942
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002943 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002944 goto return_bad_req;
2945 }
2946
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002947 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002948 struct stats_admin_rule *stats_admin_rule;
2949
2950 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002951 * FIXME!!! that one is rather dangerous, we want to
2952 * make it follow standard rules (eg: clear req->analysers).
2953 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002954
Cyril Bonté474be412010-10-12 00:14:36 +02002955 /* now check whether we have some admin rules for this request */
2956 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2957 int ret = 1;
2958
2959 if (stats_admin_rule->cond) {
2960 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2961 ret = acl_pass(ret);
2962 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2963 ret = !ret;
2964 }
2965
2966 if (ret) {
2967 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002968 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002969 break;
2970 }
2971 }
2972
Cyril Bonté70be45d2010-10-12 00:14:35 +02002973 /* Was the status page requested with a POST ? */
2974 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002975 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002976 if (msg->msg_state < HTTP_MSG_100_SENT) {
2977 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2978 * send an HTTP/1.1 100 Continue intermediate response.
2979 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002980 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002981 struct hdr_ctx ctx;
2982 ctx.idx = 0;
2983 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002984 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002985 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2986 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
2987 }
2988 }
2989 msg->msg_state = HTTP_MSG_100_SENT;
2990 s->logs.tv_request = now; /* update the request timer to reflect full request */
2991 }
Willy Tarreau295a8372011-03-10 11:25:07 +01002992 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002993 /* we need more data */
2994 req->analysers |= an_bit;
2995 buffer_dont_connect(req);
2996 return 0;
2997 }
Cyril Bonté474be412010-10-12 00:14:36 +02002998 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01002999 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003000 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003001 }
3002
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003003 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003004 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003005 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003006 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003007 s->rep->prod->applet.private = s;
3008 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003009 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003010 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3011 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003012
3013 return 0;
3014
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003015 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003016
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003017 /* check whether we have some ACLs set to redirect this request */
3018 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003019 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003020
Willy Tarreauf285f542010-01-03 20:03:03 +01003021 if (rule->cond) {
3022 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3023 ret = acl_pass(ret);
3024 if (rule->cond->pol == ACL_COND_UNLESS)
3025 ret = !ret;
3026 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003027
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003028 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003029 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003030 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003031
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003032 /* build redirect message */
3033 switch(rule->code) {
3034 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003035 msg_fmt = HTTP_303;
3036 break;
3037 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003038 msg_fmt = HTTP_301;
3039 break;
3040 case 302:
3041 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003042 msg_fmt = HTTP_302;
3043 break;
3044 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003045
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003046 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003047 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003048
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003049 switch(rule->type) {
3050 case REDIRECT_TYPE_PREFIX: {
3051 const char *path;
3052 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003053
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 path = http_get_path(txn);
3055 /* build message using path */
3056 if (path) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003057 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 +02003058 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3059 int qs = 0;
3060 while (qs < pathlen) {
3061 if (path[qs] == '?') {
3062 pathlen = qs;
3063 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003064 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003065 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003066 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003067 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003068 } else {
3069 path = "/";
3070 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003071 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003072
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003073 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003074 goto return_bad_req;
3075
3076 /* add prefix. Note that if prefix == "/", we don't want to
3077 * add anything, otherwise it makes it hard for the user to
3078 * configure a self-redirection.
3079 */
3080 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003081 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3082 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003083 }
3084
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003085 /* add path */
3086 memcpy(rdr.str + rdr.len, path, pathlen);
3087 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003088
3089 /* append a slash at the end of the location is needed and missing */
3090 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3091 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3092 if (rdr.len > rdr.size - 5)
3093 goto return_bad_req;
3094 rdr.str[rdr.len] = '/';
3095 rdr.len++;
3096 }
3097
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003098 break;
3099 }
3100 case REDIRECT_TYPE_LOCATION:
3101 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003102 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003103 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003104
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003105 /* add location */
3106 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3107 rdr.len += rule->rdr_len;
3108 break;
3109 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003110
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003111 if (rule->cookie_len) {
3112 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3113 rdr.len += 14;
3114 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3115 rdr.len += rule->cookie_len;
3116 memcpy(rdr.str + rdr.len, "\r\n", 2);
3117 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003118 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003119
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003120 /* add end of headers and the keep-alive/close status.
3121 * We may choose to set keep-alive if the Location begins
3122 * with a slash, because the client will come back to the
3123 * same server.
3124 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003125 txn->status = rule->code;
3126 /* let's log the request time */
3127 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003128
3129 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003130 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3131 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003132 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3133 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3134 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003135 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003136 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3137 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3138 rdr.len += 30;
3139 } else {
3140 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3141 rdr.len += 24;
3142 }
Willy Tarreau75661452010-01-10 10:35:01 +01003143 }
3144 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3145 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003146 buffer_write(req->prod->ob, rdr.str, rdr.len);
3147 /* "eat" the request */
3148 buffer_ignore(req, msg->sov - msg->som);
3149 msg->som = msg->sov;
3150 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003151 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3152 txn->req.msg_state = HTTP_MSG_CLOSED;
3153 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003154 break;
3155 } else {
3156 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003157 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3158 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3159 rdr.len += 29;
3160 } else {
3161 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3162 rdr.len += 23;
3163 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003164 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003165 goto return_prx_cond;
3166 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003167 }
3168 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003169
Willy Tarreau2be39392010-01-03 17:24:51 +01003170 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3171 * If this happens, then the data will not come immediately, so we must
3172 * send all what we have without waiting. Note that due to the small gain
3173 * in waiting for the body of the request, it's easier to simply put the
3174 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3175 * itself once used.
3176 */
3177 req->flags |= BF_SEND_DONTWAIT;
3178
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003179 /* that's OK for us now, let's move on to next analysers */
3180 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003181
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003182 return_bad_req:
3183 /* We centralize bad requests processing here */
3184 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3185 /* we detected a parsing error. We want to archive this request
3186 * in the dedicated proxy area for later troubleshooting.
3187 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003188 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003189 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003190
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003191 txn->req.msg_state = HTTP_MSG_ERROR;
3192 txn->status = 400;
3193 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003194
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003195 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003196 if (s->listener->counters)
3197 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003198
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003199 return_prx_cond:
3200 if (!(s->flags & SN_ERR_MASK))
3201 s->flags |= SN_ERR_PRXCOND;
3202 if (!(s->flags & SN_FINST_MASK))
3203 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003204
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003205 req->analysers = 0;
3206 req->analyse_exp = TICK_ETERNITY;
3207 return 0;
3208}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003209
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003210/* This function performs all the processing enabled for the current request.
3211 * It returns 1 if the processing can continue on next analysers, or zero if it
3212 * needs more data, encounters an error, or wants to immediately abort the
3213 * request. It relies on buffers flags, and updates s->req->analysers.
3214 */
3215int http_process_request(struct session *s, struct buffer *req, int an_bit)
3216{
3217 struct http_txn *txn = &s->txn;
3218 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003219
Willy Tarreau655dce92009-11-08 13:10:58 +01003220 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003221 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003222 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003223 return 0;
3224 }
3225
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003226 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 +02003227 now_ms, __FUNCTION__,
3228 s,
3229 req,
3230 req->rex, req->wex,
3231 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003232 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003233 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003234
Willy Tarreau59234e92008-11-30 23:51:27 +01003235 /*
3236 * Right now, we know that we have processed the entire headers
3237 * and that unwanted requests have been filtered out. We can do
3238 * whatever we want with the remaining request. Also, now we
3239 * may have separate values for ->fe, ->be.
3240 */
Willy Tarreau06619262006-12-17 08:37:22 +01003241
Willy Tarreau59234e92008-11-30 23:51:27 +01003242 /*
3243 * If HTTP PROXY is set we simply get remote server address
3244 * parsing incoming request.
3245 */
3246 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003247 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 +01003248 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003249
Willy Tarreau59234e92008-11-30 23:51:27 +01003250 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003251 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003252 * Note that doing so might move headers in the request, but
3253 * the fields will stay coherent and the URI will not move.
3254 * This should only be performed in the backend.
3255 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003256 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003257 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3258 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003259
Willy Tarreau59234e92008-11-30 23:51:27 +01003260 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003261 * 8: the appsession cookie was looked up very early in 1.2,
3262 * so let's do the same now.
3263 */
3264
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003265 /* It needs to look into the URI unless persistence must be ignored */
3266 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003267 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 +01003268 }
3269
William Lallemanda73203e2012-03-12 12:48:57 +01003270 /* add unique-id if "header-unique-id" is specified */
3271
3272 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3273 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3274
3275 if (s->fe->header_unique_id && s->unique_id) {
3276 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3277 if (ret < 0 || ret > global.tune.bufsize)
3278 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003279 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003280 goto return_bad_req;
3281 }
3282
Cyril Bontéb21570a2009-11-29 20:04:48 +01003283 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003284 * 9: add X-Forwarded-For if either the frontend or the backend
3285 * asks for it.
3286 */
3287 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003288 struct hdr_ctx ctx = { .idx = 0 };
3289
3290 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01003291 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 +02003292 /* The header is set to be added only if none is present
3293 * and we found it, so don't do anything.
3294 */
3295 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003296 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003297 /* Add an X-Forwarded-For header unless the source IP is
3298 * in the 'except' network range.
3299 */
3300 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003301 (((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 +01003302 != s->fe->except_net.s_addr) &&
3303 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003304 (((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 +01003305 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003306 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003307 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003308 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003309
3310 /* Note: we rely on the backend to get the header name to be used for
3311 * x-forwarded-for, because the header is really meant for the backends.
3312 * However, if the backend did not specify any option, we have to rely
3313 * on the frontend's header name.
3314 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003315 if (s->be->fwdfor_hdr_len) {
3316 len = s->be->fwdfor_hdr_len;
3317 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003318 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003319 len = s->fe->fwdfor_hdr_len;
3320 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003321 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003322 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003323
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003324 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003325 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003326 }
3327 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003328 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003329 /* FIXME: for the sake of completeness, we should also support
3330 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003331 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003332 int len;
3333 char pn[INET6_ADDRSTRLEN];
3334 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003335 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003336 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003337
Willy Tarreau59234e92008-11-30 23:51:27 +01003338 /* Note: we rely on the backend to get the header name to be used for
3339 * x-forwarded-for, because the header is really meant for the backends.
3340 * However, if the backend did not specify any option, we have to rely
3341 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003342 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003343 if (s->be->fwdfor_hdr_len) {
3344 len = s->be->fwdfor_hdr_len;
3345 memcpy(trash, s->be->fwdfor_hdr_name, len);
3346 } else {
3347 len = s->fe->fwdfor_hdr_len;
3348 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003349 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003350 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003351
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003352 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003353 goto return_bad_req;
3354 }
3355 }
3356
3357 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003358 * 10: add X-Original-To if either the frontend or the backend
3359 * asks for it.
3360 */
3361 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3362
3363 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003364 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003365 /* Add an X-Original-To header unless the destination IP is
3366 * in the 'except' network range.
3367 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003368 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003369
Willy Tarreau6471afb2011-09-23 10:54:59 +02003370 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003371 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003372 (((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 +02003373 != s->fe->except_to.s_addr) &&
3374 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003375 (((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 +02003376 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003377 int len;
3378 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003379 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003380
3381 /* Note: we rely on the backend to get the header name to be used for
3382 * x-original-to, because the header is really meant for the backends.
3383 * However, if the backend did not specify any option, we have to rely
3384 * on the frontend's header name.
3385 */
3386 if (s->be->orgto_hdr_len) {
3387 len = s->be->orgto_hdr_len;
3388 memcpy(trash, s->be->orgto_hdr_name, len);
3389 } else {
3390 len = s->fe->orgto_hdr_len;
3391 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003392 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003393 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3394
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003395 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003396 goto return_bad_req;
3397 }
3398 }
3399 }
3400
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003401 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3402 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003403 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003404 unsigned int want_flags = 0;
3405
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003406 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003407 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3408 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3409 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003410 want_flags |= TX_CON_CLO_SET;
3411 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003412 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3413 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3414 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003415 want_flags |= TX_CON_KAL_SET;
3416 }
3417
3418 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003419 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003420 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003421
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003422
Willy Tarreau522d6c02009-12-06 18:49:18 +01003423 /* If we have no server assigned yet and we're balancing on url_param
3424 * with a POST request, we may be interested in checking the body for
3425 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003426 */
3427 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3428 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003429 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003430 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003431 buffer_dont_connect(req);
3432 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003433 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003434
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003435 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003436 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003437#ifdef TCP_QUICKACK
3438 /* We expect some data from the client. Unless we know for sure
3439 * we already have a full request, we have to re-enable quick-ack
3440 * in case we previously disabled it, otherwise we might cause
3441 * the client to delay further data.
3442 */
3443 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003444 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003445 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003446 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3447#endif
3448 }
Willy Tarreau03945942009-12-22 16:50:27 +01003449
Willy Tarreau59234e92008-11-30 23:51:27 +01003450 /*************************************************************
3451 * OK, that's finished for the headers. We have done what we *
3452 * could. Let's switch to the DATA state. *
3453 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003454 req->analyse_exp = TICK_ETERNITY;
3455 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003456
Willy Tarreau59234e92008-11-30 23:51:27 +01003457 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003458 /* OK let's go on with the BODY now */
3459 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003460
Willy Tarreau59234e92008-11-30 23:51:27 +01003461 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003462 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003463 /* we detected a parsing error. We want to archive this request
3464 * in the dedicated proxy area for later troubleshooting.
3465 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003466 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003467 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003468
Willy Tarreau59234e92008-11-30 23:51:27 +01003469 txn->req.msg_state = HTTP_MSG_ERROR;
3470 txn->status = 400;
3471 req->analysers = 0;
3472 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003473
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003474 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003475 if (s->listener->counters)
3476 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003477
Willy Tarreau59234e92008-11-30 23:51:27 +01003478 if (!(s->flags & SN_ERR_MASK))
3479 s->flags |= SN_ERR_PRXCOND;
3480 if (!(s->flags & SN_FINST_MASK))
3481 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003482 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003483}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003484
Willy Tarreau60b85b02008-11-30 23:28:40 +01003485/* This function is an analyser which processes the HTTP tarpit. It always
3486 * returns zero, at the beginning because it prevents any other processing
3487 * from occurring, and at the end because it terminates the request.
3488 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003489int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003490{
3491 struct http_txn *txn = &s->txn;
3492
3493 /* This connection is being tarpitted. The CLIENT side has
3494 * already set the connect expiration date to the right
3495 * timeout. We just have to check that the client is still
3496 * there and that the timeout has not expired.
3497 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003498 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003499 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3500 !tick_is_expired(req->analyse_exp, now_ms))
3501 return 0;
3502
3503 /* We will set the queue timer to the time spent, just for
3504 * logging purposes. We fake a 500 server error, so that the
3505 * attacker will not suspect his connection has been tarpitted.
3506 * It will not cause trouble to the logs because we can exclude
3507 * the tarpitted connections by filtering on the 'PT' status flags.
3508 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003509 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3510
3511 txn->status = 500;
3512 if (req->flags != BF_READ_ERROR)
3513 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3514
3515 req->analysers = 0;
3516 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003517
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003518 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003519 if (s->listener->counters)
3520 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003521
Willy Tarreau60b85b02008-11-30 23:28:40 +01003522 if (!(s->flags & SN_ERR_MASK))
3523 s->flags |= SN_ERR_PRXCOND;
3524 if (!(s->flags & SN_FINST_MASK))
3525 s->flags |= SN_FINST_T;
3526 return 0;
3527}
3528
Willy Tarreaud34af782008-11-30 23:36:37 +01003529/* This function is an analyser which processes the HTTP request body. It looks
3530 * for parameters to be used for the load balancing algorithm (url_param). It
3531 * must only be called after the standard HTTP request processing has occurred,
3532 * because it expects the request to be parsed. It returns zero if it needs to
3533 * read more data, or 1 once it has completed its analysis.
3534 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003535int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003536{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003537 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003538 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003539 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003540
3541 /* We have to parse the HTTP request body to find any required data.
3542 * "balance url_param check_post" should have been the only way to get
3543 * into this. We were brought here after HTTP header analysis, so all
3544 * related structures are ready.
3545 */
3546
Willy Tarreau522d6c02009-12-06 18:49:18 +01003547 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3548 goto missing_data;
3549
3550 if (msg->msg_state < HTTP_MSG_100_SENT) {
3551 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3552 * send an HTTP/1.1 100 Continue intermediate response.
3553 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003554 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003555 struct hdr_ctx ctx;
3556 ctx.idx = 0;
3557 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01003558 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003559 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3560 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3561 }
3562 }
3563 msg->msg_state = HTTP_MSG_100_SENT;
3564 }
3565
3566 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003567 /* we have msg->sov which points to the first byte of message body.
3568 * msg->som still points to the beginning of the message. We must
3569 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003570 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003571 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003572
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003573 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003574 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3575 else
3576 msg->msg_state = HTTP_MSG_DATA;
3577 }
3578
3579 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003580 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003581 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003582 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003583 */
3584 int ret = http_parse_chunk_size(req, msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003585
Willy Tarreau115acb92009-12-26 13:56:06 +01003586 if (!ret)
3587 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003588 else if (ret < 0) {
3589 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003590 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003591 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003592 }
3593
Willy Tarreaud98cf932009-12-27 22:54:55 +01003594 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003595 * We have the first data byte is in msg->sov. We're waiting for at
3596 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003597 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003598
Willy Tarreau124d9912011-03-01 20:30:48 +01003599 if (msg->body_len < limit)
3600 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003601
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003602 if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003603 goto http_end;
3604
3605 missing_data:
3606 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003607 if (req->flags & BF_FULL) {
3608 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003609 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003610 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003611
Willy Tarreau522d6c02009-12-06 18:49:18 +01003612 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3613 txn->status = 408;
3614 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003615
3616 if (!(s->flags & SN_ERR_MASK))
3617 s->flags |= SN_ERR_CLITO;
3618 if (!(s->flags & SN_FINST_MASK))
3619 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003620 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003621 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003622
3623 /* we get here if we need to wait for more data */
3624 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003625 /* Not enough data. We'll re-use the http-request
3626 * timeout here. Ideally, we should set the timeout
3627 * relative to the accept() date. We just set the
3628 * request timeout once at the beginning of the
3629 * request.
3630 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003631 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003632 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003633 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003634 return 0;
3635 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003636
3637 http_end:
3638 /* The situation will not evolve, so let's give up on the analysis. */
3639 s->logs.tv_request = now; /* update the request timer to reflect full request */
3640 req->analysers &= ~an_bit;
3641 req->analyse_exp = TICK_ETERNITY;
3642 return 1;
3643
3644 return_bad_req: /* let's centralize all bad requests */
3645 txn->req.msg_state = HTTP_MSG_ERROR;
3646 txn->status = 400;
3647 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3648
Willy Tarreau79ebac62010-06-07 13:47:49 +02003649 if (!(s->flags & SN_ERR_MASK))
3650 s->flags |= SN_ERR_PRXCOND;
3651 if (!(s->flags & SN_FINST_MASK))
3652 s->flags |= SN_FINST_R;
3653
Willy Tarreau522d6c02009-12-06 18:49:18 +01003654 return_err_msg:
3655 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003656 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003657 if (s->listener->counters)
3658 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003659 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003660}
3661
Willy Tarreau45c0d982012-03-09 12:11:57 +01003662/* send a server's name with an outgoing request over an established connection */
3663int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003664
3665 struct hdr_ctx ctx;
3666
Mark Lamourinec2247f02012-01-04 13:02:01 -05003667 char *hdr_name = be->server_id_hdr_name;
3668 int hdr_name_len = be->server_id_hdr_len;
3669
3670 char *hdr_val;
3671
William Lallemandd9e90662012-01-30 17:27:17 +01003672 ctx.idx = 0;
3673
Willy Tarreau45c0d982012-03-09 12:11:57 +01003674 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 -05003675 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003676 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003677 }
3678
3679 /* Add the new header requested with the server value */
3680 hdr_val = trash;
3681 memcpy(hdr_val, hdr_name, hdr_name_len);
3682 hdr_val += hdr_name_len;
3683 *hdr_val++ = ':';
3684 *hdr_val++ = ' ';
3685 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003686 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003687
3688 return 0;
3689}
3690
Willy Tarreau610ecce2010-01-04 21:15:02 +01003691/* Terminate current transaction and prepare a new one. This is very tricky
3692 * right now but it works.
3693 */
3694void http_end_txn_clean_session(struct session *s)
3695{
3696 /* FIXME: We need a more portable way of releasing a backend's and a
3697 * server's connections. We need a safer way to reinitialize buffer
3698 * flags. We also need a more accurate method for computing per-request
3699 * data.
3700 */
3701 http_silent_debug(__LINE__, s);
3702
3703 s->req->cons->flags |= SI_FL_NOLINGER;
3704 s->req->cons->shutr(s->req->cons);
3705 s->req->cons->shutw(s->req->cons);
3706
3707 http_silent_debug(__LINE__, s);
3708
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003709 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003710 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003711 if (unlikely(s->srv_conn))
3712 sess_change_server(s, NULL);
3713 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003714
3715 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3716 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003717 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003718
3719 if (s->txn.status) {
3720 int n;
3721
3722 n = s->txn.status / 100;
3723 if (n < 1 || n > 5)
3724 n = 0;
3725
3726 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003727 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003728
Willy Tarreau24657792010-02-26 10:30:28 +01003729 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003730 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003731 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003732 }
3733
3734 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003735 s->logs.bytes_in -= s->req->i;
3736 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003737
3738 /* let's do a final log if we need it */
3739 if (s->logs.logwait &&
3740 !(s->flags & SN_MONITOR) &&
3741 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3742 s->do_log(s);
3743 }
3744
3745 s->logs.accept_date = date; /* user-visible date for logging */
3746 s->logs.tv_accept = now; /* corrected date for internal use */
3747 tv_zero(&s->logs.tv_request);
3748 s->logs.t_queue = -1;
3749 s->logs.t_connect = -1;
3750 s->logs.t_data = -1;
3751 s->logs.t_close = 0;
3752 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3753 s->logs.srv_queue_size = 0; /* we will get this number soon */
3754
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003755 s->logs.bytes_in = s->req->total = s->req->i;
3756 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003757
3758 if (s->pend_pos)
3759 pendconn_free(s->pend_pos);
3760
Willy Tarreau827aee92011-03-10 16:55:02 +01003761 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003762 if (s->flags & SN_CURR_SESS) {
3763 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003764 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003765 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003766 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3767 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003768 }
3769
Willy Tarreau9e000c62011-03-10 14:03:36 +01003770 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003771
3772 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3773 s->req->cons->fd = -1; /* just to help with debugging */
3774 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003775 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003776 s->req->cons->err_loc = NULL;
3777 s->req->cons->exp = TICK_ETERNITY;
3778 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003779 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3780 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 +02003781 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 +01003782 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3783 s->txn.meth = 0;
3784 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003785 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003786 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003787 s->req->cons->flags |= SI_FL_INDEP_STR;
3788
Willy Tarreau96e31212011-05-30 18:10:30 +02003789 if (s->fe->options2 & PR_O2_NODELAY) {
3790 s->req->flags |= BF_NEVER_WAIT;
3791 s->rep->flags |= BF_NEVER_WAIT;
3792 }
3793
Willy Tarreau610ecce2010-01-04 21:15:02 +01003794 /* if the request buffer is not empty, it means we're
3795 * about to process another request, so send pending
3796 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003797 * Just don't do this if the buffer is close to be full,
3798 * because the request will wait for it to flush a little
3799 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003800 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003801 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003802 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003803 !(s->rep->flags & BF_FULL) &&
Willy Tarreau363a5bb2012-03-02 20:14:45 +01003804 buffer_wrap_add(s->rep, s->rep->p + s->rep->i) <= s->rep->data + s->rep->size - global.tune.maxrewrite)
Willy Tarreau065e8332010-01-08 00:30:20 +01003805 s->rep->flags |= BF_EXPECT_MORE;
3806 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003807
3808 /* we're removing the analysers, we MUST re-enable events detection */
3809 buffer_auto_read(s->req);
3810 buffer_auto_close(s->req);
3811 buffer_auto_read(s->rep);
3812 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003813
Willy Tarreau342b11c2010-11-24 16:22:09 +01003814 s->req->analysers = s->listener->analysers;
3815 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003816 s->rep->analysers = 0;
3817
3818 http_silent_debug(__LINE__, s);
3819}
3820
3821
3822/* This function updates the request state machine according to the response
3823 * state machine and buffer flags. It returns 1 if it changes anything (flag
3824 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3825 * it is only used to find when a request/response couple is complete. Both
3826 * this function and its equivalent should loop until both return zero. It
3827 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3828 */
3829int http_sync_req_state(struct session *s)
3830{
3831 struct buffer *buf = s->req;
3832 struct http_txn *txn = &s->txn;
3833 unsigned int old_flags = buf->flags;
3834 unsigned int old_state = txn->req.msg_state;
3835
3836 http_silent_debug(__LINE__, s);
3837 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3838 return 0;
3839
3840 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003841 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003842 * We can shut the read side unless we want to abort_on_close,
3843 * or we have a POST request. The issue with POST requests is
3844 * that some browsers still send a CRLF after the request, and
3845 * this CRLF must be read so that it does not remain in the kernel
3846 * buffers, otherwise a close could cause an RST on some systems
3847 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003848 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003849 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003850 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003851
3852 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3853 goto wait_other_side;
3854
3855 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3856 /* The server has not finished to respond, so we
3857 * don't want to move in order not to upset it.
3858 */
3859 goto wait_other_side;
3860 }
3861
3862 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3863 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003864 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003865 txn->req.msg_state = HTTP_MSG_TUNNEL;
3866 goto wait_other_side;
3867 }
3868
3869 /* When we get here, it means that both the request and the
3870 * response have finished receiving. Depending on the connection
3871 * mode, we'll have to wait for the last bytes to leave in either
3872 * direction, and sometimes for a close to be effective.
3873 */
3874
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003875 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3876 /* Server-close mode : queue a connection close to the server */
3877 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003878 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003879 }
3880 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3881 /* Option forceclose is set, or either side wants to close,
3882 * let's enforce it now that we're not expecting any new
3883 * data to come. The caller knows the session is complete
3884 * once both states are CLOSED.
3885 */
3886 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003887 buffer_shutr_now(buf);
3888 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003889 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003890 }
3891 else {
3892 /* The last possible modes are keep-alive and tunnel. Since tunnel
3893 * mode does not set the body analyser, we can't reach this place
3894 * in tunnel mode, so we're left with keep-alive only.
3895 * This mode is currently not implemented, we switch to tunnel mode.
3896 */
3897 buffer_auto_read(buf);
3898 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003899 }
3900
3901 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3902 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003903 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3904
Willy Tarreau610ecce2010-01-04 21:15:02 +01003905 if (!(buf->flags & BF_OUT_EMPTY)) {
3906 txn->req.msg_state = HTTP_MSG_CLOSING;
3907 goto http_msg_closing;
3908 }
3909 else {
3910 txn->req.msg_state = HTTP_MSG_CLOSED;
3911 goto http_msg_closed;
3912 }
3913 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003914 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003915 }
3916
3917 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3918 http_msg_closing:
3919 /* nothing else to forward, just waiting for the output buffer
3920 * to be empty and for the shutw_now to take effect.
3921 */
3922 if (buf->flags & BF_OUT_EMPTY) {
3923 txn->req.msg_state = HTTP_MSG_CLOSED;
3924 goto http_msg_closed;
3925 }
3926 else if (buf->flags & BF_SHUTW) {
3927 txn->req.msg_state = HTTP_MSG_ERROR;
3928 goto wait_other_side;
3929 }
3930 }
3931
3932 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3933 http_msg_closed:
3934 goto wait_other_side;
3935 }
3936
3937 wait_other_side:
3938 http_silent_debug(__LINE__, s);
3939 return txn->req.msg_state != old_state || buf->flags != old_flags;
3940}
3941
3942
3943/* This function updates the response state machine according to the request
3944 * state machine and buffer flags. It returns 1 if it changes anything (flag
3945 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3946 * it is only used to find when a request/response couple is complete. Both
3947 * this function and its equivalent should loop until both return zero. It
3948 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3949 */
3950int http_sync_res_state(struct session *s)
3951{
3952 struct buffer *buf = s->rep;
3953 struct http_txn *txn = &s->txn;
3954 unsigned int old_flags = buf->flags;
3955 unsigned int old_state = txn->rsp.msg_state;
3956
3957 http_silent_debug(__LINE__, s);
3958 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3959 return 0;
3960
3961 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3962 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003963 * still monitor the server connection for a possible close
3964 * while the request is being uploaded, so we don't disable
3965 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003966 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003967 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003968
3969 if (txn->req.msg_state == HTTP_MSG_ERROR)
3970 goto wait_other_side;
3971
3972 if (txn->req.msg_state < HTTP_MSG_DONE) {
3973 /* The client seems to still be sending data, probably
3974 * because we got an error response during an upload.
3975 * We have the choice of either breaking the connection
3976 * or letting it pass through. Let's do the later.
3977 */
3978 goto wait_other_side;
3979 }
3980
3981 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3982 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003983 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003984 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3985 goto wait_other_side;
3986 }
3987
3988 /* When we get here, it means that both the request and the
3989 * response have finished receiving. Depending on the connection
3990 * mode, we'll have to wait for the last bytes to leave in either
3991 * direction, and sometimes for a close to be effective.
3992 */
3993
3994 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3995 /* Server-close mode : shut read and wait for the request
3996 * side to close its output buffer. The caller will detect
3997 * when we're in DONE and the other is in CLOSED and will
3998 * catch that for the final cleanup.
3999 */
4000 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4001 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004002 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004003 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4004 /* Option forceclose is set, or either side wants to close,
4005 * let's enforce it now that we're not expecting any new
4006 * data to come. The caller knows the session is complete
4007 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004008 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004009 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4010 buffer_shutr_now(buf);
4011 buffer_shutw_now(buf);
4012 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013 }
4014 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004015 /* The last possible modes are keep-alive and tunnel. Since tunnel
4016 * mode does not set the body analyser, we can't reach this place
4017 * in tunnel mode, so we're left with keep-alive only.
4018 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004020 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004021 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 }
4023
4024 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4025 /* if we've just closed an output, let's switch */
4026 if (!(buf->flags & BF_OUT_EMPTY)) {
4027 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4028 goto http_msg_closing;
4029 }
4030 else {
4031 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4032 goto http_msg_closed;
4033 }
4034 }
4035 goto wait_other_side;
4036 }
4037
4038 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4039 http_msg_closing:
4040 /* nothing else to forward, just waiting for the output buffer
4041 * to be empty and for the shutw_now to take effect.
4042 */
4043 if (buf->flags & BF_OUT_EMPTY) {
4044 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4045 goto http_msg_closed;
4046 }
4047 else if (buf->flags & BF_SHUTW) {
4048 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004049 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004050 if (target_srv(&s->target))
4051 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004052 goto wait_other_side;
4053 }
4054 }
4055
4056 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4057 http_msg_closed:
4058 /* drop any pending data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004059 buffer_ignore(buf, buf->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004061 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004062 goto wait_other_side;
4063 }
4064
4065 wait_other_side:
4066 http_silent_debug(__LINE__, s);
4067 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4068}
4069
4070
4071/* Resync the request and response state machines. Return 1 if either state
4072 * changes.
4073 */
4074int http_resync_states(struct session *s)
4075{
4076 struct http_txn *txn = &s->txn;
4077 int old_req_state = txn->req.msg_state;
4078 int old_res_state = txn->rsp.msg_state;
4079
4080 http_silent_debug(__LINE__, s);
4081 http_sync_req_state(s);
4082 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004083 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004084 if (!http_sync_res_state(s))
4085 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004086 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004087 if (!http_sync_req_state(s))
4088 break;
4089 }
4090 http_silent_debug(__LINE__, s);
4091 /* OK, both state machines agree on a compatible state.
4092 * There are a few cases we're interested in :
4093 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4094 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4095 * directions, so let's simply disable both analysers.
4096 * - HTTP_MSG_CLOSED on the response only means we must abort the
4097 * request.
4098 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4099 * with server-close mode means we've completed one request and we
4100 * must re-initialize the server connection.
4101 */
4102
4103 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4104 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4105 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4106 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4107 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004108 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004109 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004111 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004112 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004113 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004114 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4115 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004116 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004117 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004118 s->rep->analysers = 0;
4119 buffer_auto_close(s->rep);
4120 buffer_auto_read(s->rep);
4121 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004122 buffer_abort(s->req);
4123 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004124 buffer_auto_read(s->req);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004125 buffer_ignore(s->req, s->req->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004126 }
4127 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4128 txn->rsp.msg_state == HTTP_MSG_DONE &&
4129 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4130 /* server-close: terminate this server connection and
4131 * reinitialize a fresh-new transaction.
4132 */
4133 http_end_txn_clean_session(s);
4134 }
4135
4136 http_silent_debug(__LINE__, s);
4137 return txn->req.msg_state != old_req_state ||
4138 txn->rsp.msg_state != old_res_state;
4139}
4140
Willy Tarreaud98cf932009-12-27 22:54:55 +01004141/* This function is an analyser which forwards request body (including chunk
4142 * sizes if any). It is called as soon as we must forward, even if we forward
4143 * zero byte. The only situation where it must not be called is when we're in
4144 * tunnel mode and we want to forward till the close. It's used both to forward
4145 * remaining data and to resync after end of body. It expects the msg_state to
4146 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4147 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004148 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004149 * bytes of pending data + the headers if not already done (between som and sov).
4150 * It eventually adjusts som to match sov after the data in between have been sent.
4151 */
4152int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4153{
4154 struct http_txn *txn = &s->txn;
4155 struct http_msg *msg = &s->txn.req;
4156
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004157 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4158 return 0;
4159
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004160 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004161 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004162 /* Output closed while we were sending data. We must abort and
4163 * wake the other side up.
4164 */
4165 msg->msg_state = HTTP_MSG_ERROR;
4166 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004167 return 1;
4168 }
4169
Willy Tarreau4fe41902010-06-07 22:27:41 +02004170 /* in most states, we should abort in case of early close */
4171 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004172
4173 /* Note that we don't have to send 100-continue back because we don't
4174 * need the data to complete our job, and it's up to the server to
4175 * decide whether to return 100, 417 or anything else in return of
4176 * an "Expect: 100-continue" header.
4177 */
4178
4179 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004180 /* we have msg->sov which points to the first byte of message body.
4181 * msg->som still points to the beginning of the message. We must
4182 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004183 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004184 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004185
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004186 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004187 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4188 else {
4189 msg->msg_state = HTTP_MSG_DATA;
4190 }
4191 }
4192
Willy Tarreaud98cf932009-12-27 22:54:55 +01004193 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004194 int bytes;
4195
Willy Tarreau610ecce2010-01-04 21:15:02 +01004196 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004197 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004198 bytes = msg->sov - msg->som;
4199 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004200 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004201 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4202 bytes += req->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01004203 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004204 msg->chunk_len += (unsigned int)bytes;
4205 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004206 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004207
Willy Tarreaucaabe412010-01-03 23:08:28 +01004208 if (msg->msg_state == HTTP_MSG_DATA) {
4209 /* must still forward */
4210 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004211 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004212
4213 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004214 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004215 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004216 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004217 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004218 }
4219 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004220 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004221 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004222 * TRAILERS state.
4223 */
4224 int ret = http_parse_chunk_size(req, msg);
4225
4226 if (!ret)
4227 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004228 else if (ret < 0) {
4229 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004230 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004231 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004232 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004233 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004234 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004235 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004236 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4237 /* we want the CRLF after the data */
4238 int ret;
4239
Willy Tarreaud98cf932009-12-27 22:54:55 +01004240 ret = http_skip_chunk_crlf(req, msg);
4241
4242 if (ret == 0)
4243 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004244 else if (ret < 0) {
4245 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004246 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004247 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004248 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004249 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004250 /* we're in MSG_CHUNK_SIZE now */
4251 }
4252 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
4253 int ret = http_forward_trailers(req, msg);
4254
4255 if (ret == 0)
4256 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004257 else if (ret < 0) {
4258 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004259 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004260 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004261 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004262 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004263 /* we're in HTTP_MSG_DONE now */
4264 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004265 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004266 int old_state = msg->msg_state;
4267
Willy Tarreau610ecce2010-01-04 21:15:02 +01004268 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004269 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004270 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4271 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4272 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004273 if (http_resync_states(s)) {
4274 /* some state changes occurred, maybe the analyser
4275 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004276 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004277 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4278 if (req->flags & BF_SHUTW) {
4279 /* request errors are most likely due to
4280 * the server aborting the transfer.
4281 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004282 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004283 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004284 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004285 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004287 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004288 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004289 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004290
4291 /* If "option abortonclose" is set on the backend, we
4292 * want to monitor the client's connection and forward
4293 * any shutdown notification to the server, which will
4294 * decide whether to close or to go on processing the
4295 * request.
4296 */
4297 if (s->be->options & PR_O_ABRT_CLOSE) {
4298 buffer_auto_read(req);
4299 buffer_auto_close(req);
4300 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004301 else if (s->txn.meth == HTTP_METH_POST) {
4302 /* POST requests may require to read extra CRLF
4303 * sent by broken browsers and which could cause
4304 * an RST to be sent upon close on some systems
4305 * (eg: Linux).
4306 */
4307 buffer_auto_read(req);
4308 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004309
Willy Tarreau610ecce2010-01-04 21:15:02 +01004310 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004311 }
4312 }
4313
Willy Tarreaud98cf932009-12-27 22:54:55 +01004314 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004315 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004316 if (req->flags & BF_SHUTR) {
4317 if (!(s->flags & SN_ERR_MASK))
4318 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004319 if (!(s->flags & SN_FINST_MASK)) {
4320 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4321 s->flags |= SN_FINST_H;
4322 else
4323 s->flags |= SN_FINST_D;
4324 }
4325
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004326 s->fe->fe_counters.cli_aborts++;
4327 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004328 if (target_srv(&s->target))
4329 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004330
4331 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004332 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004333
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004334 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004335 if (req->flags & BF_SHUTW)
4336 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004337
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004338 /* When TE: chunked is used, we need to get there again to parse remaining
4339 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4340 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004341 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004342 buffer_dont_close(req);
4343
Willy Tarreau5c620922011-05-11 19:56:11 +02004344 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004345 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4346 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004347 * modes are already handled by the stream sock layer. We must not do
4348 * this in content-length mode because it could present the MSG_MORE
4349 * flag with the last block of forwarded data, which would cause an
4350 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004351 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004352 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004353 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004354
Willy Tarreau610ecce2010-01-04 21:15:02 +01004355 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004356 return 0;
4357
Willy Tarreaud98cf932009-12-27 22:54:55 +01004358 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004359 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004360 if (s->listener->counters)
4361 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004362 return_bad_req_stats_ok:
4363 txn->req.msg_state = HTTP_MSG_ERROR;
4364 if (txn->status) {
4365 /* Note: we don't send any error if some data were already sent */
4366 stream_int_retnclose(req->prod, NULL);
4367 } else {
4368 txn->status = 400;
4369 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4370 }
4371 req->analysers = 0;
4372 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004373
4374 if (!(s->flags & SN_ERR_MASK))
4375 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004376 if (!(s->flags & SN_FINST_MASK)) {
4377 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4378 s->flags |= SN_FINST_H;
4379 else
4380 s->flags |= SN_FINST_D;
4381 }
4382 return 0;
4383
4384 aborted_xfer:
4385 txn->req.msg_state = HTTP_MSG_ERROR;
4386 if (txn->status) {
4387 /* Note: we don't send any error if some data were already sent */
4388 stream_int_retnclose(req->prod, NULL);
4389 } else {
4390 txn->status = 502;
4391 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4392 }
4393 req->analysers = 0;
4394 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4395
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004396 s->fe->fe_counters.srv_aborts++;
4397 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004398 if (target_srv(&s->target))
4399 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004400
4401 if (!(s->flags & SN_ERR_MASK))
4402 s->flags |= SN_ERR_SRVCL;
4403 if (!(s->flags & SN_FINST_MASK)) {
4404 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4405 s->flags |= SN_FINST_H;
4406 else
4407 s->flags |= SN_FINST_D;
4408 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004409 return 0;
4410}
4411
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004412/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4413 * processing can continue on next analysers, or zero if it either needs more
4414 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4415 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4416 * when it has nothing left to do, and may remove any analyser when it wants to
4417 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004418 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004419int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004420{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004421 struct http_txn *txn = &s->txn;
4422 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004423 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004424 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004425 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004426 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004427
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004428 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 +02004429 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004430 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004431 rep,
4432 rep->rex, rep->wex,
4433 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004434 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004435 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004436
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004437 /*
4438 * Now parse the partial (or complete) lines.
4439 * We will check the response syntax, and also join multi-line
4440 * headers. An index of all the lines will be elaborated while
4441 * parsing.
4442 *
4443 * For the parsing, we use a 28 states FSM.
4444 *
4445 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004446 * rep->data + msg->som = beginning of response
4447 * rep->data + msg->eoh = end of processed headers / start of current one
4448 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01004449 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004450 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004451 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004452 */
4453
Willy Tarreau83e3af02009-12-28 17:39:57 +01004454 /* There's a protected area at the end of the buffer for rewriting
4455 * purposes. We don't want to start to parse the request if the
4456 * protected area is affected, because we may have to move processed
4457 * data later, which is much more complicated.
4458 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004459 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004460 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01004461 buffer_wrap_add(rep, rep->p + rep->i) < buffer_wrap_add(rep, rep->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01004462 buffer_wrap_add(rep, rep->p + rep->i) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004463 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004464 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004465 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4466 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004467 buffer_dont_close(rep);
4468 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4469 return 0;
4470 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004471 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004472 http_buffer_heavy_realign(rep, msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004473 }
4474
Willy Tarreaua458b672012-03-05 11:17:50 +01004475 if (likely(msg->next < rep->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004476 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004477 }
4478
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004479 /* 1: we might have to print this header in debug mode */
4480 if (unlikely((global.mode & MODE_DEBUG) &&
4481 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004482 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004483 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004484 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004485
Willy Tarreauea1175a2012-03-05 15:52:30 +01004486 sol = rep->p + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004487 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004488 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004489
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004490 sol += hdr_idx_first_pos(&txn->hdr_idx);
4491 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004492
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004493 while (cur_idx) {
4494 eol = sol + txn->hdr_idx.v[cur_idx].len;
4495 debug_hdr("srvhdr", s, sol, eol);
4496 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4497 cur_idx = txn->hdr_idx.v[cur_idx].next;
4498 }
4499 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004500
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004501 /*
4502 * Now we quickly check if we have found a full valid response.
4503 * If not so, we check the FD and buffer states before leaving.
4504 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004505 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004506 * responses are checked first.
4507 *
4508 * Depending on whether the client is still there or not, we
4509 * may send an error response back or not. Note that normally
4510 * we should only check for HTTP status there, and check I/O
4511 * errors somewhere else.
4512 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004513
Willy Tarreau655dce92009-11-08 13:10:58 +01004514 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004515 /* Invalid response */
4516 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4517 /* we detected a parsing error. We want to archive this response
4518 * in the dedicated proxy area for later troubleshooting.
4519 */
4520 hdr_response_bad:
4521 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004522 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004523
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004524 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004525 if (target_srv(&s->target)) {
4526 target_srv(&s->target)->counters.failed_resp++;
4527 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004528 }
Willy Tarreau64648412010-03-05 10:41:54 +01004529 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004530 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004531 rep->analysers = 0;
4532 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004533 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004534 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004535 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4536
4537 if (!(s->flags & SN_ERR_MASK))
4538 s->flags |= SN_ERR_PRXCOND;
4539 if (!(s->flags & SN_FINST_MASK))
4540 s->flags |= SN_FINST_H;
4541
4542 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004543 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004544
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004545 /* too large response does not fit in buffer. */
4546 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004547 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004548 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004549 goto hdr_response_bad;
4550 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004551
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004552 /* read error */
4553 else if (rep->flags & BF_READ_ERROR) {
4554 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004555 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004556
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004557 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004558 if (target_srv(&s->target)) {
4559 target_srv(&s->target)->counters.failed_resp++;
4560 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004561 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004562
Willy Tarreau90deb182010-01-07 00:20:41 +01004563 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004564 rep->analysers = 0;
4565 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004566 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004567 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004568 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004569
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004570 if (!(s->flags & SN_ERR_MASK))
4571 s->flags |= SN_ERR_SRVCL;
4572 if (!(s->flags & SN_FINST_MASK))
4573 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004574 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004575 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004576
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004577 /* read timeout : return a 504 to the client. */
4578 else if (rep->flags & BF_READ_TIMEOUT) {
4579 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004580 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004581
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004582 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004583 if (target_srv(&s->target)) {
4584 target_srv(&s->target)->counters.failed_resp++;
4585 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004586 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004587
Willy Tarreau90deb182010-01-07 00:20:41 +01004588 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004589 rep->analysers = 0;
4590 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004591 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004592 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004593 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004594
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004595 if (!(s->flags & SN_ERR_MASK))
4596 s->flags |= SN_ERR_SRVTO;
4597 if (!(s->flags & SN_FINST_MASK))
4598 s->flags |= SN_FINST_H;
4599 return 0;
4600 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004601
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004602 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004603 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004604 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004605 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004606
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004607 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004608 if (target_srv(&s->target)) {
4609 target_srv(&s->target)->counters.failed_resp++;
4610 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004611 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004612
Willy Tarreau90deb182010-01-07 00:20:41 +01004613 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004614 rep->analysers = 0;
4615 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004616 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004617 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004618 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004619
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004620 if (!(s->flags & SN_ERR_MASK))
4621 s->flags |= SN_ERR_SRVCL;
4622 if (!(s->flags & SN_FINST_MASK))
4623 s->flags |= SN_FINST_H;
4624 return 0;
4625 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004626
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004627 /* write error to client (we don't send any message then) */
4628 else if (rep->flags & BF_WRITE_ERROR) {
4629 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004630 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004631
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004632 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004633 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004634 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004635
4636 if (!(s->flags & SN_ERR_MASK))
4637 s->flags |= SN_ERR_CLICL;
4638 if (!(s->flags & SN_FINST_MASK))
4639 s->flags |= SN_FINST_H;
4640
4641 /* process_session() will take care of the error */
4642 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004643 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004644
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004645 buffer_dont_close(rep);
4646 return 0;
4647 }
4648
4649 /* More interesting part now : we know that we have a complete
4650 * response which at least looks like HTTP. We have an indicator
4651 * of each header's length, so we can parse them quickly.
4652 */
4653
4654 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004655 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004656
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004657 /*
4658 * 1: get the status code
4659 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01004660 n = rep->p[msg->sol + msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004661 if (n < 1 || n > 5)
4662 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004663 /* when the client triggers a 4xx from the server, it's most often due
4664 * to a missing object or permission. These events should be tracked
4665 * because if they happen often, it may indicate a brute force or a
4666 * vulnerability scan.
4667 */
4668 if (n == 4)
4669 session_inc_http_err_ctr(s);
4670
Willy Tarreau827aee92011-03-10 16:55:02 +01004671 if (target_srv(&s->target))
4672 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004673
Willy Tarreau5b154472009-12-21 20:11:07 +01004674 /* check if the response is HTTP/1.1 or above */
4675 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004676 ((rep->p[msg->sol + 5] > '1') ||
4677 ((rep->p[msg->sol + 5] == '1') &&
4678 (rep->p[msg->sol + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004679 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004680
4681 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004682 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 +01004683
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004684 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004685 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004686
Willy Tarreau3a215be2012-03-09 21:39:51 +01004687 txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004688
Willy Tarreau39650402010-03-15 19:44:39 +01004689 /* Adjust server's health based on status code. Note: status codes 501
4690 * and 505 are triggered on demand by client request, so we must not
4691 * count them as server failures.
4692 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004693 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004694 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004695 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004696 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004697 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004698 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004699
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004700 /*
4701 * 2: check for cacheability.
4702 */
4703
4704 switch (txn->status) {
4705 case 200:
4706 case 203:
4707 case 206:
4708 case 300:
4709 case 301:
4710 case 410:
4711 /* RFC2616 @13.4:
4712 * "A response received with a status code of
4713 * 200, 203, 206, 300, 301 or 410 MAY be stored
4714 * by a cache (...) unless a cache-control
4715 * directive prohibits caching."
4716 *
4717 * RFC2616 @9.5: POST method :
4718 * "Responses to this method are not cacheable,
4719 * unless the response includes appropriate
4720 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004721 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004722 if (likely(txn->meth != HTTP_METH_POST) &&
4723 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4724 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4725 break;
4726 default:
4727 break;
4728 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004729
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004730 /*
4731 * 3: we may need to capture headers
4732 */
4733 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004734 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01004735 capture_headers(rep->p + msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004736 txn->rsp.cap, s->fe->rsp_cap);
4737
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004738 /* 4: determine the transfer-length.
4739 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4740 * the presence of a message-body in a RESPONSE and its transfer length
4741 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004742 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004743 * All responses to the HEAD request method MUST NOT include a
4744 * message-body, even though the presence of entity-header fields
4745 * might lead one to believe they do. All 1xx (informational), 204
4746 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4747 * message-body. All other responses do include a message-body,
4748 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004749 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004750 * 1. Any response which "MUST NOT" include a message-body (such as the
4751 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4752 * always terminated by the first empty line after the header fields,
4753 * regardless of the entity-header fields present in the message.
4754 *
4755 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4756 * the "chunked" transfer-coding (Section 6.2) is used, the
4757 * transfer-length is defined by the use of this transfer-coding.
4758 * If a Transfer-Encoding header field is present and the "chunked"
4759 * transfer-coding is not present, the transfer-length is defined by
4760 * the sender closing the connection.
4761 *
4762 * 3. If a Content-Length header field is present, its decimal value in
4763 * OCTETs represents both the entity-length and the transfer-length.
4764 * If a message is received with both a Transfer-Encoding header
4765 * field and a Content-Length header field, the latter MUST be ignored.
4766 *
4767 * 4. If the message uses the media type "multipart/byteranges", and
4768 * the transfer-length is not otherwise specified, then this self-
4769 * delimiting media type defines the transfer-length. This media
4770 * type MUST NOT be used unless the sender knows that the recipient
4771 * can parse it; the presence in a request of a Range header with
4772 * multiple byte-range specifiers from a 1.1 client implies that the
4773 * client can parse multipart/byteranges responses.
4774 *
4775 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004776 */
4777
4778 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004779 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004780 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004781 * FIXME: should we parse anyway and return an error on chunked encoding ?
4782 */
4783 if (txn->meth == HTTP_METH_HEAD ||
4784 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004785 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004786 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004787 goto skip_content_length;
4788 }
4789
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004790 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004791 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004792 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004793 http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004794 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004795 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4796 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004797 /* bad transfer-encoding (chunked followed by something else) */
4798 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004799 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004800 break;
4801 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004802 }
4803
4804 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4805 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004806 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004807 http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004808 signed long long cl;
4809
Willy Tarreauad14f752011-09-02 20:33:27 +02004810 if (!ctx.vlen) {
4811 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004812 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004813 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004814
Willy Tarreauad14f752011-09-02 20:33:27 +02004815 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4816 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004817 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004818 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004819
Willy Tarreauad14f752011-09-02 20:33:27 +02004820 if (cl < 0) {
4821 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004822 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004823 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004824
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004825 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02004826 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004827 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004828 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004829
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004830 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004831 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004832 }
4833
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004834 /* FIXME: we should also implement the multipart/byterange method.
4835 * For now on, we resort to close mode in this case (unknown length).
4836 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004837skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004838
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004839 /* end of job, return OK */
4840 rep->analysers &= ~an_bit;
4841 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004842 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004843 return 1;
4844}
4845
4846/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004847 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4848 * and updates t->rep->analysers. It might make sense to explode it into several
4849 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004850 */
4851int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4852{
4853 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004854 struct http_msg *msg = &txn->rsp;
4855 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004856 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004857
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004858 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 +02004859 now_ms, __FUNCTION__,
4860 t,
4861 rep,
4862 rep->rex, rep->wex,
4863 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004864 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004865 rep->analysers);
4866
Willy Tarreau655dce92009-11-08 13:10:58 +01004867 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004868 return 0;
4869
4870 rep->analysers &= ~an_bit;
4871 rep->analyse_exp = TICK_ETERNITY;
4872
Willy Tarreau5b154472009-12-21 20:11:07 +01004873 /* Now we have to check if we need to modify the Connection header.
4874 * This is more difficult on the response than it is on the request,
4875 * because we can have two different HTTP versions and we don't know
4876 * how the client will interprete a response. For instance, let's say
4877 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4878 * HTTP/1.1 response without any header. Maybe it will bound itself to
4879 * HTTP/1.0 because it only knows about it, and will consider the lack
4880 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4881 * the lack of header as a keep-alive. Thus we will use two flags
4882 * indicating how a request MAY be understood by the client. In case
4883 * of multiple possibilities, we'll fix the header to be explicit. If
4884 * ambiguous cases such as both close and keepalive are seen, then we
4885 * will fall back to explicit close. Note that we won't take risks with
4886 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004887 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004888 */
4889
Willy Tarreaudc008c52010-02-01 16:20:08 +01004890 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4891 txn->status == 101)) {
4892 /* Either we've established an explicit tunnel, or we're
4893 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004894 * to understand the next protocols. We have to switch to tunnel
4895 * mode, so that we transfer the request and responses then let
4896 * this protocol pass unmodified. When we later implement specific
4897 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004898 * header which contains information about that protocol for
4899 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004900 */
4901 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4902 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004903 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4904 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4905 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004906 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004907
Willy Tarreau60466522010-01-18 19:08:45 +01004908 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004909 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004910 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4911 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004912
Willy Tarreau60466522010-01-18 19:08:45 +01004913 /* now adjust header transformations depending on current state */
4914 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4915 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4916 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004917 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004918 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004919 }
Willy Tarreau60466522010-01-18 19:08:45 +01004920 else { /* SCL / KAL */
4921 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004922 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004923 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004924 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004925
Willy Tarreau60466522010-01-18 19:08:45 +01004926 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004927 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004928
Willy Tarreau60466522010-01-18 19:08:45 +01004929 /* Some keep-alive responses are converted to Server-close if
4930 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004931 */
Willy Tarreau60466522010-01-18 19:08:45 +01004932 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4933 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004934 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004935 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004936 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004937 }
4938
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004939 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004940 /*
4941 * 3: we will have to evaluate the filters.
4942 * As opposed to version 1.2, now they will be evaluated in the
4943 * filters order and not in the header order. This means that
4944 * each filter has to be validated among all headers.
4945 *
4946 * Filters are tried with ->be first, then with ->fe if it is
4947 * different from ->be.
4948 */
4949
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004950 cur_proxy = t->be;
4951 while (1) {
4952 struct proxy *rule_set = cur_proxy;
4953
4954 /* try headers filters */
4955 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004956 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004957 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004958 if (target_srv(&t->target)) {
4959 target_srv(&t->target)->counters.failed_resp++;
4960 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004961 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004962 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004963 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004964 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004965 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004966 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004967 buffer_ignore(rep, rep->i);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004968 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004969 if (!(t->flags & SN_ERR_MASK))
4970 t->flags |= SN_ERR_PRXCOND;
4971 if (!(t->flags & SN_FINST_MASK))
4972 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004973 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004974 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004975 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004976
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004977 /* has the response been denied ? */
4978 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004979 if (target_srv(&t->target))
4980 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004981
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004982 t->be->be_counters.denied_resp++;
4983 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004984 if (t->listener->counters)
4985 t->listener->counters->denied_resp++;
4986
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004987 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004988 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004989
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004990 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01004991 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02004992 if (txn->status < 200)
4993 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004994 if (wl->cond) {
4995 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
4996 ret = acl_pass(ret);
4997 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
4998 ret = !ret;
4999 if (!ret)
5000 continue;
5001 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005002 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005003 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005004 }
5005
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005006 /* check whether we're already working on the frontend */
5007 if (cur_proxy == t->fe)
5008 break;
5009 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005010 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005011
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005012 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005013 * We may be facing a 100-continue response, in which case this
5014 * is not the right response, and we're waiting for the next one.
5015 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005016 * next one.
5017 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005018 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005019 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau3a215be2012-03-09 21:39:51 +01005020 msg->next -= buffer_forward(rep, msg->next - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005021 msg->msg_state = HTTP_MSG_RPBEFORE;
5022 txn->status = 0;
5023 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5024 return 1;
5025 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005026 else if (unlikely(txn->status < 200))
5027 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005028
5029 /* we don't have any 1xx status code now */
5030
5031 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005032 * 4: check for server cookie.
5033 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005034 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5035 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005036 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005037
Willy Tarreaubaaee002006-06-26 02:48:02 +02005038
Willy Tarreaua15645d2007-03-18 16:22:39 +01005039 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005040 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005041 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005042 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005043 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005044
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005045 /*
5046 * 6: add server cookie in the response if needed
5047 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005048 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005049 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005050 (!(t->flags & SN_DIRECT) ||
5051 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5052 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5053 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5054 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005055 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5056 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005057 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005058 /* the server is known, it's not the one the client requested, or the
5059 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005060 * insert a set-cookie here, except if we want to insert only on POST
5061 * requests and this one isn't. Note that servers which don't have cookies
5062 * (eg: some backup servers) will return a full cookie removal request.
5063 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005064 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005065 len = sprintf(trash,
5066 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5067 t->be->cookie_name);
5068 }
5069 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005070 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005071
5072 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5073 /* emit last_date, which is mandatory */
5074 trash[len++] = COOKIE_DELIM_DATE;
5075 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5076 if (t->be->cookie_maxlife) {
5077 /* emit first_date, which is either the original one or
5078 * the current date.
5079 */
5080 trash[len++] = COOKIE_DELIM_DATE;
5081 s30tob64(txn->cookie_first_date ?
5082 txn->cookie_first_date >> 2 :
5083 (date.tv_sec+3) >> 2, trash + len);
5084 len += 5;
5085 }
5086 }
5087 len += sprintf(trash + len, "; path=/");
5088 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005089
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005090 if (t->be->cookie_domain)
5091 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005092
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005093 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005094 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005095
Willy Tarreauf1348312010-10-07 15:54:11 +02005096 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005097 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005098 /* the server did not change, only the date was updated */
5099 txn->flags |= TX_SCK_UPDATED;
5100 else
5101 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005102
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005103 /* Here, we will tell an eventual cache on the client side that we don't
5104 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5105 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5106 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5107 */
5108 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005109
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005110 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5111
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005112 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005113 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005114 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005115 }
5116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005117
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005118 /*
5119 * 7: check if result will be cacheable with a cookie.
5120 * We'll block the response if security checks have caught
5121 * nasty things such as a cacheable cookie.
5122 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005123 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5124 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005125 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005126
5127 /* we're in presence of a cacheable response containing
5128 * a set-cookie header. We'll block it as requested by
5129 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005130 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005131 if (target_srv(&t->target))
5132 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005133
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005134 t->be->be_counters.denied_resp++;
5135 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005136 if (t->listener->counters)
5137 t->listener->counters->denied_resp++;
5138
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005139 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005140 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005141 send_log(t->be, LOG_ALERT,
5142 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005143 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005144 goto return_srv_prx_502;
5145 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005146
5147 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005148 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005149 */
Willy Tarreau60466522010-01-18 19:08:45 +01005150 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5151 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5152 unsigned int want_flags = 0;
5153
5154 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5155 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5156 /* we want a keep-alive response here. Keep-alive header
5157 * required if either side is not 1.1.
5158 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005159 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005160 want_flags |= TX_CON_KAL_SET;
5161 }
5162 else {
5163 /* we want a close response here. Close header required if
5164 * the server is 1.1, regardless of the client.
5165 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005166 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005167 want_flags |= TX_CON_CLO_SET;
5168 }
5169
5170 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005171 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005172 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005173
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005174 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005175 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005176 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005177 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005178
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005179 /*************************************************************
5180 * OK, that's finished for the headers. We have done what we *
5181 * could. Let's switch to the DATA state. *
5182 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005183
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005184 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005185
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005186 /* if the user wants to log as soon as possible, without counting
5187 * bytes from the server, then this is the right moment. We have
5188 * to temporarily assign bytes_out to log what we currently have.
5189 */
5190 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5191 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5192 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005193 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005194 t->logs.bytes_out = 0;
5195 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005196
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005197 /* Note: we must not try to cheat by jumping directly to DATA,
5198 * otherwise we would not let the client side wake up.
5199 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005200
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005201 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005202 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005203 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005204}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005205
Willy Tarreaud98cf932009-12-27 22:54:55 +01005206/* This function is an analyser which forwards response body (including chunk
5207 * sizes if any). It is called as soon as we must forward, even if we forward
5208 * zero byte. The only situation where it must not be called is when we're in
5209 * tunnel mode and we want to forward till the close. It's used both to forward
5210 * remaining data and to resync after end of body. It expects the msg_state to
5211 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5212 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005213 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005214 * bytes of pending data + the headers if not already done (between som and sov).
5215 * It eventually adjusts som to match sov after the data in between have been sent.
5216 */
5217int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5218{
5219 struct http_txn *txn = &s->txn;
5220 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005221 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005222
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005223 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5224 return 0;
5225
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005226 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005227 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005228 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005229 /* Output closed while we were sending data. We must abort and
5230 * wake the other side up.
5231 */
5232 msg->msg_state = HTTP_MSG_ERROR;
5233 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005234 return 1;
5235 }
5236
Willy Tarreau4fe41902010-06-07 22:27:41 +02005237 /* in most states, we should abort in case of early close */
5238 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005239
Willy Tarreaud98cf932009-12-27 22:54:55 +01005240 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005241 /* we have msg->sov which points to the first byte of message body.
5242 * msg->som still points to the beginning of the message. We must
5243 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005244 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005245 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005246
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005247 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005248 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5249 else {
5250 msg->msg_state = HTTP_MSG_DATA;
5251 }
5252 }
5253
Willy Tarreaud98cf932009-12-27 22:54:55 +01005254 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005255 int bytes;
5256
Willy Tarreau610ecce2010-01-04 21:15:02 +01005257 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005258 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005259 bytes = msg->sov - msg->som;
5260 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005261 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005262 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5263 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005264 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005265 msg->chunk_len += (unsigned int)bytes;
5266 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005267 }
5268
Willy Tarreaucaabe412010-01-03 23:08:28 +01005269 if (msg->msg_state == HTTP_MSG_DATA) {
5270 /* must still forward */
5271 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005272 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005273
5274 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005275 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005276 msg->msg_state = HTTP_MSG_DATA_CRLF;
5277 else
5278 msg->msg_state = HTTP_MSG_DONE;
5279 }
5280 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005281 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005282 * set ->sov and ->next to point to the body and switch to DATA or
5283 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005284 */
5285 int ret = http_parse_chunk_size(res, msg);
5286
5287 if (!ret)
5288 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005289 else if (ret < 0) {
5290 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005291 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005292 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005293 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005294 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005295 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005296 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5297 /* we want the CRLF after the data */
5298 int ret;
5299
Willy Tarreaud98cf932009-12-27 22:54:55 +01005300 ret = http_skip_chunk_crlf(res, msg);
5301
5302 if (!ret)
5303 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005304 else if (ret < 0) {
5305 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005306 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005307 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005308 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005309 /* we're in MSG_CHUNK_SIZE now */
5310 }
5311 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
5312 int ret = http_forward_trailers(res, msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005313
Willy Tarreaud98cf932009-12-27 22:54:55 +01005314 if (ret == 0)
5315 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005316 else if (ret < 0) {
5317 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005318 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005319 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005320 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005321 /* we're in HTTP_MSG_DONE now */
5322 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005323 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005324 int old_state = msg->msg_state;
5325
Willy Tarreau610ecce2010-01-04 21:15:02 +01005326 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005327 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005328 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5329 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5330 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005331 if (http_resync_states(s)) {
5332 http_silent_debug(__LINE__, s);
5333 /* some state changes occurred, maybe the analyser
5334 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005335 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005336 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5337 if (res->flags & BF_SHUTW) {
5338 /* response errors are most likely due to
5339 * the client aborting the transfer.
5340 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005341 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005342 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005343 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005344 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005345 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005346 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005347 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005348 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005349 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005350 }
5351 }
5352
Willy Tarreaud98cf932009-12-27 22:54:55 +01005353 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005354 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005355 if (res->flags & BF_SHUTR) {
5356 if (!(s->flags & SN_ERR_MASK))
5357 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005358 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005359 if (target_srv(&s->target))
5360 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005361 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005362 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005363
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005364 if (res->flags & BF_SHUTW)
5365 goto aborted_xfer;
5366
Willy Tarreau40dba092010-03-04 18:14:51 +01005367 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005368 if (!s->req->analysers)
5369 goto return_bad_res;
5370
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005371 /* forward any pending data */
5372 bytes = msg->sov - msg->som;
5373 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005374 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005375 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5376 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005377 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005378 msg->chunk_len += (unsigned int)bytes;
5379 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005380 }
5381
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005382 /* When TE: chunked is used, we need to get there again to parse remaining
5383 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5384 * Similarly, with keep-alive on the client side, we don't want to forward a
5385 * close.
5386 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005387 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005388 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5389 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5390 buffer_dont_close(res);
5391
Willy Tarreau5c620922011-05-11 19:56:11 +02005392 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005393 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5394 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005395 * modes are already handled by the stream sock layer. We must not do
5396 * this in content-length mode because it could present the MSG_MORE
5397 * flag with the last block of forwarded data, which would cause an
5398 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005399 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005400 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005401 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005402
Willy Tarreaud98cf932009-12-27 22:54:55 +01005403 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005404 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005405 return 0;
5406
Willy Tarreau40dba092010-03-04 18:14:51 +01005407 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005408 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005409 if (target_srv(&s->target))
5410 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005411
5412 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005413 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005414 /* don't send any error message as we're in the body */
5415 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005416 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005417 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005418 if (target_srv(&s->target))
5419 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005420
5421 if (!(s->flags & SN_ERR_MASK))
5422 s->flags |= SN_ERR_PRXCOND;
5423 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005424 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005425 return 0;
5426
5427 aborted_xfer:
5428 txn->rsp.msg_state = HTTP_MSG_ERROR;
5429 /* don't send any error message as we're in the body */
5430 stream_int_retnclose(res->cons, NULL);
5431 res->analysers = 0;
5432 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5433
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005434 s->fe->fe_counters.cli_aborts++;
5435 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005436 if (target_srv(&s->target))
5437 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005438
5439 if (!(s->flags & SN_ERR_MASK))
5440 s->flags |= SN_ERR_CLICL;
5441 if (!(s->flags & SN_FINST_MASK))
5442 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005443 return 0;
5444}
5445
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005446/* Iterate the same filter through all request headers.
5447 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005448 * Since it can manage the switch to another backend, it updates the per-proxy
5449 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005450 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005451int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005452{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005453 char term;
5454 char *cur_ptr, *cur_end, *cur_next;
5455 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005456 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005457 struct hdr_idx_elem *cur_hdr;
5458 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005459
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005460 last_hdr = 0;
5461
Willy Tarreau3a215be2012-03-09 21:39:51 +01005462 cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005463 old_idx = 0;
5464
5465 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005466 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005467 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005468 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005469 (exp->action == ACT_ALLOW ||
5470 exp->action == ACT_DENY ||
5471 exp->action == ACT_TARPIT))
5472 return 0;
5473
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005474 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005475 if (!cur_idx)
5476 break;
5477
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005478 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005479 cur_ptr = cur_next;
5480 cur_end = cur_ptr + cur_hdr->len;
5481 cur_next = cur_end + cur_hdr->cr + 1;
5482
5483 /* Now we have one header between cur_ptr and cur_end,
5484 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005485 */
5486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005487 /* The annoying part is that pattern matching needs
5488 * that we modify the contents to null-terminate all
5489 * strings before testing them.
5490 */
5491
5492 term = *cur_end;
5493 *cur_end = '\0';
5494
5495 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5496 switch (exp->action) {
5497 case ACT_SETBE:
5498 /* It is not possible to jump a second time.
5499 * FIXME: should we return an HTTP/500 here so that
5500 * the admin knows there's a problem ?
5501 */
5502 if (t->be != t->fe)
5503 break;
5504
5505 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005506 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005507 last_hdr = 1;
5508 break;
5509
5510 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005511 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005512 last_hdr = 1;
5513 break;
5514
5515 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005516 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005517 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005518
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005519 t->fe->fe_counters.denied_req++;
5520 if (t->fe != t->be)
5521 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005522 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005523 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005525 break;
5526
5527 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005528 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005529 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005530
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005531 t->fe->fe_counters.denied_req++;
5532 if (t->fe != t->be)
5533 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005534 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005535 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005536
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005537 break;
5538
5539 case ACT_REPLACE:
5540 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5541 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5542 /* FIXME: if the user adds a newline in the replacement, the
5543 * index will not be recalculated for now, and the new line
5544 * will not be counted as a new header.
5545 */
5546
5547 cur_end += delta;
5548 cur_next += delta;
5549 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005550 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005551 break;
5552
5553 case ACT_REMOVE:
5554 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5555 cur_next += delta;
5556
Willy Tarreaufa355d42009-11-29 18:12:29 +01005557 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005558 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5559 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005560 cur_hdr->len = 0;
5561 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005562 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005563 break;
5564
5565 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005566 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005567 if (cur_end)
5568 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005570 /* keep the link from this header to next one in case of later
5571 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005572 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005573 old_idx = cur_idx;
5574 }
5575 return 0;
5576}
5577
5578
5579/* Apply the filter to the request line.
5580 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5581 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005582 * Since it can manage the switch to another backend, it updates the per-proxy
5583 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005584 */
5585int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5586{
5587 char term;
5588 char *cur_ptr, *cur_end;
5589 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005590 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005591 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005592
Willy Tarreau58f10d72006-12-04 02:26:12 +01005593
Willy Tarreau3d300592007-03-18 18:34:41 +01005594 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005595 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005596 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005597 (exp->action == ACT_ALLOW ||
5598 exp->action == ACT_DENY ||
5599 exp->action == ACT_TARPIT))
5600 return 0;
5601 else if (exp->action == ACT_REMOVE)
5602 return 0;
5603
5604 done = 0;
5605
Willy Tarreau3a215be2012-03-09 21:39:51 +01005606 cur_ptr = req->p + txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005607 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005608
5609 /* Now we have the request line between cur_ptr and cur_end */
5610
5611 /* The annoying part is that pattern matching needs
5612 * that we modify the contents to null-terminate all
5613 * strings before testing them.
5614 */
5615
5616 term = *cur_end;
5617 *cur_end = '\0';
5618
5619 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5620 switch (exp->action) {
5621 case ACT_SETBE:
5622 /* It is not possible to jump a second time.
5623 * FIXME: should we return an HTTP/500 here so that
5624 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005625 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005626 if (t->be != t->fe)
5627 break;
5628
5629 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005630 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005631 done = 1;
5632 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005633
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005634 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005635 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005636 done = 1;
5637 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005638
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005639 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005640 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005641
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005642 t->fe->fe_counters.denied_req++;
5643 if (t->fe != t->be)
5644 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005645 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005646 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005648 done = 1;
5649 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005650
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005651 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005652 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005653
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005654 t->fe->fe_counters.denied_req++;
5655 if (t->fe != t->be)
5656 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005657 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005658 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005660 done = 1;
5661 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005662
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005663 case ACT_REPLACE:
5664 *cur_end = term; /* restore the string terminator */
5665 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5666 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5667 /* FIXME: if the user adds a newline in the replacement, the
5668 * index will not be recalculated for now, and the new line
5669 * will not be counted as a new header.
5670 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005671
Willy Tarreaufa355d42009-11-29 18:12:29 +01005672 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005673 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01005674 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005675 HTTP_MSG_RQMETH,
5676 cur_ptr, cur_end + 1,
5677 NULL, NULL);
5678 if (unlikely(!cur_end))
5679 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005680
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005681 /* we have a full request and we know that we have either a CR
5682 * or an LF at <ptr>.
5683 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005684 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5685 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005686 /* there is no point trying this regex on headers */
5687 return 1;
5688 }
5689 }
5690 *cur_end = term; /* restore the string terminator */
5691 return done;
5692}
Willy Tarreau97de6242006-12-27 17:18:38 +01005693
Willy Tarreau58f10d72006-12-04 02:26:12 +01005694
Willy Tarreau58f10d72006-12-04 02:26:12 +01005695
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005696/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005697 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005698 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005699 * unparsable request. Since it can manage the switch to another backend, it
5700 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005701 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005702int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005703{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005704 struct http_txn *txn = &s->txn;
5705 struct hdr_exp *exp;
5706
5707 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005708 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005709
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 /*
5711 * The interleaving of transformations and verdicts
5712 * makes it difficult to decide to continue or stop
5713 * the evaluation.
5714 */
5715
Willy Tarreau6c123b12010-01-28 20:22:06 +01005716 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5717 break;
5718
Willy Tarreau3d300592007-03-18 18:34:41 +01005719 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005720 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005721 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005722 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005723
5724 /* if this filter had a condition, evaluate it now and skip to
5725 * next filter if the condition does not match.
5726 */
5727 if (exp->cond) {
5728 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5729 ret = acl_pass(ret);
5730 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5731 ret = !ret;
5732
5733 if (!ret)
5734 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005735 }
5736
5737 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005738 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005739 if (unlikely(ret < 0))
5740 return -1;
5741
5742 if (likely(ret == 0)) {
5743 /* The filter did not match the request, it can be
5744 * iterated through all headers.
5745 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005746 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005747 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005748 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005749 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005750}
5751
5752
Willy Tarreaua15645d2007-03-18 16:22:39 +01005753
Willy Tarreau58f10d72006-12-04 02:26:12 +01005754/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005755 * Try to retrieve the server associated to the appsession.
5756 * If the server is found, it's assigned to the session.
5757 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005758void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005759 struct http_txn *txn = &t->txn;
5760 appsess *asession = NULL;
5761 char *sessid_temp = NULL;
5762
Cyril Bontéb21570a2009-11-29 20:04:48 +01005763 if (len > t->be->appsession_len) {
5764 len = t->be->appsession_len;
5765 }
5766
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005767 if (t->be->options2 & PR_O2_AS_REQL) {
5768 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005769 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005770 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005771 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005772 }
5773
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005774 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005775 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5776 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5777 return;
5778 }
5779
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005780 memcpy(txn->sessid, buf, len);
5781 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005782 }
5783
5784 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5785 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5786 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5787 return;
5788 }
5789
Cyril Bontéb21570a2009-11-29 20:04:48 +01005790 memcpy(sessid_temp, buf, len);
5791 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005792
5793 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5794 /* free previously allocated memory */
5795 pool_free2(apools.sessid, sessid_temp);
5796
5797 if (asession != NULL) {
5798 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5799 if (!(t->be->options2 & PR_O2_AS_REQL))
5800 asession->request_count++;
5801
5802 if (asession->serverid != NULL) {
5803 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005804
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005805 while (srv) {
5806 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005807 if ((srv->state & SRV_RUNNING) ||
5808 (t->be->options & PR_O_PERSIST) ||
5809 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005810 /* we found the server and it's usable */
5811 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005812 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005813 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005814 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005815
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005816 break;
5817 } else {
5818 txn->flags &= ~TX_CK_MASK;
5819 txn->flags |= TX_CK_DOWN;
5820 }
5821 }
5822 srv = srv->next;
5823 }
5824 }
5825 }
5826}
5827
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005828/* Find the end of a cookie value contained between <s> and <e>. It works the
5829 * same way as with headers above except that the semi-colon also ends a token.
5830 * See RFC2965 for more information. Note that it requires a valid header to
5831 * return a valid result.
5832 */
5833char *find_cookie_value_end(char *s, const char *e)
5834{
5835 int quoted, qdpair;
5836
5837 quoted = qdpair = 0;
5838 for (; s < e; s++) {
5839 if (qdpair) qdpair = 0;
5840 else if (quoted) {
5841 if (*s == '\\') qdpair = 1;
5842 else if (*s == '"') quoted = 0;
5843 }
5844 else if (*s == '"') quoted = 1;
5845 else if (*s == ',' || *s == ';') return s;
5846 }
5847 return s;
5848}
5849
5850/* Delete a value in a header between delimiters <from> and <next> in buffer
5851 * <buf>. The number of characters displaced is returned, and the pointer to
5852 * the first delimiter is updated if required. The function tries as much as
5853 * possible to respect the following principles :
5854 * - replace <from> delimiter by the <next> one unless <from> points to a
5855 * colon, in which case <next> is simply removed
5856 * - set exactly one space character after the new first delimiter, unless
5857 * there are not enough characters in the block being moved to do so.
5858 * - remove unneeded spaces before the previous delimiter and after the new
5859 * one.
5860 *
5861 * It is the caller's responsibility to ensure that :
5862 * - <from> points to a valid delimiter or the colon ;
5863 * - <next> points to a valid delimiter or the final CR/LF ;
5864 * - there are non-space chars before <from> ;
5865 * - there is a CR/LF at or after <next>.
5866 */
5867int del_hdr_value(struct buffer *buf, char **from, char *next)
5868{
5869 char *prev = *from;
5870
5871 if (*prev == ':') {
5872 /* We're removing the first value, preserve the colon and add a
5873 * space if possible.
5874 */
5875 if (!http_is_crlf[(unsigned char)*next])
5876 next++;
5877 prev++;
5878 if (prev < next)
5879 *prev++ = ' ';
5880
5881 while (http_is_spht[(unsigned char)*next])
5882 next++;
5883 } else {
5884 /* Remove useless spaces before the old delimiter. */
5885 while (http_is_spht[(unsigned char)*(prev-1)])
5886 prev--;
5887 *from = prev;
5888
5889 /* copy the delimiter and if possible a space if we're
5890 * not at the end of the line.
5891 */
5892 if (!http_is_crlf[(unsigned char)*next]) {
5893 *prev++ = *next++;
5894 if (prev + 1 < next)
5895 *prev++ = ' ';
5896 while (http_is_spht[(unsigned char)*next])
5897 next++;
5898 }
5899 }
5900 return buffer_replace2(buf, prev, next, NULL, 0);
5901}
5902
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005903/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005904 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005905 * desirable to call it only when needed. This code is quite complex because
5906 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5907 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005908 */
5909void manage_client_side_cookies(struct session *t, struct buffer *req)
5910{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005911 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005912 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005913 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005914 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5915 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005916
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005917 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005918 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01005919 hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005920
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005921 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005922 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005923 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005924
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005925 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005926 hdr_beg = hdr_next;
5927 hdr_end = hdr_beg + cur_hdr->len;
5928 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005929
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005930 /* We have one full header between hdr_beg and hdr_end, and the
5931 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005932 * "Cookie:" headers.
5933 */
5934
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005935 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005936 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005937 old_idx = cur_idx;
5938 continue;
5939 }
5940
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005941 del_from = NULL; /* nothing to be deleted */
5942 preserve_hdr = 0; /* assume we may kill the whole header */
5943
Willy Tarreau58f10d72006-12-04 02:26:12 +01005944 /* Now look for cookies. Conforming to RFC2109, we have to support
5945 * attributes whose name begin with a '$', and associate them with
5946 * the right cookie, if we want to delete this cookie.
5947 * So there are 3 cases for each cookie read :
5948 * 1) it's a special attribute, beginning with a '$' : ignore it.
5949 * 2) it's a server id cookie that we *MAY* want to delete : save
5950 * some pointers on it (last semi-colon, beginning of cookie...)
5951 * 3) it's an application cookie : we *MAY* have to delete a previous
5952 * "special" cookie.
5953 * At the end of loop, if a "special" cookie remains, we may have to
5954 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005955 * *MUST* delete it.
5956 *
5957 * Note: RFC2965 is unclear about the processing of spaces around
5958 * the equal sign in the ATTR=VALUE form. A careful inspection of
5959 * the RFC explicitly allows spaces before it, and not within the
5960 * tokens (attrs or values). An inspection of RFC2109 allows that
5961 * too but section 10.1.3 lets one think that spaces may be allowed
5962 * after the equal sign too, resulting in some (rare) buggy
5963 * implementations trying to do that. So let's do what servers do.
5964 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5965 * allowed quoted strings in values, with any possible character
5966 * after a backslash, including control chars and delimitors, which
5967 * causes parsing to become ambiguous. Browsers also allow spaces
5968 * within values even without quotes.
5969 *
5970 * We have to keep multiple pointers in order to support cookie
5971 * removal at the beginning, middle or end of header without
5972 * corrupting the header. All of these headers are valid :
5973 *
5974 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5975 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5976 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5977 * | | | | | | | | |
5978 * | | | | | | | | hdr_end <--+
5979 * | | | | | | | +--> next
5980 * | | | | | | +----> val_end
5981 * | | | | | +-----------> val_beg
5982 * | | | | +--------------> equal
5983 * | | | +----------------> att_end
5984 * | | +---------------------> att_beg
5985 * | +--------------------------> prev
5986 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005987 */
5988
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005989 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5990 /* Iterate through all cookies on this line */
5991
5992 /* find att_beg */
5993 att_beg = prev + 1;
5994 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
5995 att_beg++;
5996
5997 /* find att_end : this is the first character after the last non
5998 * space before the equal. It may be equal to hdr_end.
5999 */
6000 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006001
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006002 while (equal < hdr_end) {
6003 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006004 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006005 if (http_is_spht[(unsigned char)*equal++])
6006 continue;
6007 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006008 }
6009
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006010 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6011 * is between <att_beg> and <equal>, both may be identical.
6012 */
6013
6014 /* look for end of cookie if there is an equal sign */
6015 if (equal < hdr_end && *equal == '=') {
6016 /* look for the beginning of the value */
6017 val_beg = equal + 1;
6018 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6019 val_beg++;
6020
6021 /* find the end of the value, respecting quotes */
6022 next = find_cookie_value_end(val_beg, hdr_end);
6023
6024 /* make val_end point to the first white space or delimitor after the value */
6025 val_end = next;
6026 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6027 val_end--;
6028 } else {
6029 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006030 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006031
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006032 /* We have nothing to do with attributes beginning with '$'. However,
6033 * they will automatically be removed if a header before them is removed,
6034 * since they're supposed to be linked together.
6035 */
6036 if (*att_beg == '$')
6037 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006038
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006039 /* Ignore cookies with no equal sign */
6040 if (equal == next) {
6041 /* This is not our cookie, so we must preserve it. But if we already
6042 * scheduled another cookie for removal, we cannot remove the
6043 * complete header, but we can remove the previous block itself.
6044 */
6045 preserve_hdr = 1;
6046 if (del_from != NULL) {
6047 int delta = del_hdr_value(req, &del_from, prev);
6048 val_end += delta;
6049 next += delta;
6050 hdr_end += delta;
6051 hdr_next += delta;
6052 cur_hdr->len += delta;
6053 http_msg_move_end(&txn->req, delta);
6054 prev = del_from;
6055 del_from = NULL;
6056 }
6057 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006058 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006059
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006060 /* if there are spaces around the equal sign, we need to
6061 * strip them otherwise we'll get trouble for cookie captures,
6062 * or even for rewrites. Since this happens extremely rarely,
6063 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006064 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006065 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6066 int stripped_before = 0;
6067 int stripped_after = 0;
6068
6069 if (att_end != equal) {
6070 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6071 equal += stripped_before;
6072 val_beg += stripped_before;
6073 }
6074
6075 if (val_beg > equal + 1) {
6076 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6077 val_beg += stripped_after;
6078 stripped_before += stripped_after;
6079 }
6080
6081 val_end += stripped_before;
6082 next += stripped_before;
6083 hdr_end += stripped_before;
6084 hdr_next += stripped_before;
6085 cur_hdr->len += stripped_before;
6086 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006087 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006088 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006089
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006090 /* First, let's see if we want to capture this cookie. We check
6091 * that we don't already have a client side cookie, because we
6092 * can only capture one. Also as an optimisation, we ignore
6093 * cookies shorter than the declared name.
6094 */
6095 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6096 (val_end - att_beg >= t->fe->capture_namelen) &&
6097 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6098 int log_len = val_end - att_beg;
6099
6100 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6101 Alert("HTTP logging : out of memory.\n");
6102 } else {
6103 if (log_len > t->fe->capture_len)
6104 log_len = t->fe->capture_len;
6105 memcpy(txn->cli_cookie, att_beg, log_len);
6106 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006107 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006109
Willy Tarreaubca99692010-10-06 19:25:55 +02006110 /* Persistence cookies in passive, rewrite or insert mode have the
6111 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006112 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006113 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006114 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006115 * For cookies in prefix mode, the form is :
6116 *
6117 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006118 */
6119 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6120 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6121 struct server *srv = t->be->srv;
6122 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006123
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006124 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6125 * have the server ID between val_beg and delim, and the original cookie between
6126 * delim+1 and val_end. Otherwise, delim==val_end :
6127 *
6128 * Cookie: NAME=SRV; # in all but prefix modes
6129 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6130 * | || || | |+-> next
6131 * | || || | +--> val_end
6132 * | || || +---------> delim
6133 * | || |+------------> val_beg
6134 * | || +-------------> att_end = equal
6135 * | |+-----------------> att_beg
6136 * | +------------------> prev
6137 * +-------------------------> hdr_beg
6138 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006139
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006140 if (t->be->options & PR_O_COOK_PFX) {
6141 for (delim = val_beg; delim < val_end; delim++)
6142 if (*delim == COOKIE_DELIM)
6143 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006144 } else {
6145 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006146 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006147 /* Now check if the cookie contains a date field, which would
6148 * appear after a vertical bar ('|') just after the server name
6149 * and before the delimiter.
6150 */
6151 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6152 if (vbar1) {
6153 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006154 * right is the last seen date. It is a base64 encoded
6155 * 30-bit value representing the UNIX date since the
6156 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006157 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006158 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006159 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006160 if (val_end - vbar1 >= 5) {
6161 val = b64tos30(vbar1);
6162 if (val > 0)
6163 txn->cookie_last_date = val << 2;
6164 }
6165 /* look for a second vertical bar */
6166 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6167 if (vbar1 && (val_end - vbar1 > 5)) {
6168 val = b64tos30(vbar1 + 1);
6169 if (val > 0)
6170 txn->cookie_first_date = val << 2;
6171 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006172 }
6173 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006174
Willy Tarreauf64d1412010-10-07 20:06:11 +02006175 /* if the cookie has an expiration date and the proxy wants to check
6176 * it, then we do that now. We first check if the cookie is too old,
6177 * then only if it has expired. We detect strict overflow because the
6178 * time resolution here is not great (4 seconds). Cookies with dates
6179 * in the future are ignored if their offset is beyond one day. This
6180 * allows an admin to fix timezone issues without expiring everyone
6181 * and at the same time avoids keeping unwanted side effects for too
6182 * long.
6183 */
6184 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006185 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6186 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006187 txn->flags &= ~TX_CK_MASK;
6188 txn->flags |= TX_CK_OLD;
6189 delim = val_beg; // let's pretend we have not found the cookie
6190 txn->cookie_first_date = 0;
6191 txn->cookie_last_date = 0;
6192 }
6193 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006194 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6195 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006196 txn->flags &= ~TX_CK_MASK;
6197 txn->flags |= TX_CK_EXPIRED;
6198 delim = val_beg; // let's pretend we have not found the cookie
6199 txn->cookie_first_date = 0;
6200 txn->cookie_last_date = 0;
6201 }
6202
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006203 /* Here, we'll look for the first running server which supports the cookie.
6204 * This allows to share a same cookie between several servers, for example
6205 * to dedicate backup servers to specific servers only.
6206 * However, to prevent clients from sticking to cookie-less backup server
6207 * when they have incidentely learned an empty cookie, we simply ignore
6208 * empty cookies and mark them as invalid.
6209 * The same behaviour is applied when persistence must be ignored.
6210 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006211 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006212 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006213
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006214 while (srv) {
6215 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6216 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6217 if ((srv->state & SRV_RUNNING) ||
6218 (t->be->options & PR_O_PERSIST) ||
6219 (t->flags & SN_FORCE_PRST)) {
6220 /* we found the server and we can use it */
6221 txn->flags &= ~TX_CK_MASK;
6222 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6223 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006224 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006225 break;
6226 } else {
6227 /* we found a server, but it's down,
6228 * mark it as such and go on in case
6229 * another one is available.
6230 */
6231 txn->flags &= ~TX_CK_MASK;
6232 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006233 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006234 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006235 srv = srv->next;
6236 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237
Willy Tarreauf64d1412010-10-07 20:06:11 +02006238 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006239 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006240 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006241 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6242 txn->flags |= TX_CK_UNUSED;
6243 else
6244 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006246
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 /* depending on the cookie mode, we may have to either :
6248 * - delete the complete cookie if we're in insert+indirect mode, so that
6249 * the server never sees it ;
6250 * - remove the server id from the cookie value, and tag the cookie as an
6251 * application cookie so that it does not get accidentely removed later,
6252 * if we're in cookie prefix mode
6253 */
6254 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6255 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006257 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6258 val_end += delta;
6259 next += delta;
6260 hdr_end += delta;
6261 hdr_next += delta;
6262 cur_hdr->len += delta;
6263 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006264
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006265 del_from = NULL;
6266 preserve_hdr = 1; /* we want to keep this cookie */
6267 }
6268 else if (del_from == NULL &&
6269 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6270 del_from = prev;
6271 }
6272 } else {
6273 /* This is not our cookie, so we must preserve it. But if we already
6274 * scheduled another cookie for removal, we cannot remove the
6275 * complete header, but we can remove the previous block itself.
6276 */
6277 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006278
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006279 if (del_from != NULL) {
6280 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006281 if (att_beg >= del_from)
6282 att_beg += delta;
6283 if (att_end >= del_from)
6284 att_end += delta;
6285 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006286 val_end += delta;
6287 next += delta;
6288 hdr_end += delta;
6289 hdr_next += delta;
6290 cur_hdr->len += delta;
6291 http_msg_move_end(&txn->req, delta);
6292 prev = del_from;
6293 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006294 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006295 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006297 /* Look for the appsession cookie unless persistence must be ignored */
6298 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6299 int cmp_len, value_len;
6300 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006301
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006302 if (t->be->options2 & PR_O2_AS_PFX) {
6303 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6304 value_begin = att_beg + t->be->appsession_name_len;
6305 value_len = val_end - att_beg - t->be->appsession_name_len;
6306 } else {
6307 cmp_len = att_end - att_beg;
6308 value_begin = val_beg;
6309 value_len = val_end - val_beg;
6310 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006311
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006312 /* let's see if the cookie is our appcookie */
6313 if (cmp_len == t->be->appsession_name_len &&
6314 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6315 manage_client_side_appsession(t, value_begin, value_len);
6316 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006317 }
6318
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006319 /* continue with next cookie on this header line */
6320 att_beg = next;
6321 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006322
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 /* There are no more cookies on this line.
6324 * We may still have one (or several) marked for deletion at the
6325 * end of the line. We must do this now in two ways :
6326 * - if some cookies must be preserved, we only delete from the
6327 * mark to the end of line ;
6328 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006329 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006330 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006331 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006332 if (preserve_hdr) {
6333 delta = del_hdr_value(req, &del_from, hdr_end);
6334 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006335 cur_hdr->len += delta;
6336 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006337 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338
6339 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006340 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6341 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006343 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006344 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006345 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006346 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006347 }
6348
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006349 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006350 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006351 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006352}
6353
6354
Willy Tarreaua15645d2007-03-18 16:22:39 +01006355/* Iterate the same filter through all response headers contained in <rtr>.
6356 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6357 */
6358int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6359{
6360 char term;
6361 char *cur_ptr, *cur_end, *cur_next;
6362 int cur_idx, old_idx, last_hdr;
6363 struct http_txn *txn = &t->txn;
6364 struct hdr_idx_elem *cur_hdr;
6365 int len, delta;
6366
6367 last_hdr = 0;
6368
Willy Tarreau3a215be2012-03-09 21:39:51 +01006369 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006370 old_idx = 0;
6371
6372 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006373 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006374 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006375 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006376 (exp->action == ACT_ALLOW ||
6377 exp->action == ACT_DENY))
6378 return 0;
6379
6380 cur_idx = txn->hdr_idx.v[old_idx].next;
6381 if (!cur_idx)
6382 break;
6383
6384 cur_hdr = &txn->hdr_idx.v[cur_idx];
6385 cur_ptr = cur_next;
6386 cur_end = cur_ptr + cur_hdr->len;
6387 cur_next = cur_end + cur_hdr->cr + 1;
6388
6389 /* Now we have one header between cur_ptr and cur_end,
6390 * and the next header starts at cur_next.
6391 */
6392
6393 /* The annoying part is that pattern matching needs
6394 * that we modify the contents to null-terminate all
6395 * strings before testing them.
6396 */
6397
6398 term = *cur_end;
6399 *cur_end = '\0';
6400
6401 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6402 switch (exp->action) {
6403 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006404 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006405 last_hdr = 1;
6406 break;
6407
6408 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006409 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006410 last_hdr = 1;
6411 break;
6412
6413 case ACT_REPLACE:
6414 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6415 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6416 /* FIXME: if the user adds a newline in the replacement, the
6417 * index will not be recalculated for now, and the new line
6418 * will not be counted as a new header.
6419 */
6420
6421 cur_end += delta;
6422 cur_next += delta;
6423 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006424 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006425 break;
6426
6427 case ACT_REMOVE:
6428 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6429 cur_next += delta;
6430
Willy Tarreaufa355d42009-11-29 18:12:29 +01006431 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006432 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6433 txn->hdr_idx.used--;
6434 cur_hdr->len = 0;
6435 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006436 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006437 break;
6438
6439 }
6440 }
6441 if (cur_end)
6442 *cur_end = term; /* restore the string terminator */
6443
6444 /* keep the link from this header to next one in case of later
6445 * removal of next header.
6446 */
6447 old_idx = cur_idx;
6448 }
6449 return 0;
6450}
6451
6452
6453/* Apply the filter to the status line in the response buffer <rtr>.
6454 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6455 * or -1 if a replacement resulted in an invalid status line.
6456 */
6457int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6458{
6459 char term;
6460 char *cur_ptr, *cur_end;
6461 int done;
6462 struct http_txn *txn = &t->txn;
6463 int len, delta;
6464
6465
Willy Tarreau3d300592007-03-18 18:34:41 +01006466 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006467 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006468 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006469 (exp->action == ACT_ALLOW ||
6470 exp->action == ACT_DENY))
6471 return 0;
6472 else if (exp->action == ACT_REMOVE)
6473 return 0;
6474
6475 done = 0;
6476
Willy Tarreau3a215be2012-03-09 21:39:51 +01006477 cur_ptr = rtr->p + txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006478 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006479
6480 /* Now we have the status line between cur_ptr and cur_end */
6481
6482 /* The annoying part is that pattern matching needs
6483 * that we modify the contents to null-terminate all
6484 * strings before testing them.
6485 */
6486
6487 term = *cur_end;
6488 *cur_end = '\0';
6489
6490 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6491 switch (exp->action) {
6492 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006493 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006494 done = 1;
6495 break;
6496
6497 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006498 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006499 done = 1;
6500 break;
6501
6502 case ACT_REPLACE:
6503 *cur_end = term; /* restore the string terminator */
6504 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6505 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6506 /* FIXME: if the user adds a newline in the replacement, the
6507 * index will not be recalculated for now, and the new line
6508 * will not be counted as a new header.
6509 */
6510
Willy Tarreaufa355d42009-11-29 18:12:29 +01006511 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006512 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01006513 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006514 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006515 cur_ptr, cur_end + 1,
6516 NULL, NULL);
6517 if (unlikely(!cur_end))
6518 return -1;
6519
6520 /* we have a full respnse and we know that we have either a CR
6521 * or an LF at <ptr>.
6522 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01006523 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 +02006524 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006525 /* there is no point trying this regex on headers */
6526 return 1;
6527 }
6528 }
6529 *cur_end = term; /* restore the string terminator */
6530 return done;
6531}
6532
6533
6534
6535/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006536 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006537 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6538 * unparsable response.
6539 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006540int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006541{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006542 struct http_txn *txn = &s->txn;
6543 struct hdr_exp *exp;
6544
6545 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546 int ret;
6547
6548 /*
6549 * The interleaving of transformations and verdicts
6550 * makes it difficult to decide to continue or stop
6551 * the evaluation.
6552 */
6553
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006554 if (txn->flags & TX_SVDENY)
6555 break;
6556
Willy Tarreau3d300592007-03-18 18:34:41 +01006557 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006558 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6559 exp->action == ACT_PASS)) {
6560 exp = exp->next;
6561 continue;
6562 }
6563
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006564 /* if this filter had a condition, evaluate it now and skip to
6565 * next filter if the condition does not match.
6566 */
6567 if (exp->cond) {
6568 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6569 ret = acl_pass(ret);
6570 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6571 ret = !ret;
6572 if (!ret)
6573 continue;
6574 }
6575
Willy Tarreaua15645d2007-03-18 16:22:39 +01006576 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006577 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006578 if (unlikely(ret < 0))
6579 return -1;
6580
6581 if (likely(ret == 0)) {
6582 /* The filter did not match the response, it can be
6583 * iterated through all headers.
6584 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006585 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006586 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006587 }
6588 return 0;
6589}
6590
6591
Willy Tarreaua15645d2007-03-18 16:22:39 +01006592/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006593 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006594 * desirable to call it only when needed. This function is also used when we
6595 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006597void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006598{
6599 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006600 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006601 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006602 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006603 char *hdr_beg, *hdr_end, *hdr_next;
6604 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605
Willy Tarreaua15645d2007-03-18 16:22:39 +01006606 /* Iterate through the headers.
6607 * we start with the start line.
6608 */
6609 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006610 hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006611
6612 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6613 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006614 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006615
6616 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006617 hdr_beg = hdr_next;
6618 hdr_end = hdr_beg + cur_hdr->len;
6619 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620
Willy Tarreau24581ba2010-08-31 22:39:35 +02006621 /* We have one full header between hdr_beg and hdr_end, and the
6622 * next header starts at hdr_next. We're only interested in
6623 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006624 */
6625
Willy Tarreau24581ba2010-08-31 22:39:35 +02006626 is_cookie2 = 0;
6627 prev = hdr_beg + 10;
6628 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006629 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006630 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6631 if (!val) {
6632 old_idx = cur_idx;
6633 continue;
6634 }
6635 is_cookie2 = 1;
6636 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006637 }
6638
Willy Tarreau24581ba2010-08-31 22:39:35 +02006639 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6640 * <prev> points to the colon.
6641 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006642 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006643
Willy Tarreau24581ba2010-08-31 22:39:35 +02006644 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6645 * check-cache is enabled) and we are not interested in checking
6646 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006647 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006648 if (t->be->cookie_name == NULL &&
6649 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006650 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006651 return;
6652
Willy Tarreau24581ba2010-08-31 22:39:35 +02006653 /* OK so now we know we have to process this response cookie.
6654 * The format of the Set-Cookie header is slightly different
6655 * from the format of the Cookie header in that it does not
6656 * support the comma as a cookie delimiter (thus the header
6657 * cannot be folded) because the Expires attribute described in
6658 * the original Netscape's spec may contain an unquoted date
6659 * with a comma inside. We have to live with this because
6660 * many browsers don't support Max-Age and some browsers don't
6661 * support quoted strings. However the Set-Cookie2 header is
6662 * clean.
6663 *
6664 * We have to keep multiple pointers in order to support cookie
6665 * removal at the beginning, middle or end of header without
6666 * corrupting the header (in case of set-cookie2). A special
6667 * pointer, <scav> points to the beginning of the set-cookie-av
6668 * fields after the first semi-colon. The <next> pointer points
6669 * either to the end of line (set-cookie) or next unquoted comma
6670 * (set-cookie2). All of these headers are valid :
6671 *
6672 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6673 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6674 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6675 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6676 * | | | | | | | | | |
6677 * | | | | | | | | +-> next hdr_end <--+
6678 * | | | | | | | +------------> scav
6679 * | | | | | | +--------------> val_end
6680 * | | | | | +--------------------> val_beg
6681 * | | | | +----------------------> equal
6682 * | | | +------------------------> att_end
6683 * | | +----------------------------> att_beg
6684 * | +------------------------------> prev
6685 * +-----------------------------------------> hdr_beg
6686 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687
Willy Tarreau24581ba2010-08-31 22:39:35 +02006688 for (; prev < hdr_end; prev = next) {
6689 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006690
Willy Tarreau24581ba2010-08-31 22:39:35 +02006691 /* find att_beg */
6692 att_beg = prev + 1;
6693 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6694 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006695
Willy Tarreau24581ba2010-08-31 22:39:35 +02006696 /* find att_end : this is the first character after the last non
6697 * space before the equal. It may be equal to hdr_end.
6698 */
6699 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700
Willy Tarreau24581ba2010-08-31 22:39:35 +02006701 while (equal < hdr_end) {
6702 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6703 break;
6704 if (http_is_spht[(unsigned char)*equal++])
6705 continue;
6706 att_end = equal;
6707 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708
Willy Tarreau24581ba2010-08-31 22:39:35 +02006709 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6710 * is between <att_beg> and <equal>, both may be identical.
6711 */
6712
6713 /* look for end of cookie if there is an equal sign */
6714 if (equal < hdr_end && *equal == '=') {
6715 /* look for the beginning of the value */
6716 val_beg = equal + 1;
6717 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6718 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006719
Willy Tarreau24581ba2010-08-31 22:39:35 +02006720 /* find the end of the value, respecting quotes */
6721 next = find_cookie_value_end(val_beg, hdr_end);
6722
6723 /* make val_end point to the first white space or delimitor after the value */
6724 val_end = next;
6725 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6726 val_end--;
6727 } else {
6728 /* <equal> points to next comma, semi-colon or EOL */
6729 val_beg = val_end = next = equal;
6730 }
6731
6732 if (next < hdr_end) {
6733 /* Set-Cookie2 supports multiple cookies, and <next> points to
6734 * a colon or semi-colon before the end. So skip all attr-value
6735 * pairs and look for the next comma. For Set-Cookie, since
6736 * commas are permitted in values, skip to the end.
6737 */
6738 if (is_cookie2)
6739 next = find_hdr_value_end(next, hdr_end);
6740 else
6741 next = hdr_end;
6742 }
6743
6744 /* Now everything is as on the diagram above */
6745
6746 /* Ignore cookies with no equal sign */
6747 if (equal == val_end)
6748 continue;
6749
6750 /* If there are spaces around the equal sign, we need to
6751 * strip them otherwise we'll get trouble for cookie captures,
6752 * or even for rewrites. Since this happens extremely rarely,
6753 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006754 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006755 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6756 int stripped_before = 0;
6757 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758
Willy Tarreau24581ba2010-08-31 22:39:35 +02006759 if (att_end != equal) {
6760 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6761 equal += stripped_before;
6762 val_beg += stripped_before;
6763 }
6764
6765 if (val_beg > equal + 1) {
6766 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6767 val_beg += stripped_after;
6768 stripped_before += stripped_after;
6769 }
6770
6771 val_end += stripped_before;
6772 next += stripped_before;
6773 hdr_end += stripped_before;
6774 hdr_next += stripped_before;
6775 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006776 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006777 }
6778
6779 /* First, let's see if we want to capture this cookie. We check
6780 * that we don't already have a server side cookie, because we
6781 * can only capture one. Also as an optimisation, we ignore
6782 * cookies shorter than the declared name.
6783 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006784 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006785 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006786 (val_end - att_beg >= t->fe->capture_namelen) &&
6787 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6788 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006789 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 Alert("HTTP logging : out of memory.\n");
6791 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006792 else {
6793 if (log_len > t->fe->capture_len)
6794 log_len = t->fe->capture_len;
6795 memcpy(txn->srv_cookie, att_beg, log_len);
6796 txn->srv_cookie[log_len] = 0;
6797 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006798 }
6799
Willy Tarreau827aee92011-03-10 16:55:02 +01006800 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006802 if (!(t->flags & SN_IGNORE_PRST) &&
6803 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6804 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006805 /* assume passive cookie by default */
6806 txn->flags &= ~TX_SCK_MASK;
6807 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006808
6809 /* If the cookie is in insert mode on a known server, we'll delete
6810 * this occurrence because we'll insert another one later.
6811 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006812 * a direct access.
6813 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006814 if (t->be->options2 & PR_O2_COOK_PSV) {
6815 /* The "preserve" flag was set, we don't want to touch the
6816 * server's cookie.
6817 */
6818 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006819 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006820 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006821 /* this cookie must be deleted */
6822 if (*prev == ':' && next == hdr_end) {
6823 /* whole header */
6824 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6825 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6826 txn->hdr_idx.used--;
6827 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006828 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006829 hdr_next += delta;
6830 http_msg_move_end(&txn->rsp, delta);
6831 /* note: while both invalid now, <next> and <hdr_end>
6832 * are still equal, so the for() will stop as expected.
6833 */
6834 } else {
6835 /* just remove the value */
6836 int delta = del_hdr_value(res, &prev, next);
6837 next = prev;
6838 hdr_end += delta;
6839 hdr_next += delta;
6840 cur_hdr->len += delta;
6841 http_msg_move_end(&txn->rsp, delta);
6842 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006843 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006844 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006845 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006847 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006848 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006849 * with this server since we know it.
6850 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006851 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006852 next += delta;
6853 hdr_end += delta;
6854 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006856 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006857
Willy Tarreauf1348312010-10-07 15:54:11 +02006858 txn->flags &= ~TX_SCK_MASK;
6859 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006860 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006861 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006862 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006863 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006864 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006865 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006866 next += delta;
6867 hdr_end += delta;
6868 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006869 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006870 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006871
Willy Tarreau827aee92011-03-10 16:55:02 +01006872 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006873 txn->flags &= ~TX_SCK_MASK;
6874 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875 }
6876 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006877 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6878 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006879 int cmp_len, value_len;
6880 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006881
Cyril Bontéb21570a2009-11-29 20:04:48 +01006882 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006883 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6884 value_begin = att_beg + t->be->appsession_name_len;
6885 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006886 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006887 cmp_len = att_end - att_beg;
6888 value_begin = val_beg;
6889 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006890 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006891
Cyril Bonté17530c32010-04-06 21:11:10 +02006892 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006893 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6894 /* free a possibly previously allocated memory */
6895 pool_free2(apools.sessid, txn->sessid);
6896
Cyril Bontéb21570a2009-11-29 20:04:48 +01006897 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006898 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006899 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6900 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6901 return;
6902 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006903 memcpy(txn->sessid, value_begin, value_len);
6904 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006905 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006906 }
6907 /* that's done for this cookie, check the next one on the same
6908 * line when next != hdr_end (only if is_cookie2).
6909 */
6910 }
6911 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006913 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006914
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006915 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006916 appsess *asession = NULL;
6917 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006918 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006919 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006920 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006921 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6922 Alert("Not enough Memory process_srv():asession:calloc().\n");
6923 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6924 return;
6925 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006926 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6927
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006928 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6929 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6930 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006931 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006932 return;
6933 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006934 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006935 asession->sessid[t->be->appsession_len] = 0;
6936
Willy Tarreau827aee92011-03-10 16:55:02 +01006937 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006938 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006939 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006940 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006941 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006942 return;
6943 }
6944 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006945 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006946
6947 asession->request_count = 0;
6948 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6949 }
6950
6951 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6952 asession->request_count++;
6953 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006954}
6955
6956
Willy Tarreaua15645d2007-03-18 16:22:39 +01006957/*
6958 * Check if response is cacheable or not. Updates t->flags.
6959 */
6960void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6961{
6962 struct http_txn *txn = &t->txn;
6963 char *p1, *p2;
6964
6965 char *cur_ptr, *cur_end, *cur_next;
6966 int cur_idx;
6967
Willy Tarreau5df51872007-11-25 16:20:08 +01006968 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006969 return;
6970
6971 /* Iterate through the headers.
6972 * we start with the start line.
6973 */
6974 cur_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006975 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006976
6977 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6978 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006979 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980
6981 cur_hdr = &txn->hdr_idx.v[cur_idx];
6982 cur_ptr = cur_next;
6983 cur_end = cur_ptr + cur_hdr->len;
6984 cur_next = cur_end + cur_hdr->cr + 1;
6985
6986 /* We have one full header between cur_ptr and cur_end, and the
6987 * next header starts at cur_next. We're only interested in
6988 * "Cookie:" headers.
6989 */
6990
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006991 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
6992 if (val) {
6993 if ((cur_end - (cur_ptr + val) >= 8) &&
6994 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
6995 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
6996 return;
6997 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998 }
6999
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007000 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7001 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007002 continue;
7003
7004 /* OK, right now we know we have a cache-control header at cur_ptr */
7005
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007006 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007
7008 if (p1 >= cur_end) /* no more info */
7009 continue;
7010
7011 /* p1 is at the beginning of the value */
7012 p2 = p1;
7013
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007014 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007015 p2++;
7016
7017 /* we have a complete value between p1 and p2 */
7018 if (p2 < cur_end && *p2 == '=') {
7019 /* we have something of the form no-cache="set-cookie" */
7020 if ((cur_end - p1 >= 21) &&
7021 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7022 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007023 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007024 continue;
7025 }
7026
7027 /* OK, so we know that either p2 points to the end of string or to a comma */
7028 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7029 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7030 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7031 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007032 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007033 return;
7034 }
7035
7036 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007037 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007038 continue;
7039 }
7040 }
7041}
7042
7043
Willy Tarreau58f10d72006-12-04 02:26:12 +01007044/*
7045 * Try to retrieve a known appsession in the URI, then the associated server.
7046 * If the server is found, it's assigned to the session.
7047 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007048void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007049{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007050 char *end_params, *first_param, *cur_param, *next_param;
7051 char separator;
7052 int value_len;
7053
7054 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007055
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007056 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007057 (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 +01007058 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007059 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007060
Cyril Bontéb21570a2009-11-29 20:04:48 +01007061 first_param = NULL;
7062 switch (mode) {
7063 case PR_O2_AS_M_PP:
7064 first_param = memchr(begin, ';', len);
7065 break;
7066 case PR_O2_AS_M_QS:
7067 first_param = memchr(begin, '?', len);
7068 break;
7069 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007070
Cyril Bontéb21570a2009-11-29 20:04:48 +01007071 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007072 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007073 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007074
Cyril Bontéb21570a2009-11-29 20:04:48 +01007075 switch (mode) {
7076 case PR_O2_AS_M_PP:
7077 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7078 end_params = (char *) begin + len;
7079 }
7080 separator = ';';
7081 break;
7082 case PR_O2_AS_M_QS:
7083 end_params = (char *) begin + len;
7084 separator = '&';
7085 break;
7086 default:
7087 /* unknown mode, shouldn't happen */
7088 return;
7089 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007090
Cyril Bontéb21570a2009-11-29 20:04:48 +01007091 cur_param = next_param = end_params;
7092 while (cur_param > first_param) {
7093 cur_param--;
7094 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7095 /* let's see if this is the appsession parameter */
7096 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7097 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7098 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7099 /* Cool... it's the right one */
7100 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7101 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7102 if (value_len > 0) {
7103 manage_client_side_appsession(t, cur_param, value_len);
7104 }
7105 break;
7106 }
7107 next_param = cur_param;
7108 }
7109 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007110#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007111 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007112 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007113#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007114}
7115
Willy Tarreaub2513902006-12-17 14:52:38 +01007116/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007117 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007118 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007119 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007120 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007121 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007122 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007123 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007124 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007125int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007126{
7127 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007128 struct http_msg *msg = &txn->req;
7129 const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u;
7130 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007131
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007132 if (!uri_auth)
7133 return 0;
7134
Cyril Bonté70be45d2010-10-12 00:14:35 +02007135 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007136 return 0;
7137
Willy Tarreau295a8372011-03-10 11:25:07 +01007138 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007139 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007140
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007141 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007142 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007143 return 0;
7144
Willy Tarreau3a215be2012-03-09 21:39:51 +01007145 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007146 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007147 return 0;
7148
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007149 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007150 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007151 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007152 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007153 break;
7154 }
7155 h++;
7156 }
7157
7158 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007159 h = uri + uri_auth->uri_len;
7160 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007161 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007162 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007163 break;
7164 }
7165 h++;
7166 }
7167 }
7168
Willy Tarreau3a215be2012-03-09 21:39:51 +01007169 h = uri + uri_auth->uri_len;
7170 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007171 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007172 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007173 break;
7174 }
7175 h++;
7176 }
7177
Willy Tarreau3a215be2012-03-09 21:39:51 +01007178 h = uri + uri_auth->uri_len;
7179 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007180 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007181 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007182 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007183 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7184 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7185 si->applet.ctx.stats.st_code = i;
7186 break;
7187 }
7188 }
7189 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007190 break;
7191 }
7192 h++;
7193 }
7194
Willy Tarreau295a8372011-03-10 11:25:07 +01007195 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007196
Willy Tarreaub2513902006-12-17 14:52:38 +01007197 return 1;
7198}
7199
Willy Tarreau4076a152009-04-02 15:18:36 +02007200/*
7201 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007202 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007203 * assume that msg->sol = msg->buf->p + msg->som. Also, while HTTP requests
7204 * or response messages cannot wrap, this function may also be used with chunks
7205 * which may wrap.
Willy Tarreau4076a152009-04-02 15:18:36 +02007206 */
7207void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007208 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007209 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007210{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007211 struct buffer *buf = msg->buf;
7212
Willy Tarreauea1175a2012-03-05 15:52:30 +01007213 if (buffer_wrap_add(buf, buf->p + buf->i) <= (buf->p + msg->som)) { /* message wraps */
7214 int len1 = buf->size - msg->som - (buf->p - buf->data);
7215 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som) + buf->size;
7216 memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007217 if (es->len > len1 && len1 < sizeof(es->buf))
7218 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7219 }
7220 else {
Willy Tarreauea1175a2012-03-05 15:52:30 +01007221 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som);
7222 memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007223 }
7224
Willy Tarreau4076a152009-04-02 15:18:36 +02007225 if (msg->err_pos >= 0)
Willy Tarreauea1175a2012-03-05 15:52:30 +01007226 es->pos = msg->err_pos - msg->som - (buf->p - buf->data);
7227 else if (buffer_wrap_add(buf, buf->p + msg->next) >= (buf->p + msg->som))
7228 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007229 else
Willy Tarreauea1175a2012-03-05 15:52:30 +01007230 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som) + buf->size;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007231
Willy Tarreau4076a152009-04-02 15:18:36 +02007232 es->when = date; // user-visible date
7233 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007234 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007235 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007236 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007237 es->state = state;
7238 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007239 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007240}
Willy Tarreaub2513902006-12-17 14:52:38 +01007241
Willy Tarreau294c4732011-12-16 21:35:50 +01007242/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7243 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7244 * performed over the whole headers. Otherwise it must contain a valid header
7245 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7246 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7247 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7248 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7249 * -1.
7250 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007251 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007252unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7253 struct hdr_idx *idx, int occ,
7254 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007255{
Willy Tarreau294c4732011-12-16 21:35:50 +01007256 struct hdr_ctx local_ctx;
7257 char *ptr_hist[MAX_HDR_HISTORY];
7258 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007259 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007260 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007261
Willy Tarreau294c4732011-12-16 21:35:50 +01007262 if (!ctx) {
7263 local_ctx.idx = 0;
7264 ctx = &local_ctx;
7265 }
7266
Willy Tarreaubce70882009-09-07 11:51:47 +02007267 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007268 /* search from the beginning */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007269 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007270 occ--;
7271 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007272 *vptr = ctx->line + ctx->val;
7273 *vlen = ctx->vlen;
7274 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007275 }
7276 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007277 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007278 }
7279
7280 /* negative occurrence, we scan all the list then walk back */
7281 if (-occ > MAX_HDR_HISTORY)
7282 return 0;
7283
Willy Tarreau294c4732011-12-16 21:35:50 +01007284 found = hist_ptr = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007285 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007286 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7287 len_hist[hist_ptr] = ctx->vlen;
7288 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007289 hist_ptr = 0;
7290 found++;
7291 }
7292 if (-occ > found)
7293 return 0;
7294 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7295 * find occurrence -occ, so we have to check [hist_ptr+occ].
7296 */
7297 hist_ptr += occ;
7298 if (hist_ptr >= MAX_HDR_HISTORY)
7299 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007300 *vptr = ptr_hist[hist_ptr];
7301 *vlen = len_hist[hist_ptr];
7302 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007303}
7304
Willy Tarreaubaaee002006-06-26 02:48:02 +02007305/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007306 * Print a debug line with a header
7307 */
7308void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7309{
7310 int len, max;
7311 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007312 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007313 max = end - start;
7314 UBOUND(max, sizeof(trash) - len - 1);
7315 len += strlcpy2(trash + len, start, max + 1);
7316 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007317 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007318}
7319
Willy Tarreau0937bc42009-12-22 15:03:09 +01007320/*
7321 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7322 * the required fields are properly allocated and that we only need to (re)init
7323 * them. This should be used before processing any new request.
7324 */
7325void http_init_txn(struct session *s)
7326{
7327 struct http_txn *txn = &s->txn;
7328 struct proxy *fe = s->fe;
7329
7330 txn->flags = 0;
7331 txn->status = -1;
7332
William Lallemand5f232402012-04-05 18:02:55 +02007333 global.req_count++;
7334
Willy Tarreauf64d1412010-10-07 20:06:11 +02007335 txn->cookie_first_date = 0;
7336 txn->cookie_last_date = 0;
7337
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007338 txn->req.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007339 txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007340 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007341 txn->rsp.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007342 txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007343 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007344 txn->req.chunk_len = 0LL;
7345 txn->req.body_len = 0LL;
7346 txn->rsp.chunk_len = 0LL;
7347 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007348 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7349 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007350 txn->req.buf = s->req;
7351 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007352
7353 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007354
7355 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7356 if (fe->options2 & PR_O2_REQBUG_OK)
7357 txn->req.err_pos = -1; /* let buggy requests pass */
7358
Willy Tarreau46023632010-01-07 22:51:47 +01007359 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007360 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7361
Willy Tarreau46023632010-01-07 22:51:47 +01007362 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007363 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7364
7365 if (txn->hdr_idx.v)
7366 hdr_idx_init(&txn->hdr_idx);
7367}
7368
7369/* to be used at the end of a transaction */
7370void http_end_txn(struct session *s)
7371{
7372 struct http_txn *txn = &s->txn;
7373
7374 /* these ones will have been dynamically allocated */
7375 pool_free2(pool2_requri, txn->uri);
7376 pool_free2(pool2_capture, txn->cli_cookie);
7377 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007378 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007379 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007380
William Lallemanda73203e2012-03-12 12:48:57 +01007381 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007382 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007383 txn->uri = NULL;
7384 txn->srv_cookie = NULL;
7385 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007386
7387 if (txn->req.cap) {
7388 struct cap_hdr *h;
7389 for (h = s->fe->req_cap; h; h = h->next)
7390 pool_free2(h->pool, txn->req.cap[h->index]);
7391 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7392 }
7393
7394 if (txn->rsp.cap) {
7395 struct cap_hdr *h;
7396 for (h = s->fe->rsp_cap; h; h = h->next)
7397 pool_free2(h->pool, txn->rsp.cap[h->index]);
7398 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7399 }
7400
Willy Tarreau0937bc42009-12-22 15:03:09 +01007401}
7402
7403/* to be used at the end of a transaction to prepare a new one */
7404void http_reset_txn(struct session *s)
7405{
7406 http_end_txn(s);
7407 http_init_txn(s);
7408
7409 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007410 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007411 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007412 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007413 /* re-init store persistence */
7414 s->store_count = 0;
7415
Willy Tarreau0937bc42009-12-22 15:03:09 +01007416 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007417
7418 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7419
Willy Tarreau739cfba2010-01-25 23:11:14 +01007420 /* We must trim any excess data from the response buffer, because we
7421 * may have blocked an invalid response from a server that we don't
7422 * want to accidentely forward once we disable the analysers, nor do
7423 * we want those data to come along with next response. A typical
7424 * example of such data would be from a buggy server responding to
7425 * a HEAD with some data, or sending more than the advertised
7426 * content-length.
7427 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007428 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007429 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007430
Willy Tarreau0937bc42009-12-22 15:03:09 +01007431 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007432 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007433
Willy Tarreaud04e8582010-05-31 12:31:35 +02007434 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007435 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007436
7437 s->req->rex = TICK_ETERNITY;
7438 s->req->wex = TICK_ETERNITY;
7439 s->req->analyse_exp = TICK_ETERNITY;
7440 s->rep->rex = TICK_ETERNITY;
7441 s->rep->wex = TICK_ETERNITY;
7442 s->rep->analyse_exp = TICK_ETERNITY;
7443}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007444
Willy Tarreauff011f22011-01-06 17:51:27 +01007445void free_http_req_rules(struct list *r) {
7446 struct http_req_rule *tr, *pr;
7447
7448 list_for_each_entry_safe(pr, tr, r, list) {
7449 LIST_DEL(&pr->list);
7450 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7451 free(pr->http_auth.realm);
7452
7453 free(pr);
7454 }
7455}
7456
7457struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7458{
7459 struct http_req_rule *rule;
7460 int cur_arg;
7461
7462 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7463 if (!rule) {
7464 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7465 return NULL;
7466 }
7467
7468 if (!*args[0]) {
7469 goto req_error_parsing;
7470 } else if (!strcmp(args[0], "allow")) {
7471 rule->action = HTTP_REQ_ACT_ALLOW;
7472 cur_arg = 1;
7473 } else if (!strcmp(args[0], "deny")) {
7474 rule->action = HTTP_REQ_ACT_DENY;
7475 cur_arg = 1;
7476 } else if (!strcmp(args[0], "auth")) {
7477 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7478 cur_arg = 1;
7479
7480 while(*args[cur_arg]) {
7481 if (!strcmp(args[cur_arg], "realm")) {
7482 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7483 cur_arg+=2;
7484 continue;
7485 } else
7486 break;
7487 }
7488 } else {
7489req_error_parsing:
7490 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7491 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7492 return NULL;
7493 }
7494
7495 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7496 struct acl_cond *cond;
7497
7498 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7499 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7500 file, linenum, args[0]);
7501 return NULL;
7502 }
7503 rule->cond = cond;
7504 }
7505 else if (*args[cur_arg]) {
7506 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7507 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7508 file, linenum, args[0], args[cur_arg]);
7509 return NULL;
7510 }
7511
7512 return rule;
7513}
7514
Willy Tarreau8797c062007-05-07 00:55:35 +02007515/************************************************************************/
7516/* The code below is dedicated to ACL parsing and matching */
7517/************************************************************************/
7518
7519
7520
7521
7522/* 1. Check on METHOD
7523 * We use the pre-parsed method if it is known, and store its number as an
7524 * integer. If it is unknown, we use the pointer and the length.
7525 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007526static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007527{
7528 int len, meth;
7529
Willy Tarreauae8b7962007-06-09 23:10:04 +02007530 len = strlen(*text);
7531 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007532
7533 pattern->val.i = meth;
7534 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007535 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007536 if (!pattern->ptr.str)
7537 return 0;
7538 pattern->len = len;
7539 }
7540 return 1;
7541}
7542
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007543/* This function fetches the method of current HTTP request and stores
7544 * it in the global pattern struct as a chunk. There are two possibilities :
7545 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7546 * in <len> and <ptr> is NULL ;
7547 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7548 * <len> to its length.
7549 * This is intended to be used with acl_match_meth() only.
7550 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007551static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007552acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7553 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007554{
7555 int meth;
7556 struct http_txn *txn = l7;
7557
Willy Tarreaub6866442008-07-14 23:54:42 +02007558 if (!txn)
7559 return 0;
7560
Willy Tarreau655dce92009-11-08 13:10:58 +01007561 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007562 return 0;
7563
Willy Tarreau8797c062007-05-07 00:55:35 +02007564 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007565 temp_pattern.data.str.len = meth;
7566 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007567 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007568 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7569 /* ensure the indexes are not affected */
7570 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007571 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007572 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007573 }
7574 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7575 return 1;
7576}
7577
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007578/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007579static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7580{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007581 int icase;
7582
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007583
7584 if (temp_pattern.data.str.str == NULL) {
7585 /* well-known method */
7586 if (temp_pattern.data.str.len == pattern->val.i)
7587 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007588 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007589 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007590
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007591 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7592 if (pattern->val.i != HTTP_METH_OTHER)
7593 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007594
7595 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007596 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007597 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007598
7599 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007600 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7601 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007602 return ACL_PAT_FAIL;
7603 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007604}
7605
7606/* 2. Check on Request/Status Version
7607 * We simply compare strings here.
7608 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007609static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007610{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007611 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007612 if (!pattern->ptr.str)
7613 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007614 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007615 return 1;
7616}
7617
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007618static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007619acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7620 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007621{
7622 struct http_txn *txn = l7;
7623 char *ptr;
7624 int len;
7625
Willy Tarreaub6866442008-07-14 23:54:42 +02007626 if (!txn)
7627 return 0;
7628
Willy Tarreau655dce92009-11-08 13:10:58 +01007629 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007630 return 0;
7631
Willy Tarreau8797c062007-05-07 00:55:35 +02007632 len = txn->req.sl.rq.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007633 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007634
7635 while ((len-- > 0) && (*ptr++ != '/'));
7636 if (len <= 0)
7637 return 0;
7638
Willy Tarreau664092c2011-12-16 19:11:42 +01007639 temp_pattern.data.str.str = ptr;
7640 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007641
7642 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7643 return 1;
7644}
7645
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007646static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007647acl_fetch_stver(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 struct http_txn *txn = l7;
7651 char *ptr;
7652 int len;
7653
Willy Tarreaub6866442008-07-14 23:54:42 +02007654 if (!txn)
7655 return 0;
7656
Willy Tarreau655dce92009-11-08 13:10:58 +01007657 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007658 return 0;
7659
Willy Tarreau8797c062007-05-07 00:55:35 +02007660 len = txn->rsp.sl.st.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007661 ptr = txn->rsp.buf->p + txn->rsp.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007662
7663 while ((len-- > 0) && (*ptr++ != '/'));
7664 if (len <= 0)
7665 return 0;
7666
Willy Tarreau664092c2011-12-16 19:11:42 +01007667 temp_pattern.data.str.str = ptr;
7668 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007669
7670 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7671 return 1;
7672}
7673
7674/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007675static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007676acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7677 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007678{
7679 struct http_txn *txn = l7;
7680 char *ptr;
7681 int len;
7682
Willy Tarreaub6866442008-07-14 23:54:42 +02007683 if (!txn)
7684 return 0;
7685
Willy Tarreau655dce92009-11-08 13:10:58 +01007686 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007687 return 0;
7688
Willy Tarreau8797c062007-05-07 00:55:35 +02007689 len = txn->rsp.sl.st.c_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007690 ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007691
Willy Tarreaua5e37562011-12-16 17:06:15 +01007692 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007693 test->flags = ACL_TEST_F_VOL_1ST;
7694 return 1;
7695}
7696
7697/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007698static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007699acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7700 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007701{
7702 struct http_txn *txn = l7;
7703
Willy Tarreaub6866442008-07-14 23:54:42 +02007704 if (!txn)
7705 return 0;
7706
Willy Tarreau655dce92009-11-08 13:10:58 +01007707 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007708 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007709
Willy Tarreauc11416f2007-06-17 16:58:38 +02007710 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7711 /* ensure the indexes are not affected */
7712 return 0;
7713
Willy Tarreau664092c2011-12-16 19:11:42 +01007714 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007715 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007716
Willy Tarreauf3d25982007-05-08 22:45:09 +02007717 /* we do not need to set READ_ONLY because the data is in a buffer */
7718 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007719 return 1;
7720}
7721
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007722static int
7723acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7724 struct acl_expr *expr, struct acl_test *test)
7725{
7726 struct http_txn *txn = l7;
7727
Willy Tarreaub6866442008-07-14 23:54:42 +02007728 if (!txn)
7729 return 0;
7730
Willy Tarreau655dce92009-11-08 13:10:58 +01007731 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007732 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007733
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007734 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7735 /* ensure the indexes are not affected */
7736 return 0;
7737
7738 /* Parse HTTP request */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007739 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 +01007740 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7741 return 0;
7742 temp_pattern.type = PATTERN_TYPE_IP;
7743 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007744
7745 /*
7746 * If we are parsing url in frontend space, we prepare backend stage
7747 * to not parse again the same url ! optimization lazyness...
7748 */
7749 if (px->options & PR_O_HTTP_PROXY)
7750 l4->flags |= SN_ADDR_SET;
7751
Willy Tarreauf4362b32011-12-16 17:49:52 +01007752 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007753 return 1;
7754}
7755
7756static int
7757acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7758 struct acl_expr *expr, struct acl_test *test)
7759{
7760 struct http_txn *txn = l7;
7761
Willy Tarreaub6866442008-07-14 23:54:42 +02007762 if (!txn)
7763 return 0;
7764
Willy Tarreau655dce92009-11-08 13:10:58 +01007765 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007766 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007767
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007768 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7769 /* ensure the indexes are not affected */
7770 return 0;
7771
7772 /* Same optimization as url_ip */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007773 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 +01007774 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007775
7776 if (px->options & PR_O_HTTP_PROXY)
7777 l4->flags |= SN_ADDR_SET;
7778
7779 test->flags = ACL_TEST_F_READ_ONLY;
7780 return 1;
7781}
7782
Willy Tarreauc11416f2007-06-17 16:58:38 +02007783/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7784 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7785 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007786static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007787acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007788 struct acl_expr *expr, struct acl_test *test)
7789{
7790 struct http_txn *txn = l7;
7791 struct hdr_idx *idx = &txn->hdr_idx;
7792 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007793
Willy Tarreaub6866442008-07-14 23:54:42 +02007794 if (!txn)
7795 return 0;
7796
Willy Tarreau33a7e692007-06-10 19:45:56 +02007797 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7798 /* search for header from the beginning */
7799 ctx->idx = 0;
7800
Willy Tarreau33a7e692007-06-10 19:45:56 +02007801 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7802 test->flags |= ACL_TEST_F_FETCH_MORE;
7803 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007804 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7805 temp_pattern.data.str.len = ctx->vlen;
7806
Willy Tarreau33a7e692007-06-10 19:45:56 +02007807 return 1;
7808 }
7809
7810 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7811 test->flags |= ACL_TEST_F_VOL_HDR;
7812 return 0;
7813}
7814
Willy Tarreau33a7e692007-06-10 19:45:56 +02007815static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007816acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7817 struct acl_expr *expr, struct acl_test *test)
7818{
7819 struct http_txn *txn = l7;
7820
Willy Tarreaub6866442008-07-14 23:54:42 +02007821 if (!txn)
7822 return 0;
7823
Willy Tarreau655dce92009-11-08 13:10:58 +01007824 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007825 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007826
Willy Tarreauc11416f2007-06-17 16:58:38 +02007827 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7828 /* ensure the indexes are not affected */
7829 return 0;
7830
Willy Tarreau3a215be2012-03-09 21:39:51 +01007831 return acl_fetch_hdr(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007832}
7833
7834static int
7835acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7836 struct acl_expr *expr, struct acl_test *test)
7837{
7838 struct http_txn *txn = l7;
7839
Willy Tarreaub6866442008-07-14 23:54:42 +02007840 if (!txn)
7841 return 0;
7842
Willy Tarreau655dce92009-11-08 13:10:58 +01007843 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007844 return 0;
7845
Willy Tarreau3a215be2012-03-09 21:39:51 +01007846 return acl_fetch_hdr(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007847}
7848
7849/* 6. Check on HTTP header count. The number of occurrences is returned.
7850 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7851 */
7852static int
7853acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007854 struct acl_expr *expr, struct acl_test *test)
7855{
7856 struct http_txn *txn = l7;
7857 struct hdr_idx *idx = &txn->hdr_idx;
7858 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007859 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007860
Willy Tarreaub6866442008-07-14 23:54:42 +02007861 if (!txn)
7862 return 0;
7863
Willy Tarreau33a7e692007-06-10 19:45:56 +02007864 ctx.idx = 0;
7865 cnt = 0;
7866 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7867 cnt++;
7868
Willy Tarreaua5e37562011-12-16 17:06:15 +01007869 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007870 test->flags = ACL_TEST_F_VOL_HDR;
7871 return 1;
7872}
7873
Willy Tarreauc11416f2007-06-17 16:58:38 +02007874static int
7875acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7876 struct acl_expr *expr, struct acl_test *test)
7877{
7878 struct http_txn *txn = l7;
7879
Willy Tarreaub6866442008-07-14 23:54:42 +02007880 if (!txn)
7881 return 0;
7882
Willy Tarreau655dce92009-11-08 13:10:58 +01007883 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007884 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007885
Willy Tarreauc11416f2007-06-17 16:58:38 +02007886 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7887 /* ensure the indexes are not affected */
7888 return 0;
7889
Willy Tarreau3a215be2012-03-09 21:39:51 +01007890 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007891}
7892
7893static int
7894acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7895 struct acl_expr *expr, struct acl_test *test)
7896{
7897 struct http_txn *txn = l7;
7898
Willy Tarreaub6866442008-07-14 23:54:42 +02007899 if (!txn)
7900 return 0;
7901
Willy Tarreau655dce92009-11-08 13:10:58 +01007902 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007903 return 0;
7904
Willy Tarreau3a215be2012-03-09 21:39:51 +01007905 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007906}
7907
Willy Tarreau33a7e692007-06-10 19:45:56 +02007908/* 7. Check on HTTP header's integer value. The integer value is returned.
7909 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007910 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007911 */
7912static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007913acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007914 struct acl_expr *expr, struct acl_test *test)
7915{
7916 struct http_txn *txn = l7;
7917 struct hdr_idx *idx = &txn->hdr_idx;
7918 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007919
Willy Tarreaub6866442008-07-14 23:54:42 +02007920 if (!txn)
7921 return 0;
7922
Willy Tarreau33a7e692007-06-10 19:45:56 +02007923 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7924 /* search for header from the beginning */
7925 ctx->idx = 0;
7926
Willy Tarreau33a7e692007-06-10 19:45:56 +02007927 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7928 test->flags |= ACL_TEST_F_FETCH_MORE;
7929 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007930 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007931 return 1;
7932 }
7933
7934 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7935 test->flags |= ACL_TEST_F_VOL_HDR;
7936 return 0;
7937}
7938
Willy Tarreauc11416f2007-06-17 16:58:38 +02007939static int
7940acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7941 struct acl_expr *expr, struct acl_test *test)
7942{
7943 struct http_txn *txn = l7;
7944
Willy Tarreaub6866442008-07-14 23:54:42 +02007945 if (!txn)
7946 return 0;
7947
Willy Tarreau655dce92009-11-08 13:10:58 +01007948 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007949 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007950
Willy Tarreauc11416f2007-06-17 16:58:38 +02007951 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7952 /* ensure the indexes are not affected */
7953 return 0;
7954
Willy Tarreau3a215be2012-03-09 21:39:51 +01007955 return acl_fetch_hdr_val(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007956}
7957
7958static int
7959acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7960 struct acl_expr *expr, struct acl_test *test)
7961{
7962 struct http_txn *txn = l7;
7963
Willy Tarreaub6866442008-07-14 23:54:42 +02007964 if (!txn)
7965 return 0;
7966
Willy Tarreau655dce92009-11-08 13:10:58 +01007967 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007968 return 0;
7969
Willy Tarreau3a215be2012-03-09 21:39:51 +01007970 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007971}
7972
Willy Tarreau106f9792009-09-19 07:54:16 +02007973/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7974 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7975 */
7976static int
7977acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7978 struct acl_expr *expr, struct acl_test *test)
7979{
7980 struct http_txn *txn = l7;
7981 struct hdr_idx *idx = &txn->hdr_idx;
7982 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7983
7984 if (!txn)
7985 return 0;
7986
7987 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7988 /* search for header from the beginning */
7989 ctx->idx = 0;
7990
Willy Tarreauf4362b32011-12-16 17:49:52 +01007991 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02007992 test->flags |= ACL_TEST_F_FETCH_MORE;
7993 test->flags |= ACL_TEST_F_VOL_HDR;
7994 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01007995 temp_pattern.type = PATTERN_TYPE_IP;
7996 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
7997 return 1;
7998 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02007999 }
8000
8001 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8002 test->flags |= ACL_TEST_F_VOL_HDR;
8003 return 0;
8004}
8005
8006static int
8007acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8008 struct acl_expr *expr, struct acl_test *test)
8009{
8010 struct http_txn *txn = l7;
8011
8012 if (!txn)
8013 return 0;
8014
Willy Tarreau655dce92009-11-08 13:10:58 +01008015 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008016 return 0;
8017
8018 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8019 /* ensure the indexes are not affected */
8020 return 0;
8021
Willy Tarreau3a215be2012-03-09 21:39:51 +01008022 return acl_fetch_hdr_ip(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008023}
8024
8025static int
8026acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8027 struct acl_expr *expr, struct acl_test *test)
8028{
8029 struct http_txn *txn = l7;
8030
8031 if (!txn)
8032 return 0;
8033
Willy Tarreau655dce92009-11-08 13:10:58 +01008034 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008035 return 0;
8036
Willy Tarreau3a215be2012-03-09 21:39:51 +01008037 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008038}
8039
Willy Tarreau737b0c12007-06-10 21:28:46 +02008040/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8041 * the first '/' after the possible hostname, and ends before the possible '?'.
8042 */
8043static int
8044acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8045 struct acl_expr *expr, struct acl_test *test)
8046{
8047 struct http_txn *txn = l7;
8048 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008049
Willy Tarreaub6866442008-07-14 23:54:42 +02008050 if (!txn)
8051 return 0;
8052
Willy Tarreau655dce92009-11-08 13:10:58 +01008053 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008054 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008055
Willy Tarreauc11416f2007-06-17 16:58:38 +02008056 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8057 /* ensure the indexes are not affected */
8058 return 0;
8059
Willy Tarreau3a215be2012-03-09 21:39:51 +01008060 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 +01008061 ptr = http_get_path(txn);
8062 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008063 return 0;
8064
8065 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008066 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008067
8068 while (ptr < end && *ptr != '?')
8069 ptr++;
8070
Willy Tarreau664092c2011-12-16 19:11:42 +01008071 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008072
8073 /* we do not need to set READ_ONLY because the data is in a buffer */
8074 test->flags = ACL_TEST_F_VOL_1ST;
8075 return 1;
8076}
8077
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008078static int
8079acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8080 struct acl_expr *expr, struct acl_test *test)
8081{
8082 struct buffer *req = s->req;
8083 struct http_txn *txn = &s->txn;
8084 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008085
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008086 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8087 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8088 */
8089
8090 if (!s || !req)
8091 return 0;
8092
Willy Tarreau655dce92009-11-08 13:10:58 +01008093 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008094 /* Already decoded as OK */
8095 test->flags |= ACL_TEST_F_SET_RES_PASS;
8096 return 1;
8097 }
8098
8099 /* Try to decode HTTP request */
Willy Tarreaua458b672012-03-05 11:17:50 +01008100 if (likely(msg->next < req->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01008101 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008102
Willy Tarreau655dce92009-11-08 13:10:58 +01008103 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008104 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8105 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8106 return 1;
8107 }
8108 /* wait for final state */
8109 test->flags |= ACL_TEST_F_MAY_CHANGE;
8110 return 0;
8111 }
8112
8113 /* OK we got a valid HTTP request. We have some minor preparation to
8114 * perform so that further checks can rely on HTTP tests.
8115 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008116 txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008117 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8118 s->flags |= SN_REDIRECTABLE;
8119
8120 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) {
8121 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8122 return 1;
8123 }
8124
8125 test->flags |= ACL_TEST_F_SET_RES_PASS;
8126 return 1;
8127}
8128
Willy Tarreau7f18e522010-10-22 20:04:13 +02008129/* return a valid test if the current request is the first one on the connection */
8130static int
8131acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8132 struct acl_expr *expr, struct acl_test *test)
8133{
8134 if (!s)
8135 return 0;
8136
8137 if (s->txn.flags & TX_NOT_FIRST)
8138 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8139 else
8140 test->flags |= ACL_TEST_F_SET_RES_PASS;
8141
8142 return 1;
8143}
8144
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008145static int
8146acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8147 struct acl_expr *expr, struct acl_test *test)
8148{
8149
8150 if (!s)
8151 return 0;
8152
8153 if (!get_http_auth(s))
8154 return 0;
8155
8156 test->ctx.a[0] = expr->arg.ul;
8157 test->ctx.a[1] = s->txn.auth.user;
8158 test->ctx.a[2] = s->txn.auth.pass;
8159
8160 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8161
8162 return 1;
8163}
Willy Tarreau8797c062007-05-07 00:55:35 +02008164
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008165/* Try to find the next occurrence of a cookie name in a cookie header value.
8166 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8167 * the cookie value is returned into *value and *value_l, and the function
8168 * returns a pointer to the next pointer to search from if the value was found.
8169 * Otherwise if the cookie was not found, NULL is returned and neither value
8170 * nor value_l are touched. The input <hdr> string should first point to the
8171 * header's value, and the <hdr_end> pointer must point to the first character
8172 * not part of the value. <list> must be non-zero if value may represent a list
8173 * of values (cookie headers). This makes it faster to abort parsing when no
8174 * list is expected.
8175 */
8176static char *
8177extract_cookie_value(char *hdr, const char *hdr_end,
8178 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008179 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008180{
8181 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8182 char *next;
8183
8184 /* we search at least a cookie name followed by an equal, and more
8185 * generally something like this :
8186 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8187 */
8188 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8189 /* Iterate through all cookies on this line */
8190
8191 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8192 att_beg++;
8193
8194 /* find att_end : this is the first character after the last non
8195 * space before the equal. It may be equal to hdr_end.
8196 */
8197 equal = att_end = att_beg;
8198
8199 while (equal < hdr_end) {
8200 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8201 break;
8202 if (http_is_spht[(unsigned char)*equal++])
8203 continue;
8204 att_end = equal;
8205 }
8206
8207 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8208 * is between <att_beg> and <equal>, both may be identical.
8209 */
8210
8211 /* look for end of cookie if there is an equal sign */
8212 if (equal < hdr_end && *equal == '=') {
8213 /* look for the beginning of the value */
8214 val_beg = equal + 1;
8215 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8216 val_beg++;
8217
8218 /* find the end of the value, respecting quotes */
8219 next = find_cookie_value_end(val_beg, hdr_end);
8220
8221 /* make val_end point to the first white space or delimitor after the value */
8222 val_end = next;
8223 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8224 val_end--;
8225 } else {
8226 val_beg = val_end = next = equal;
8227 }
8228
8229 /* We have nothing to do with attributes beginning with '$'. However,
8230 * they will automatically be removed if a header before them is removed,
8231 * since they're supposed to be linked together.
8232 */
8233 if (*att_beg == '$')
8234 continue;
8235
8236 /* Ignore cookies with no equal sign */
8237 if (equal == next)
8238 continue;
8239
8240 /* Now we have the cookie name between att_beg and att_end, and
8241 * its value between val_beg and val_end.
8242 */
8243
8244 if (att_end - att_beg == cookie_name_l &&
8245 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8246 /* let's return this value and indicate where to go on from */
8247 *value = val_beg;
8248 *value_l = val_end - val_beg;
8249 return next + 1;
8250 }
8251
8252 /* Set-Cookie headers only have the name in the first attr=value part */
8253 if (!list)
8254 break;
8255 }
8256
8257 return NULL;
8258}
8259
8260/* Iterate over all cookies present in a request. The context is stored in
8261 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8262 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8263 * non-null, then multiple cookies may be parsed on the same line.
8264 * The cookie name is in expr->arg and the name length in expr->arg_len.
8265 */
8266static int
8267acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8268 const char *hdr_name, int hdr_name_len, int multi,
8269 struct acl_expr *expr, struct acl_test *test)
8270{
8271 struct http_txn *txn = l7;
8272 struct hdr_idx *idx = &txn->hdr_idx;
8273 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8274
8275 if (!txn)
8276 return 0;
8277
8278 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8279 /* search for the header from the beginning, we must first initialize
8280 * the search parameters.
8281 */
8282 test->ctx.a[0] = NULL;
8283 ctx->idx = 0;
8284 }
8285
8286 while (1) {
8287 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8288 if (!test->ctx.a[0]) {
8289 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8290 goto out;
8291
8292 if (ctx->vlen < expr->arg_len + 1)
8293 continue;
8294
8295 test->ctx.a[0] = ctx->line + ctx->val;
8296 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8297 }
8298
8299 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8300 expr->arg.str, expr->arg_len, multi,
8301 &temp_pattern.data.str.str,
8302 &temp_pattern.data.str.len);
8303 if (test->ctx.a[0]) {
8304 /* one value was returned into temp_pattern.data.str.{str,len} */
8305 test->flags |= ACL_TEST_F_FETCH_MORE;
8306 test->flags |= ACL_TEST_F_VOL_HDR;
8307 return 1;
8308 }
8309 }
8310
8311 out:
8312 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8313 test->flags |= ACL_TEST_F_VOL_HDR;
8314 return 0;
8315}
8316
8317static int
8318acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8319 struct acl_expr *expr, struct acl_test *test)
8320{
8321 struct http_txn *txn = l7;
8322
8323 if (!txn)
8324 return 0;
8325
8326 if (txn->req.msg_state < HTTP_MSG_BODY)
8327 return 0;
8328
8329 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8330 /* ensure the indexes are not affected */
8331 return 0;
8332
8333 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008334 return acl_fetch_any_cookie_value(px, l4, txn, txn->req.buf->p + txn->req.sol, "Cookie", 6, 1, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008335}
8336
8337static int
8338acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8339 struct acl_expr *expr, struct acl_test *test)
8340{
8341 struct http_txn *txn = l7;
8342
8343 if (!txn)
8344 return 0;
8345
8346 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8347 return 0;
8348
8349 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008350 return acl_fetch_any_cookie_value(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008351}
8352
8353/* Iterate over all cookies present in a request to count how many occurrences
8354 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8355 * multiple cookies may be parsed on the same line.
8356 */
8357static int
8358acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8359 const char *hdr_name, int hdr_name_len, int multi,
8360 struct acl_expr *expr, struct acl_test *test)
8361{
8362 struct http_txn *txn = l7;
8363 struct hdr_idx *idx = &txn->hdr_idx;
8364 struct hdr_ctx ctx;
8365 int cnt;
8366 char *val_beg, *val_end;
8367
8368 if (!txn)
8369 return 0;
8370
Willy Tarreau46787ed2012-04-11 17:28:40 +02008371 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008372 ctx.idx = 0;
8373 cnt = 0;
8374
8375 while (1) {
8376 /* Note: val_beg == NULL every time we need to fetch a new header */
8377 if (!val_beg) {
8378 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8379 break;
8380
8381 if (ctx.vlen < expr->arg_len + 1)
8382 continue;
8383
8384 val_beg = ctx.line + ctx.val;
8385 val_end = val_beg + ctx.vlen;
8386 }
8387
8388 while ((val_beg = extract_cookie_value(val_beg, val_end,
8389 expr->arg.str, expr->arg_len, multi,
8390 &temp_pattern.data.str.str,
8391 &temp_pattern.data.str.len))) {
8392 cnt++;
8393 }
8394 }
8395
8396 temp_pattern.data.integer = cnt;
8397 test->flags |= ACL_TEST_F_VOL_HDR;
8398 return 1;
8399}
8400
8401static int
8402acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8403 struct acl_expr *expr, struct acl_test *test)
8404{
8405 struct http_txn *txn = l7;
8406
8407 if (!txn)
8408 return 0;
8409
8410 if (txn->req.msg_state < HTTP_MSG_BODY)
8411 return 0;
8412
8413 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8414 /* ensure the indexes are not affected */
8415 return 0;
8416
8417 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008418 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, "Cookie", 6, 1, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008419}
8420
8421static int
8422acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8423 struct acl_expr *expr, struct acl_test *test)
8424{
8425 struct http_txn *txn = l7;
8426
8427 if (!txn)
8428 return 0;
8429
8430 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8431 return 0;
8432
8433 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008434 return acl_fetch_any_cookie_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, "Set-Cookie", 10, 0, expr, test);
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008435}
8436
Willy Tarreau8797c062007-05-07 00:55:35 +02008437/************************************************************************/
8438/* All supported keywords must be declared here. */
8439/************************************************************************/
8440
8441/* Note: must not be declared <const> as its list will be overwritten */
8442static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008443 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8444
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008445 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008446 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8447 { "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 +02008448 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008449
Willy Tarreauc4262962010-05-10 23:42:40 +02008450 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008451 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8452 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8453 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8454 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8455 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8456 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008457 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008458 { "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 +02008459 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008460
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008461 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008462 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008463 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8464 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8465 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8466 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8467 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8468 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8469 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008470 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008471 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008472 { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreauc11416f2007-06-17 16:58:38 +02008473
Willy Tarreauc4262962010-05-10 23:42:40 +02008474 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008475 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8476 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8477 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8478 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8479 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8480 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8481 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008482 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008483 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008484 { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008485
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008486 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8487 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8488 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8489 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8490 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8491 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8492 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8493 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8494 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8495
8496 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8497 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8498 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8499 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8500 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8501 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8502 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8503 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8504 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8505
Willy Tarreauc4262962010-05-10 23:42:40 +02008506 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008507 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8508 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8509 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8510 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8511 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8512 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008513 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008514
Willy Tarreau7f18e522010-10-22 20:04:13 +02008515 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8516 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8517 { "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 +02008518 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008519}};
8520
Willy Tarreau4a568972010-05-12 08:08:50 +02008521/************************************************************************/
8522/* The code below is dedicated to pattern fetching and matching */
8523/************************************************************************/
8524
Willy Tarreaue428fb72011-12-16 21:50:30 +01008525/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008526static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008527pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8528 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008529{
8530 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008531
Willy Tarreaue428fb72011-12-16 21:50:30 +01008532 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8533 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008534}
8535
David Cournapeau16023ee2010-12-23 20:55:41 +09008536/*
8537 * Given a path string and its length, find the position of beginning of the
8538 * query string. Returns NULL if no query string is found in the path.
8539 *
8540 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8541 *
8542 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8543 */
8544static inline char *find_query_string(char *path, size_t path_l)
8545{
8546 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008547
David Cournapeau16023ee2010-12-23 20:55:41 +09008548 p = memchr(path, '?', path_l);
8549 return p ? p + 1 : NULL;
8550}
8551
8552static inline int is_param_delimiter(char c)
8553{
8554 return c == '&' || c == ';';
8555}
8556
8557/*
8558 * Given a url parameter, find the starting position of the first occurence,
8559 * or NULL if the parameter is not found.
8560 *
8561 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8562 * the function will return query_string+8.
8563 */
8564static char*
8565find_url_param_pos(char* query_string, size_t query_string_l,
8566 char* url_param_name, size_t url_param_name_l)
8567{
8568 char *pos, *last;
8569
8570 pos = query_string;
8571 last = query_string + query_string_l - url_param_name_l - 1;
8572
8573 while (pos <= last) {
8574 if (pos[url_param_name_l] == '=') {
8575 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8576 return pos;
8577 pos += url_param_name_l + 1;
8578 }
8579 while (pos <= last && !is_param_delimiter(*pos))
8580 pos++;
8581 pos++;
8582 }
8583 return NULL;
8584}
8585
8586/*
8587 * Given a url parameter name, returns its value and size into *value and
8588 * *value_l respectively, and returns non-zero. If the parameter is not found,
8589 * zero is returned and value/value_l are not touched.
8590 */
8591static int
8592find_url_param_value(char* path, size_t path_l,
8593 char* url_param_name, size_t url_param_name_l,
8594 char** value, size_t* value_l)
8595{
8596 char *query_string, *qs_end;
8597 char *arg_start;
8598 char *value_start, *value_end;
8599
8600 query_string = find_query_string(path, path_l);
8601 if (!query_string)
8602 return 0;
8603
8604 qs_end = path + path_l;
8605 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8606 url_param_name, url_param_name_l);
8607 if (!arg_start)
8608 return 0;
8609
8610 value_start = arg_start + url_param_name_l + 1;
8611 value_end = value_start;
8612
8613 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8614 value_end++;
8615
8616 *value = value_start;
8617 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008618 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008619}
8620
8621static int
8622pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8623 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8624{
8625 struct http_txn *txn = l7;
8626 struct http_msg *msg = &txn->req;
8627 char *url_param_value;
8628 size_t url_param_value_l;
8629
Willy Tarreau3a215be2012-03-09 21:39:51 +01008630 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 +09008631 arg_p->data.str.str, arg_p->data.str.len,
8632 &url_param_value, &url_param_value_l))
8633 return 0;
8634
8635 data->str.str = url_param_value;
8636 data->str.len = url_param_value_l;
8637 return 1;
8638}
8639
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008640/* Try to find in request or response message is in <msg> and whose transaction
8641 * is in <txn> the last occurrence of a cookie name in all cookie header values
8642 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8643 * pointer and size of the last occurrence of the cookie value is returned into
8644 * <value> and <value_l>, and the function returns non-zero if the value was
8645 * found. Otherwise if the cookie was not found, zero is returned and neither
8646 * value nor value_l are touched. The input hdr string should begin at the
8647 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8648 * value may represent a list of values (cookie headers).
8649 */
8650
8651static int
8652find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8653 const char *hdr_name, int hdr_name_len,
8654 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008655 char **value, int *value_l)
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008656{
8657 struct hdr_ctx ctx;
8658 int found = 0;
8659
8660 ctx.idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008661 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 +02008662 char *hdr, *end;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008663 if (ctx.vlen < cookie_name_l + 1)
8664 continue;
8665
Willy Tarreau4573af92012-04-06 18:20:06 +02008666 hdr = ctx.line + ctx.val;
8667 end = hdr + ctx.vlen;
8668 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8669 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008670 }
8671 return found;
8672}
8673
8674static int
8675pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8676 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8677{
8678 struct http_txn *txn = l7;
8679 struct http_msg *msg = &txn->req;
8680 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008681 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008682 int found = 0;
8683
8684 found = find_cookie_value(msg, txn, "Cookie", 6,
8685 arg_p->data.str.str, arg_p->data.str.len, 1,
8686 &cookie_value, &cookie_value_l);
8687 if (found) {
8688 data->str.str = cookie_value;
8689 data->str.len = cookie_value_l;
8690 }
8691
8692 return found;
8693}
8694
8695
8696static int
8697pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8698 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8699{
8700 struct http_txn *txn = l7;
8701 struct http_msg *msg = &txn->rsp;
8702 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008703 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008704 int found = 0;
8705
8706 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8707 arg_p->data.str.str, arg_p->data.str.len, 1,
8708 &cookie_value, &cookie_value_l);
8709 if (found) {
8710 data->str.str = cookie_value;
8711 data->str.len = cookie_value_l;
8712 }
8713
8714 return found;
8715}
8716
Emeric Brun485479d2010-09-23 18:02:19 +02008717
Willy Tarreau4a568972010-05-12 08:08:50 +02008718/************************************************************************/
8719/* All supported keywords must be declared here. */
8720/************************************************************************/
8721/* Note: must not be declared <const> as its list will be overwritten */
8722static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008723 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008724 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008725 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8726 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008727 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008728}};
8729
Willy Tarreau8797c062007-05-07 00:55:35 +02008730
8731__attribute__((constructor))
8732static void __http_protocol_init(void)
8733{
8734 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008735 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008736}
8737
8738
Willy Tarreau58f10d72006-12-04 02:26:12 +01008739/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008740 * Local variables:
8741 * c-indent-level: 8
8742 * c-basic-offset: 8
8743 * End:
8744 */