blob: 1b2bfbde32d2c0e852a9ec239dd5b3ad7f7760cb [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 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001589static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001590{
1591 int delta;
1592 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001593 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001594
1595 if (msg->sl.rq.v_l != 0)
1596 return 1;
1597
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001598 cur_end = msg->buf->p + msg->sol + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001599 delta = 0;
1600
1601 if (msg->sl.rq.u_l == 0) {
1602 /* if no URI was set, add "/" */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001603 delta = buffer_replace2(msg->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001604 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001605 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001606 }
1607 /* add HTTP version */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001608 delta = buffer_replace2(msg->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001609 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001610 cur_end += delta;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001611 cur_end = (char *)http_parse_reqline(msg, msg->buf->data,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612 HTTP_MSG_RQMETH,
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001613 msg->buf->p + msg->sol, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 NULL, NULL);
1615 if (unlikely(!cur_end))
1616 return 0;
1617
1618 /* we have a full HTTP/1.0 request now and we know that
1619 * we have either a CR or an LF at <ptr>.
1620 */
1621 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1622 return 1;
1623}
1624
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001625/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001626 * and "keep-alive" values. If we already know that some headers may safely
1627 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1629 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1630 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1631 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1632 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001633 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001634 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001635void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001636{
Willy Tarreau5b154472009-12-21 20:11:07 +01001637 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001638 const char *hdr_val = "Connection";
1639 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001640
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001641 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 return;
1643
Willy Tarreau88d349d2010-01-25 12:15:43 +01001644 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1645 hdr_val = "Proxy-Connection";
1646 hdr_len = 16;
1647 }
1648
Willy Tarreau5b154472009-12-21 20:11:07 +01001649 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001650 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001651 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001652 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1653 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001654 if (to_del & 2)
1655 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001656 else
1657 txn->flags |= TX_CON_KAL_SET;
1658 }
1659 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1660 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001661 if (to_del & 1)
1662 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001663 else
1664 txn->flags |= TX_CON_CLO_SET;
1665 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001666 }
1667
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001668 txn->flags |= TX_HDR_CONN_PRS;
1669 return;
1670}
Willy Tarreau5b154472009-12-21 20:11:07 +01001671
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001672/* Apply desired changes on the Connection: header. Values may be removed and/or
1673 * added depending on the <wanted> flags, which are exclusively composed of
1674 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1675 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1676 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001677void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001678{
1679 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001680 const char *hdr_val = "Connection";
1681 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001682
1683 ctx.idx = 0;
1684
Willy Tarreau88d349d2010-01-25 12:15:43 +01001685
1686 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1687 hdr_val = "Proxy-Connection";
1688 hdr_len = 16;
1689 }
1690
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001691 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001692 while (http_find_header2(hdr_val, hdr_len, msg->buf->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001693 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1694 if (wanted & TX_CON_KAL_SET)
1695 txn->flags |= TX_CON_KAL_SET;
1696 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001697 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001698 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1700 if (wanted & TX_CON_CLO_SET)
1701 txn->flags |= TX_CON_CLO_SET;
1702 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001703 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001704 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001705 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001706
1707 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1708 return;
1709
1710 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1711 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001712 hdr_val = "Connection: close";
1713 hdr_len = 17;
1714 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1715 hdr_val = "Proxy-Connection: close";
1716 hdr_len = 23;
1717 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001718 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001719 }
1720
1721 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1722 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001723 hdr_val = "Connection: keep-alive";
1724 hdr_len = 22;
1725 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1726 hdr_val = "Proxy-Connection: keep-alive";
1727 hdr_len = 28;
1728 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001729 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001730 }
1731 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001732}
1733
Willy Tarreaua458b672012-03-05 11:17:50 +01001734/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001735 * first byte of body, and increments msg->sov by the number of bytes parsed,
1736 * so that we know we can forward between ->som and ->sov. Note that due to
1737 * possible wrapping at the end of the buffer, it is possible that msg->sov is
1738 * lower than msg->som.
Willy Tarreau115acb92009-12-26 13:56:06 +01001739 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001740 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001741 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001742int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001743{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001744 const struct buffer *buf = msg->buf;
1745 const char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
1746 const char *ptr_old = ptr;
1747 const char *end = buf->data + buf->size;
1748 const char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau115acb92009-12-26 13:56:06 +01001749 unsigned int chunk = 0;
1750
1751 /* The chunk size is in the following form, though we are only
1752 * interested in the size and CRLF :
1753 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1754 */
1755 while (1) {
1756 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001757 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001758 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001759 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001760 if (c < 0) /* not a hex digit anymore */
1761 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001762 if (++ptr >= end)
1763 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001764 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001765 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001766 chunk = (chunk << 4) + c;
1767 }
1768
Willy Tarreaud98cf932009-12-27 22:54:55 +01001769 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001770 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001771 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001772
1773 while (http_is_spht[(unsigned char)*ptr]) {
1774 if (++ptr >= end)
1775 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001776 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001777 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001778 }
1779
Willy Tarreaud98cf932009-12-27 22:54:55 +01001780 /* Up to there, we know that at least one byte is present at *ptr. Check
1781 * for the end of chunk size.
1782 */
1783 while (1) {
1784 if (likely(HTTP_IS_CRLF(*ptr))) {
1785 /* we now have a CR or an LF at ptr */
1786 if (likely(*ptr == '\r')) {
1787 if (++ptr >= end)
1788 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001789 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001790 return 0;
1791 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001792
Willy Tarreaud98cf932009-12-27 22:54:55 +01001793 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001794 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001795 if (++ptr >= end)
1796 ptr = buf->data;
1797 /* done */
1798 break;
1799 }
1800 else if (*ptr == ';') {
1801 /* chunk extension, ends at next CRLF */
1802 if (++ptr >= end)
1803 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001804 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001805 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001806
1807 while (!HTTP_IS_CRLF(*ptr)) {
1808 if (++ptr >= end)
1809 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001810 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001811 return 0;
1812 }
1813 /* we have a CRLF now, loop above */
1814 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001815 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001816 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001817 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001818 }
1819
Willy Tarreaud98cf932009-12-27 22:54:55 +01001820 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001821 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001823 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001824 msg->sov += ptr - ptr_old;
1825 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001826 msg->chunk_len = chunk;
1827 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001828 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001829 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001830 error:
1831 msg->err_pos = ptr - buf->data;
1832 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001833}
1834
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001835/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001836 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001837 * the trailers is found, it is automatically scheduled to be forwarded,
1838 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1839 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001840 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001841 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001842 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001843 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1844 * which implies that all non-trailers data have already been scheduled for
1845 * forwarding, and that the difference between msg->som and msg->sov exactly
1846 * matches the length of trailers already parsed and not forwarded. It is also
1847 * important to note that this function is designed to be able to parse wrapped
1848 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001849 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001850int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001851{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001852 const struct buffer *buf = msg->buf;
1853
Willy Tarreaua458b672012-03-05 11:17:50 +01001854 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001856 const char *p1 = NULL, *p2 = NULL;
1857 const char *ptr = buffer_wrap_add(buf, buf->p + msg->next);
1858 const char *stop = buffer_wrap_add(buf, buf->p + buf->i);
Willy Tarreau638cd022010-01-03 07:42:04 +01001859 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860
1861 /* scan current line and stop at LF or CRLF */
1862 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001863 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001864 return 0;
1865
1866 if (*ptr == '\n') {
1867 if (!p1)
1868 p1 = ptr;
1869 p2 = ptr;
1870 break;
1871 }
1872
1873 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001874 if (p1) {
1875 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001876 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001877 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001878 p1 = ptr;
1879 }
1880
1881 ptr++;
1882 if (ptr >= buf->data + buf->size)
1883 ptr = buf->data;
1884 }
1885
1886 /* after LF; point to beginning of next line */
1887 p2++;
1888 if (p2 >= buf->data + buf->size)
1889 p2 = buf->data;
1890
Willy Tarreaua458b672012-03-05 11:17:50 +01001891 bytes = p2 - buffer_wrap_add(buf, buf->p + msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001892 if (bytes < 0)
1893 bytes += buf->size;
1894
1895 /* schedule this line for forwarding */
1896 msg->sov += bytes;
1897 if (msg->sov >= buf->size)
1898 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001899
Willy Tarreaua458b672012-03-05 11:17:50 +01001900 if (p1 == buffer_wrap_add(buf, buf->p + msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001901 /* LF/CRLF at beginning of line => end of trailers at p2.
1902 * Everything was scheduled for forwarding, there's nothing
1903 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001904 */
Willy Tarreaua458b672012-03-05 11:17:50 +01001905 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 msg->msg_state = HTTP_MSG_DONE;
1907 return 1;
1908 }
1909 /* OK, next line then */
Willy Tarreaua458b672012-03-05 11:17:50 +01001910 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001911 }
1912}
1913
1914/* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or
1915 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreaua458b672012-03-05 11:17:50 +01001916 * ->som, ->next in order to include this part into the next forwarding phase.
1917 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1919 * not enough data are available, the function does not change anything and
1920 * returns zero. If a parse error is encountered, the function returns < 0 and
1921 * does not change anything. Note: this function is designed to parse wrapped
1922 * CRLF at the end of the buffer.
1923 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001924int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925{
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001926 const struct buffer *buf = msg->buf;
1927 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928 int bytes;
1929
1930 /* NB: we'll check data availabilty at the end. It's not a
1931 * problem because whatever we match first will be checked
1932 * against the correct length.
1933 */
1934 bytes = 1;
Willy Tarreaua458b672012-03-05 11:17:50 +01001935 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001936 if (*ptr == '\r') {
1937 bytes++;
1938 ptr++;
1939 if (ptr >= buf->data + buf->size)
1940 ptr = buf->data;
1941 }
1942
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001943 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001944 return 0;
1945
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001946 if (*ptr != '\n') {
1947 msg->err_pos = ptr - buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001949 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950
1951 ptr++;
1952 if (ptr >= buf->data + buf->size)
1953 ptr = buf->data;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001954 /* prepare the CRLF to be forwarded (between ->som and ->sov) */
1955 msg->som = 0;
1956 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1958 return 1;
1959}
Willy Tarreau5b154472009-12-21 20:11:07 +01001960
Willy Tarreau21710ff2012-03-09 13:58:04 +01001961/* This function realigns a possibly wrapping http message at the beginning
1962 * of its buffer. The function may only be used when the buffer's tail is
1963 * empty.
1964 */
1965void http_message_realign(struct http_msg *msg)
Willy Tarreau83e3af02009-12-28 17:39:57 +01001966{
Willy Tarreau21710ff2012-03-09 13:58:04 +01001967 struct buffer *buf = msg->buf;
Willy Tarreau89fa7062012-03-02 16:13:16 +01001968 int off = buf->data + buf->size - buf->p;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001969
1970 /* two possible cases :
1971 * - the buffer is in one contiguous block, we move it in-place
Willy Tarreau8096de92010-02-26 11:12:27 +01001972 * - the buffer is in two blocks, we move it via the swap_buffer
Willy Tarreau83e3af02009-12-28 17:39:57 +01001973 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001974 if (buf->i) {
1975 int block1 = buf->i;
Willy Tarreau8096de92010-02-26 11:12:27 +01001976 int block2 = 0;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001977 if (buf->p + buf->i > buf->data + buf->size) {
Willy Tarreau83e3af02009-12-28 17:39:57 +01001978 /* non-contiguous block */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001979 block1 = buf->data + buf->size - buf->p;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001980 block2 = buf->p + buf->i - (buf->data + buf->size);
Willy Tarreau8096de92010-02-26 11:12:27 +01001981 }
1982 if (block2)
1983 memcpy(swap_buffer, buf->data, block2);
Willy Tarreau89fa7062012-03-02 16:13:16 +01001984 memmove(buf->data, buf->p, block1);
Willy Tarreau8096de92010-02-26 11:12:27 +01001985 if (block2)
1986 memcpy(buf->data + block1, swap_buffer, block2);
Willy Tarreau83e3af02009-12-28 17:39:57 +01001987 }
1988
1989 /* adjust all known pointers */
Willy Tarreau89fa7062012-03-02 16:13:16 +01001990 buf->p = buf->data;
Willy Tarreau83e3af02009-12-28 17:39:57 +01001991
Willy Tarreau83e3af02009-12-28 17:39:57 +01001992 if (msg->err_pos >= 0) {
1993 msg->err_pos += off;
1994 if (msg->err_pos >= buf->size)
1995 msg->err_pos -= buf->size;
1996 }
1997
1998 buf->flags &= ~BF_FULL;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001999 if (buffer_len(buf) >= buffer_max_len(buf))
Willy Tarreau83e3af02009-12-28 17:39:57 +01002000 buf->flags |= BF_FULL;
2001}
2002
Willy Tarreaud787e662009-07-07 10:14:51 +02002003/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2004 * processing can continue on next analysers, or zero if it either needs more
2005 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2006 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2007 * when it has nothing left to do, and may remove any analyser when it wants to
2008 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002009 */
Willy Tarreau3a816292009-07-07 10:55:49 +02002010int http_wait_for_request(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002011{
Willy Tarreau59234e92008-11-30 23:51:27 +01002012 /*
2013 * We will parse the partial (or complete) lines.
2014 * We will check the request syntax, and also join multi-line
2015 * headers. An index of all the lines will be elaborated while
2016 * parsing.
2017 *
2018 * For the parsing, we use a 28 states FSM.
2019 *
2020 * Here is the information we currently have :
Willy Tarreauea1175a2012-03-05 15:52:30 +01002021 * req->p + msg->som = beginning of request
2022 * req->p + msg->eoh = end of processed headers / start of current one
Willy Tarreau83e3af02009-12-28 17:39:57 +01002023 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01002024 * msg->next = first non-visited byte
Willy Tarreau59234e92008-11-30 23:51:27 +01002025 * req->r = end of data
Willy Tarreaud787e662009-07-07 10:14:51 +02002026 *
2027 * At end of parsing, we may perform a capture of the error (if any), and
2028 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2029 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2030 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002031 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002032
Willy Tarreau59234e92008-11-30 23:51:27 +01002033 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002034 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002035 struct http_txn *txn = &s->txn;
2036 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002037 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002038
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002039 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 +01002040 now_ms, __FUNCTION__,
2041 s,
2042 req,
2043 req->rex, req->wex,
2044 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002045 req->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002046 req->analysers);
2047
Willy Tarreau52a0c602009-08-16 22:45:38 +02002048 /* we're speaking HTTP here, so let's speak HTTP to the client */
2049 s->srv_error = http_return_srv_error;
2050
Willy Tarreau83e3af02009-12-28 17:39:57 +01002051 /* There's a protected area at the end of the buffer for rewriting
2052 * purposes. We don't want to start to parse the request if the
2053 * protected area is affected, because we may have to move processed
2054 * data later, which is much more complicated.
2055 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002056 if (buffer_not_empty(req) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002057 if ((txn->flags & TX_NOT_FIRST) &&
2058 unlikely((req->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002059 buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002060 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01002061 if (req->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002062 if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2063 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002064 /* some data has still not left the buffer, wake us once that's done */
2065 buffer_dont_connect(req);
2066 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
2067 return 0;
2068 }
Willy Tarreaua458b672012-03-05 11:17:50 +01002069 if (buffer_wrap_add(req, req->p + req->i) < buffer_wrap_add(req, req->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002070 buffer_wrap_add(req, req->p + req->i) > req->data + req->size - global.tune.maxrewrite)
Willy Tarreau21710ff2012-03-09 13:58:04 +01002071 http_message_realign(msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002072 }
2073
Willy Tarreau065e8332010-01-08 00:30:20 +01002074 /* Note that we have the same problem with the response ; we
2075 * may want to send a redirect, error or anything which requires
2076 * some spare space. So we'll ensure that we have at least
2077 * maxrewrite bytes available in the response buffer before
2078 * processing that one. This will only affect pipelined
2079 * keep-alive requests.
2080 */
2081 if ((txn->flags & TX_NOT_FIRST) &&
2082 unlikely((s->rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01002083 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 +01002084 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 +01002085 if (s->rep->o) {
Willy Tarreau64648412010-03-05 10:41:54 +01002086 if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
2087 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002088 /* don't let a connection request be initiated */
2089 buffer_dont_connect(req);
Willy Tarreauff7b5882010-01-22 14:41:29 +01002090 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002091 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002092 return 0;
2093 }
2094 }
2095
Willy Tarreaua458b672012-03-05 11:17:50 +01002096 if (likely(msg->next < req->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002097 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002098 }
2099
Willy Tarreau59234e92008-11-30 23:51:27 +01002100 /* 1: we might have to print this header in debug mode */
2101 if (unlikely((global.mode & MODE_DEBUG) &&
2102 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02002103 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002104 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002105 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002106
Willy Tarreauea1175a2012-03-05 15:52:30 +01002107 sol = req->p + msg->som;
Willy Tarreau59234e92008-11-30 23:51:27 +01002108 eol = sol + msg->sl.rq.l;
2109 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002110
Willy Tarreau59234e92008-11-30 23:51:27 +01002111 sol += hdr_idx_first_pos(&txn->hdr_idx);
2112 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002113
Willy Tarreau59234e92008-11-30 23:51:27 +01002114 while (cur_idx) {
2115 eol = sol + txn->hdr_idx.v[cur_idx].len;
2116 debug_hdr("clihdr", s, sol, eol);
2117 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2118 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002119 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002120 }
2121
Willy Tarreau58f10d72006-12-04 02:26:12 +01002122
Willy Tarreau59234e92008-11-30 23:51:27 +01002123 /*
2124 * Now we quickly check if we have found a full valid request.
2125 * If not so, we check the FD and buffer states before leaving.
2126 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002127 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002128 * requests are checked first. When waiting for a second request
2129 * on a keep-alive session, if we encounter and error, close, t/o,
2130 * we note the error in the session flags but don't set any state.
2131 * Since the error will be noted there, it will not be counted by
2132 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002133 * Last, we may increase some tracked counters' http request errors on
2134 * the cases that are deliberately the client's fault. For instance,
2135 * a timeout or connection reset is not counted as an error. However
2136 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002137 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002138
Willy Tarreau655dce92009-11-08 13:10:58 +01002139 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002140 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002141 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002142 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002143 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002144 session_inc_http_req_ctr(s);
2145 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002146 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002147 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002148 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002149
Willy Tarreau59234e92008-11-30 23:51:27 +01002150 /* 1: Since we are in header mode, if there's no space
2151 * left for headers, we won't be able to free more
2152 * later, so the session will never terminate. We
2153 * must terminate it now.
2154 */
2155 if (unlikely(req->flags & BF_FULL)) {
2156 /* FIXME: check if URI is set and return Status
2157 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002158 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002159 session_inc_http_req_ctr(s);
2160 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002161 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002162 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002163 msg->err_pos = req->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002164 goto return_bad_req;
2165 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002166
Willy Tarreau59234e92008-11-30 23:51:27 +01002167 /* 2: have we encountered a read error ? */
2168 else if (req->flags & BF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002169 if (!(s->flags & SN_ERR_MASK))
2170 s->flags |= SN_ERR_CLICL;
2171
Willy Tarreaufcffa692010-01-10 14:21:19 +01002172 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002173 goto failed_keep_alive;
2174
Willy Tarreau59234e92008-11-30 23:51:27 +01002175 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002176 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002177 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002178 session_inc_http_err_ctr(s);
2179 }
2180
Willy Tarreau59234e92008-11-30 23:51:27 +01002181 msg->msg_state = HTTP_MSG_ERROR;
2182 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002183
Willy Tarreauda7ff642010-06-23 11:44:09 +02002184 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002185 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002186 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002187 if (s->listener->counters)
2188 s->listener->counters->failed_req++;
2189
Willy Tarreau59234e92008-11-30 23:51:27 +01002190 if (!(s->flags & SN_FINST_MASK))
2191 s->flags |= SN_FINST_R;
2192 return 0;
2193 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002194
Willy Tarreau59234e92008-11-30 23:51:27 +01002195 /* 3: has the read timeout expired ? */
2196 else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002197 if (!(s->flags & SN_ERR_MASK))
2198 s->flags |= SN_ERR_CLITO;
2199
Willy Tarreaufcffa692010-01-10 14:21:19 +01002200 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002201 goto failed_keep_alive;
2202
Willy Tarreau59234e92008-11-30 23:51:27 +01002203 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002204 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002205 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002206 session_inc_http_err_ctr(s);
2207 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002208 txn->status = 408;
2209 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
2210 msg->msg_state = HTTP_MSG_ERROR;
2211 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002212
Willy Tarreauda7ff642010-06-23 11:44:09 +02002213 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002214 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002215 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002216 if (s->listener->counters)
2217 s->listener->counters->failed_req++;
2218
Willy Tarreau59234e92008-11-30 23:51:27 +01002219 if (!(s->flags & SN_FINST_MASK))
2220 s->flags |= SN_FINST_R;
2221 return 0;
2222 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002223
Willy Tarreau59234e92008-11-30 23:51:27 +01002224 /* 4: have we encountered a close ? */
2225 else if (req->flags & BF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002226 if (!(s->flags & SN_ERR_MASK))
2227 s->flags |= SN_ERR_CLICL;
2228
Willy Tarreaufcffa692010-01-10 14:21:19 +01002229 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002230 goto failed_keep_alive;
2231
Willy Tarreau4076a152009-04-02 15:18:36 +02002232 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002233 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002234 txn->status = 400;
2235 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
2236 msg->msg_state = HTTP_MSG_ERROR;
2237 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002238
Willy Tarreauda7ff642010-06-23 11:44:09 +02002239 session_inc_http_err_ctr(s);
2240 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002241 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002242 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002243 if (s->listener->counters)
2244 s->listener->counters->failed_req++;
2245
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 if (!(s->flags & SN_FINST_MASK))
2247 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002248 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002249 }
2250
Willy Tarreau520d95e2009-09-19 21:04:57 +02002251 buffer_dont_connect(req);
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002252 req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreauff7b5882010-01-22 14:41:29 +01002253 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002254#ifdef TCP_QUICKACK
2255 if (s->listener->options & LI_O_NOQUICKACK) {
2256 /* We need more data, we have to re-enable quick-ack in case we
2257 * previously disabled it, otherwise we might cause the client
2258 * to delay next data.
2259 */
2260 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
2261 }
2262#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002263
Willy Tarreaufcffa692010-01-10 14:21:19 +01002264 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2265 /* If the client starts to talk, let's fall back to
2266 * request timeout processing.
2267 */
2268 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002269 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002270 }
2271
Willy Tarreau59234e92008-11-30 23:51:27 +01002272 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002273 if (!tick_isset(req->analyse_exp)) {
2274 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2275 (txn->flags & TX_WAIT_NEXT_RQ) &&
2276 tick_isset(s->be->timeout.httpka))
2277 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2278 else
2279 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2280 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002281
Willy Tarreau59234e92008-11-30 23:51:27 +01002282 /* we're not ready yet */
2283 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002284
2285 failed_keep_alive:
2286 /* Here we process low-level errors for keep-alive requests. In
2287 * short, if the request is not the first one and it experiences
2288 * a timeout, read error or shutdown, we just silently close so
2289 * that the client can try again.
2290 */
2291 txn->status = 0;
2292 msg->msg_state = HTTP_MSG_RQBEFORE;
2293 req->analysers = 0;
2294 s->logs.logwait = 0;
Willy Tarreauff7b5882010-01-22 14:41:29 +01002295 s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002296 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002297 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002298 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002299
Willy Tarreaud787e662009-07-07 10:14:51 +02002300 /* OK now we have a complete HTTP request with indexed headers. Let's
2301 * complete the request parsing by setting a few fields we will need
Willy Tarreaufa355d42009-11-29 18:12:29 +01002302 * later. At this point, we have the last CRLF at req->data + msg->eoh.
2303 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002304 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002305 * byte after the last LF. msg->sov points to the first byte of data.
2306 * msg->eol cannot be trusted because it may have been left uninitialized
2307 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002308 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002309
Willy Tarreauda7ff642010-06-23 11:44:09 +02002310 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002311 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2312
Willy Tarreaub16a5742010-01-10 14:46:16 +01002313 if (txn->flags & TX_WAIT_NEXT_RQ) {
2314 /* kill the pending keep-alive timeout */
2315 txn->flags &= ~TX_WAIT_NEXT_RQ;
2316 req->analyse_exp = TICK_ETERNITY;
2317 }
2318
2319
Willy Tarreaud787e662009-07-07 10:14:51 +02002320 /* Maybe we found in invalid header name while we were configured not
2321 * to block on that, so we have to capture it now.
2322 */
2323 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002324 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002325
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 /*
2327 * 1: identify the method
2328 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002329 txn->meth = find_http_meth(req->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002330
2331 /* we can make use of server redirect on GET and HEAD */
2332 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2333 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002334
Willy Tarreau59234e92008-11-30 23:51:27 +01002335 /*
2336 * 2: check if the URI matches the monitor_uri.
2337 * We have to do this for every request which gets in, because
2338 * the monitor-uri is defined by the frontend.
2339 */
2340 if (unlikely((s->fe->monitor_uri_len != 0) &&
2341 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002342 !memcmp(req->p + msg->sol + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 s->fe->monitor_uri,
2344 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002345 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002346 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002347 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002349
Willy Tarreau59234e92008-11-30 23:51:27 +01002350 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002351 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002354 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
2355 int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ);
Willy Tarreau11382812008-07-09 16:18:21 +02002356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 ret = acl_pass(ret);
2358 if (cond->pol == ACL_COND_UNLESS)
2359 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002360
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 if (ret) {
2362 /* we fail this request, let's return 503 service unavail */
2363 txn->status = 503;
2364 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503));
2365 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002366 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002367 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002368
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 /* nothing to fail, let's reply normaly */
2370 txn->status = 200;
Willy Tarreauae94d4d2011-05-11 16:28:49 +02002371 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002372 goto return_prx_cond;
2373 }
2374
2375 /*
2376 * 3: Maybe we have to copy the original REQURI for the logs ?
2377 * Note: we cannot log anymore if the request has been
2378 * classified as invalid.
2379 */
2380 if (unlikely(s->logs.logwait & LW_REQ)) {
2381 /* we have a complete HTTP request that we must log */
2382 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2383 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002384
Willy Tarreau59234e92008-11-30 23:51:27 +01002385 if (urilen >= REQURI_LEN)
2386 urilen = REQURI_LEN - 1;
Willy Tarreauea1175a2012-03-05 15:52:30 +01002387 memcpy(txn->uri, &req->p[msg->som], urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002388 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 if (!(s->logs.logwait &= ~LW_REQ))
2391 s->do_log(s);
2392 } else {
2393 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002394 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002395 }
Willy Tarreau06619262006-12-17 08:37:22 +01002396
William Lallemanda73203e2012-03-12 12:48:57 +01002397 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2398 s->unique_id = pool_alloc2(pool2_uniqueid);
2399 }
2400
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002402 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002403 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002404
Willy Tarreau5b154472009-12-21 20:11:07 +01002405 /* ... and check if the request is HTTP/1.1 or above */
2406 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002407 ((req->p[msg->sol + msg->sl.rq.v + 5] > '1') ||
2408 ((req->p[msg->sol + msg->sl.rq.v + 5] == '1') &&
2409 (req->p[msg->sol + msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002410 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002411
2412 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002413 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002414
Willy Tarreau88d349d2010-01-25 12:15:43 +01002415 /* if the frontend has "option http-use-proxy-header", we'll check if
2416 * we have what looks like a proxied connection instead of a connection,
2417 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2418 * Note that this is *not* RFC-compliant, however browsers and proxies
2419 * happen to do that despite being non-standard :-(
2420 * We consider that a request not beginning with either '/' or '*' is
2421 * a proxied connection, which covers both "scheme://location" and
2422 * CONNECT ip:port.
2423 */
2424 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002425 req->p[msg->sol + msg->sl.rq.u] != '/' && req->p[msg->sol + msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002426 txn->flags |= TX_USE_PX_CONN;
2427
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002428 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002429 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002430
Willy Tarreau59234e92008-11-30 23:51:27 +01002431 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002432 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01002433 capture_headers(req->p + msg->sol, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002434 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002435
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002436 /* 6: determine the transfer-length.
2437 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2438 * the presence of a message-body in a REQUEST and its transfer length
2439 * must be determined that way (in order of precedence) :
2440 * 1. The presence of a message-body in a request is signaled by the
2441 * inclusion of a Content-Length or Transfer-Encoding header field
2442 * in the request's header fields. When a request message contains
2443 * both a message-body of non-zero length and a method that does
2444 * not define any semantics for that request message-body, then an
2445 * origin server SHOULD either ignore the message-body or respond
2446 * with an appropriate error message (e.g., 413). A proxy or
2447 * gateway, when presented the same request, SHOULD either forward
2448 * the request inbound with the message- body or ignore the
2449 * message-body when determining a response.
2450 *
2451 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2452 * and the "chunked" transfer-coding (Section 6.2) is used, the
2453 * transfer-length is defined by the use of this transfer-coding.
2454 * If a Transfer-Encoding header field is present and the "chunked"
2455 * transfer-coding is not present, the transfer-length is defined
2456 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002457 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002458 * 3. If a Content-Length header field is present, its decimal value in
2459 * OCTETs represents both the entity-length and the transfer-length.
2460 * If a message is received with both a Transfer-Encoding header
2461 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002462 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002463 * 4. By the server closing the connection. (Closing the connection
2464 * cannot be used to indicate the end of a request body, since that
2465 * would leave no possibility for the server to send back a response.)
2466 *
2467 * Whenever a transfer-coding is applied to a message-body, the set of
2468 * transfer-codings MUST include "chunked", unless the message indicates
2469 * it is terminated by closing the connection. When the "chunked"
2470 * transfer-coding is used, it MUST be the last transfer-coding applied
2471 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002472 */
2473
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002474 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002475 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002476 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002477 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002478 http_find_header2("Transfer-Encoding", 17, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002479 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002480 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2481 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002482 /* bad transfer-encoding (chunked followed by something else) */
2483 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002484 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002485 break;
2486 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002487 }
2488
Willy Tarreau32b47f42009-10-18 20:55:02 +02002489 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002490 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01002491 http_find_header2("Content-Length", 14, req->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002492 signed long long cl;
2493
Willy Tarreauad14f752011-09-02 20:33:27 +02002494 if (!ctx.vlen) {
2495 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002496 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002497 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002498
Willy Tarreauad14f752011-09-02 20:33:27 +02002499 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
2500 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002501 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002502 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002503
Willy Tarreauad14f752011-09-02 20:33:27 +02002504 if (cl < 0) {
2505 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002506 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002507 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002508
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002509 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02002510 msg->err_pos = ctx.line + ctx.val - req->data;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002511 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002512 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002513
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002514 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002515 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002516 }
2517
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002518 /* bodyless requests have a known length */
2519 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002520 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002521
Willy Tarreaud787e662009-07-07 10:14:51 +02002522 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002523 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002524 req->analyse_exp = TICK_ETERNITY;
2525 return 1;
2526
2527 return_bad_req:
2528 /* We centralize bad requests processing here */
2529 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2530 /* we detected a parsing error. We want to archive this request
2531 * in the dedicated proxy area for later troubleshooting.
2532 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002533 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002534 }
2535
2536 txn->req.msg_state = HTTP_MSG_ERROR;
2537 txn->status = 400;
2538 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002539
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002540 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002541 if (s->listener->counters)
2542 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002543
2544 return_prx_cond:
2545 if (!(s->flags & SN_ERR_MASK))
2546 s->flags |= SN_ERR_PRXCOND;
2547 if (!(s->flags & SN_FINST_MASK))
2548 s->flags |= SN_FINST_R;
2549
2550 req->analysers = 0;
2551 req->analyse_exp = TICK_ETERNITY;
2552 return 0;
2553}
2554
Cyril Bonté70be45d2010-10-12 00:14:35 +02002555/* We reached the stats page through a POST request.
2556 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002557 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002558 */
Willy Tarreau295a8372011-03-10 11:25:07 +01002559int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002560{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002561 struct proxy *px = NULL;
2562 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002563
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002564 char key[LINESIZE];
2565 int action = ST_ADM_ACTION_NONE;
2566 int reprocess = 0;
2567
2568 int total_servers = 0;
2569 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002570
2571 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002572 char *st_cur_param = NULL;
2573 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002574
Willy Tarreauea1175a2012-03-05 15:52:30 +01002575 first_param = req->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002576 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002577
2578 cur_param = next_param = end_params;
2579
Cyril Bonté23b39d92011-02-10 22:54:44 +01002580 if (end_params >= req->data + req->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002581 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002582 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002583 return 1;
2584 }
Willy Tarreau363a5bb2012-03-02 20:14:45 +01002585 else if (end_params > req->p + req->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002586 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002587 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002588 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002589 }
2590
2591 *end_params = '\0';
2592
Willy Tarreau295a8372011-03-10 11:25:07 +01002593 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002594
2595 /*
2596 * Parse the parameters in reverse order to only store the last value.
2597 * From the html form, the backend and the action are at the end.
2598 */
2599 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002600 char *value;
2601 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002602
2603 cur_param--;
2604 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002605 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002606 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002607 poffset = (cur_param != first_param ? 1 : 0);
2608 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2609 if ((plen > 0) && (plen <= sizeof(key))) {
2610 strncpy(key, cur_param + poffset, plen);
2611 key[plen - 1] = '\0';
2612 } else {
2613 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2614 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002615 }
2616
2617 /* Parse the value */
2618 value = key;
2619 while (*value != '\0' && *value != '=') {
2620 value++;
2621 }
2622 if (*value == '=') {
2623 /* Ok, a value is found, we can mark the end of the key */
2624 *value++ = '\0';
2625 }
2626
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002627 if (!url_decode(key) || !url_decode(value))
2628 break;
2629
Cyril Bonté70be45d2010-10-12 00:14:35 +02002630 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002631 if (!px && (strcmp(key, "b") == 0)) {
2632 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2633 /* the backend name is unknown or ambiguous (duplicate names) */
2634 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2635 goto out;
2636 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002637 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002638 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002639 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002640 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002641 }
2642 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002643 action = ST_ADM_ACTION_ENABLE;
2644 }
2645 else {
2646 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2647 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002648 }
2649 }
2650 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002651 if (!(px && action)) {
2652 /*
2653 * Indicates that we'll need to reprocess the parameters
2654 * as soon as backend and action are known
2655 */
2656 if (!reprocess) {
2657 st_cur_param = cur_param;
2658 st_next_param = next_param;
2659 }
2660 reprocess = 1;
2661 }
2662 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002663 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002664 case ST_ADM_ACTION_DISABLE:
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 /* Not already in maintenance, we can change the server state */
2667 sv->state |= SRV_MAINTAIN;
2668 set_server_down(sv);
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;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002673 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002674 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002675 /* Already in maintenance, we can change the server state */
2676 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002677 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002678 altered_servers++;
2679 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002680 }
2681 break;
2682 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002683 } else {
2684 /* the server name is unknown or ambiguous (duplicate names) */
2685 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002686 }
2687 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002688 if (reprocess && px && action) {
2689 /* Now, we know the backend and the action chosen by the user.
2690 * We can safely restart from the first server parameter
2691 * to reprocess them
2692 */
2693 cur_param = st_cur_param;
2694 next_param = st_next_param;
2695 reprocess = 0;
2696 goto reprocess_servers;
2697 }
2698
Cyril Bonté70be45d2010-10-12 00:14:35 +02002699 next_param = cur_param;
2700 }
2701 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002702
2703 if (total_servers == 0) {
2704 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2705 }
2706 else if (altered_servers == 0) {
2707 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2708 }
2709 else if (altered_servers == total_servers) {
2710 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2711 }
2712 else {
2713 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2714 }
2715 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002716 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002717}
2718
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002719/* returns a pointer to the first rule which forbids access (deny or http_auth),
2720 * or NULL if everything's OK.
2721 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002722static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002723http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2724{
Willy Tarreauff011f22011-01-06 17:51:27 +01002725 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002726
Willy Tarreauff011f22011-01-06 17:51:27 +01002727 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002728 int ret = 1;
2729
Willy Tarreauff011f22011-01-06 17:51:27 +01002730 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002731 continue;
2732
2733 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002734 if (rule->cond) {
2735 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002736 ret = acl_pass(ret);
2737
Willy Tarreauff011f22011-01-06 17:51:27 +01002738 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002739 ret = !ret;
2740 }
2741
2742 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002743 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002744 return NULL; /* no problem */
2745 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002746 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002747 }
2748 }
2749 return NULL;
2750}
2751
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002752/* This stream analyser runs all HTTP request processing which is common to
2753 * frontends and backends, which means blocking ACLs, filters, connection-close,
2754 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002755 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002756 * either needs more data or wants to immediately abort the request (eg: deny,
2757 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002758 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002759int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002760{
Willy Tarreaud787e662009-07-07 10:14:51 +02002761 struct http_txn *txn = &s->txn;
2762 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002763 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002764 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002765 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002766 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002767 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002768
Willy Tarreau655dce92009-11-08 13:10:58 +01002769 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002770 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002771 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002772 return 0;
2773 }
2774
Willy Tarreau3a816292009-07-07 10:55:49 +02002775 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002776 req->analyse_exp = TICK_ETERNITY;
2777
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002778 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 +02002779 now_ms, __FUNCTION__,
2780 s,
2781 req,
2782 req->rex, req->wex,
2783 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002784 req->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002785 req->analysers);
2786
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002787 /* first check whether we have some ACLs set to block this request */
2788 list_for_each_entry(cond, &px->block_cond, list) {
2789 int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002790
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002791 ret = acl_pass(ret);
2792 if (cond->pol == ACL_COND_UNLESS)
2793 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002794
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002795 if (ret) {
2796 txn->status = 403;
2797 /* let's log the request time */
2798 s->logs.tv_request = now;
2799 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002800 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002801 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002802 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002803 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002804
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002805 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002806 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002807
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002808 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002809 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002810 do_stats = stats_check_uri(s->rep->prod, txn, px);
2811 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002812 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 +01002813 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002814 else
2815 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002816
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002817 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002818 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002819 txn->status = 403;
2820 s->logs.tv_request = now;
2821 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002822 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002823 s->fe->fe_counters.denied_req++;
2824 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2825 s->be->be_counters.denied_req++;
2826 if (s->listener->counters)
2827 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002828 goto return_prx_cond;
2829 }
2830
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002831 /* try headers filters */
2832 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01002833 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002834 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01002835
Willy Tarreau59234e92008-11-30 23:51:27 +01002836 /* has the request been denied ? */
2837 if (txn->flags & TX_CLDENY) {
2838 /* no need to go further */
2839 txn->status = 403;
2840 /* let's log the request time */
2841 s->logs.tv_request = now;
2842 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002843 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01002844 goto return_prx_cond;
2845 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02002846
2847 /* When a connection is tarpitted, we use the tarpit timeout,
2848 * which may be the same as the connect timeout if unspecified.
2849 * If unset, then set it to zero because we really want it to
2850 * eventually expire. We build the tarpit as an analyser.
2851 */
2852 if (txn->flags & TX_CLTARPIT) {
2853 buffer_erase(s->req);
2854 /* wipe the request out so that we can drop the connection early
2855 * if the client closes first.
2856 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02002857 buffer_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002858 req->analysers = 0; /* remove switching rules etc... */
2859 req->analysers |= AN_REQ_HTTP_TARPIT;
2860 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
2861 if (!req->analyse_exp)
2862 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002863 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02002864 return 1;
2865 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002866 }
Willy Tarreau06619262006-12-17 08:37:22 +01002867
Willy Tarreau5b154472009-12-21 20:11:07 +01002868 /* Until set to anything else, the connection mode is set as TUNNEL. It will
2869 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002870 * Option httpclose by itself does not set a mode, it remains a tunnel mode
2871 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002872 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
2873 * avoid to redo the same work if FE and BE have the same settings (common).
2874 * The method consists in checking if options changed between the two calls
2875 * (implying that either one is non-null, or one of them is non-null and we
2876 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02002877 */
Willy Tarreau5b154472009-12-21 20:11:07 +01002878
Willy Tarreaudc008c52010-02-01 16:20:08 +01002879 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
2880 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
2881 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
2882 (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 +01002883 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002884
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01002885 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
2886 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01002887 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002888 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
2889 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002890 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01002891 tmp = TX_CON_WANT_CLO;
2892
Willy Tarreau5b154472009-12-21 20:11:07 +01002893 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
2894 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002895
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002896 if (!(txn->flags & TX_HDR_CONN_PRS)) {
2897 /* parse the Connection header and possibly clean it */
2898 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002899 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02002900 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
2901 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002902 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002903 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002904 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002905 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01002906 }
Willy Tarreau5b154472009-12-21 20:11:07 +01002907
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002908 /* check if client or config asks for explicit close in KAL/SCL */
2909 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
2910 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
2911 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002912 (!(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 +01002913 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002914 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01002915 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002916 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
2917 }
Willy Tarreau78599912009-10-17 20:12:21 +02002918
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002919 /* we can be blocked here because the request needs to be authenticated,
2920 * either to pass or to access stats.
2921 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002922 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002923 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01002924 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002925
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002926 if (!realm)
2927 realm = do_stats?STATS_DEFAULT_REALM:px->id;
2928
Willy Tarreau844a7e72010-01-31 21:46:18 +01002929 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002930 chunk_initlen(&msg, trash, sizeof(trash), strlen(trash));
2931 txn->status = 401;
2932 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002933 /* on 401 we still count one error, because normal browsing
2934 * won't significantly increase the counter but brute force
2935 * attempts will.
2936 */
2937 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002938 goto return_prx_cond;
2939 }
2940
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002941 /* add request headers from the rule sets in the same order */
2942 list_for_each_entry(wl, &px->req_add, list) {
2943 if (wl->cond) {
2944 int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ);
2945 ret = acl_pass(ret);
2946 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
2947 ret = !ret;
2948 if (!ret)
2949 continue;
2950 }
2951
Willy Tarreau6acf7c92012-03-09 13:30:45 +01002952 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002953 goto return_bad_req;
2954 }
2955
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002956 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02002957 struct stats_admin_rule *stats_admin_rule;
2958
2959 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002960 * FIXME!!! that one is rather dangerous, we want to
2961 * make it follow standard rules (eg: clear req->analysers).
2962 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01002963
Cyril Bonté474be412010-10-12 00:14:36 +02002964 /* now check whether we have some admin rules for this request */
2965 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2966 int ret = 1;
2967
2968 if (stats_admin_rule->cond) {
2969 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ);
2970 ret = acl_pass(ret);
2971 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2972 ret = !ret;
2973 }
2974
2975 if (ret) {
2976 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01002977 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02002978 break;
2979 }
2980 }
2981
Cyril Bonté70be45d2010-10-12 00:14:35 +02002982 /* Was the status page requested with a POST ? */
2983 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01002984 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002985 if (msg->msg_state < HTTP_MSG_100_SENT) {
2986 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2987 * send an HTTP/1.1 100 Continue intermediate response.
2988 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002989 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002990 struct hdr_ctx ctx;
2991 ctx.idx = 0;
2992 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01002993 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01002994 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2995 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
2996 }
2997 }
2998 msg->msg_state = HTTP_MSG_100_SENT;
2999 s->logs.tv_request = now; /* update the request timer to reflect full request */
3000 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003001 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003002 /* we need more data */
3003 req->analysers |= an_bit;
3004 buffer_dont_connect(req);
3005 return 0;
3006 }
Cyril Bonté474be412010-10-12 00:14:36 +02003007 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003008 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003009 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003010 }
3011
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003012 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003013 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003014 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau7b7a8e92011-03-27 19:53:06 +02003015 copy_target(&s->target, &s->rep->prod->target); // for logging only
Willy Tarreaubc4af052011-02-13 13:25:14 +01003016 s->rep->prod->applet.private = s;
3017 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003018 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003019 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3020 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003021
3022 return 0;
3023
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003024 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003025
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003026 /* check whether we have some ACLs set to redirect this request */
3027 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003028 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003029
Willy Tarreauf285f542010-01-03 20:03:03 +01003030 if (rule->cond) {
3031 ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ);
3032 ret = acl_pass(ret);
3033 if (rule->cond->pol == ACL_COND_UNLESS)
3034 ret = !ret;
3035 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003036
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 if (ret) {
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003038 struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003039 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003040
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003041 /* build redirect message */
3042 switch(rule->code) {
3043 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003044 msg_fmt = HTTP_303;
3045 break;
3046 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003047 msg_fmt = HTTP_301;
3048 break;
3049 case 302:
3050 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003051 msg_fmt = HTTP_302;
3052 break;
3053 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003054
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003055 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003056 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003057
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003058 switch(rule->type) {
3059 case REDIRECT_TYPE_PREFIX: {
3060 const char *path;
3061 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003062
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 path = http_get_path(txn);
3064 /* build message using path */
3065 if (path) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003066 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 +02003067 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3068 int qs = 0;
3069 while (qs < pathlen) {
3070 if (path[qs] == '?') {
3071 pathlen = qs;
3072 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003073 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003074 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003075 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003076 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003077 } else {
3078 path = "/";
3079 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003080 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003081
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003082 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003083 goto return_bad_req;
3084
3085 /* add prefix. Note that if prefix == "/", we don't want to
3086 * add anything, otherwise it makes it hard for the user to
3087 * configure a self-redirection.
3088 */
3089 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003090 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3091 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003092 }
3093
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003094 /* add path */
3095 memcpy(rdr.str + rdr.len, path, pathlen);
3096 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003097
3098 /* append a slash at the end of the location is needed and missing */
3099 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3100 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3101 if (rdr.len > rdr.size - 5)
3102 goto return_bad_req;
3103 rdr.str[rdr.len] = '/';
3104 rdr.len++;
3105 }
3106
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003107 break;
3108 }
3109 case REDIRECT_TYPE_LOCATION:
3110 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003111 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003112 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003113
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003114 /* add location */
3115 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3116 rdr.len += rule->rdr_len;
3117 break;
3118 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003119
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003120 if (rule->cookie_len) {
3121 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3122 rdr.len += 14;
3123 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3124 rdr.len += rule->cookie_len;
3125 memcpy(rdr.str + rdr.len, "\r\n", 2);
3126 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003127 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003128
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003129 /* add end of headers and the keep-alive/close status.
3130 * We may choose to set keep-alive if the Location begins
3131 * with a slash, because the client will come back to the
3132 * same server.
3133 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003134 txn->status = rule->code;
3135 /* let's log the request time */
3136 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003137
3138 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003139 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3140 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003141 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3142 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3143 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003144 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003145 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3146 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3147 rdr.len += 30;
3148 } else {
3149 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3150 rdr.len += 24;
3151 }
Willy Tarreau75661452010-01-10 10:35:01 +01003152 }
3153 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3154 rdr.len += 4;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003155 buffer_write(req->prod->ob, rdr.str, rdr.len);
3156 /* "eat" the request */
3157 buffer_ignore(req, msg->sov - msg->som);
3158 msg->som = msg->sov;
3159 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003160 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3161 txn->req.msg_state = HTTP_MSG_CLOSED;
3162 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003163 break;
3164 } else {
3165 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003166 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3167 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3168 rdr.len += 29;
3169 } else {
3170 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3171 rdr.len += 23;
3172 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003173 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003174 goto return_prx_cond;
3175 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003176 }
3177 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003178
Willy Tarreau2be39392010-01-03 17:24:51 +01003179 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3180 * If this happens, then the data will not come immediately, so we must
3181 * send all what we have without waiting. Note that due to the small gain
3182 * in waiting for the body of the request, it's easier to simply put the
3183 * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
3184 * itself once used.
3185 */
3186 req->flags |= BF_SEND_DONTWAIT;
3187
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003188 /* that's OK for us now, let's move on to next analysers */
3189 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003190
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003191 return_bad_req:
3192 /* We centralize bad requests processing here */
3193 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3194 /* we detected a parsing error. We want to archive this request
3195 * in the dedicated proxy area for later troubleshooting.
3196 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003197 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003198 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003199
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003200 txn->req.msg_state = HTTP_MSG_ERROR;
3201 txn->status = 400;
3202 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003203
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003204 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003205 if (s->listener->counters)
3206 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003207
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003208 return_prx_cond:
3209 if (!(s->flags & SN_ERR_MASK))
3210 s->flags |= SN_ERR_PRXCOND;
3211 if (!(s->flags & SN_FINST_MASK))
3212 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003213
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003214 req->analysers = 0;
3215 req->analyse_exp = TICK_ETERNITY;
3216 return 0;
3217}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003218
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003219/* This function performs all the processing enabled for the current request.
3220 * It returns 1 if the processing can continue on next analysers, or zero if it
3221 * needs more data, encounters an error, or wants to immediately abort the
3222 * request. It relies on buffers flags, and updates s->req->analysers.
3223 */
3224int http_process_request(struct session *s, struct buffer *req, int an_bit)
3225{
3226 struct http_txn *txn = &s->txn;
3227 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003228
Willy Tarreau655dce92009-11-08 13:10:58 +01003229 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003230 /* we need more data */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003231 buffer_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003232 return 0;
3233 }
3234
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003235 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 +02003236 now_ms, __FUNCTION__,
3237 s,
3238 req,
3239 req->rex, req->wex,
3240 req->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003241 req->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003242 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003243
Willy Tarreau59234e92008-11-30 23:51:27 +01003244 /*
3245 * Right now, we know that we have processed the entire headers
3246 * and that unwanted requests have been filtered out. We can do
3247 * whatever we want with the remaining request. Also, now we
3248 * may have separate values for ->fe, ->be.
3249 */
Willy Tarreau06619262006-12-17 08:37:22 +01003250
Willy Tarreau59234e92008-11-30 23:51:27 +01003251 /*
3252 * If HTTP PROXY is set we simply get remote server address
3253 * parsing incoming request.
3254 */
3255 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003256 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 +01003257 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003258
Willy Tarreau59234e92008-11-30 23:51:27 +01003259 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003260 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003261 * Note that doing so might move headers in the request, but
3262 * the fields will stay coherent and the URI will not move.
3263 * This should only be performed in the backend.
3264 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003265 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003266 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3267 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003268
Willy Tarreau59234e92008-11-30 23:51:27 +01003269 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003270 * 8: the appsession cookie was looked up very early in 1.2,
3271 * so let's do the same now.
3272 */
3273
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003274 /* It needs to look into the URI unless persistence must be ignored */
3275 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01003276 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 +01003277 }
3278
William Lallemanda73203e2012-03-12 12:48:57 +01003279 /* add unique-id if "header-unique-id" is specified */
3280
3281 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3282 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3283
3284 if (s->fe->header_unique_id && s->unique_id) {
3285 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3286 if (ret < 0 || ret > global.tune.bufsize)
3287 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003288 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003289 goto return_bad_req;
3290 }
3291
Cyril Bontéb21570a2009-11-29 20:04:48 +01003292 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003293 * 9: add X-Forwarded-For if either the frontend or the backend
3294 * asks for it.
3295 */
3296 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003297 struct hdr_ctx ctx = { .idx = 0 };
3298
3299 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01003300 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 +02003301 /* The header is set to be added only if none is present
3302 * and we found it, so don't do anything.
3303 */
3304 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003305 else if (s->req->prod->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003306 /* Add an X-Forwarded-For header unless the source IP is
3307 * in the 'except' network range.
3308 */
3309 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003310 (((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 +01003311 != s->fe->except_net.s_addr) &&
3312 (!s->be->except_mask.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003313 (((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 +01003314 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003315 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003316 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003317 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003318
3319 /* Note: we rely on the backend to get the header name to be used for
3320 * x-forwarded-for, because the header is really meant for the backends.
3321 * However, if the backend did not specify any option, we have to rely
3322 * on the frontend's header name.
3323 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003324 if (s->be->fwdfor_hdr_len) {
3325 len = s->be->fwdfor_hdr_len;
3326 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003327 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003328 len = s->fe->fwdfor_hdr_len;
3329 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003330 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003331 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003332
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003333 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003334 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003335 }
3336 }
Willy Tarreau6471afb2011-09-23 10:54:59 +02003337 else if (s->req->prod->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003338 /* FIXME: for the sake of completeness, we should also support
3339 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003340 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003341 int len;
3342 char pn[INET6_ADDRSTRLEN];
3343 inet_ntop(AF_INET6,
Willy Tarreau6471afb2011-09-23 10:54:59 +02003344 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003345 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003346
Willy Tarreau59234e92008-11-30 23:51:27 +01003347 /* Note: we rely on the backend to get the header name to be used for
3348 * x-forwarded-for, because the header is really meant for the backends.
3349 * However, if the backend did not specify any option, we have to rely
3350 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003351 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003352 if (s->be->fwdfor_hdr_len) {
3353 len = s->be->fwdfor_hdr_len;
3354 memcpy(trash, s->be->fwdfor_hdr_name, len);
3355 } else {
3356 len = s->fe->fwdfor_hdr_len;
3357 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003358 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003359 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003360
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003361 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003362 goto return_bad_req;
3363 }
3364 }
3365
3366 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003367 * 10: add X-Original-To if either the frontend or the backend
3368 * asks for it.
3369 */
3370 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3371
3372 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau6471afb2011-09-23 10:54:59 +02003373 if (s->req->prod->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003374 /* Add an X-Original-To header unless the destination IP is
3375 * in the 'except' network range.
3376 */
Willy Tarreau9b061e32012-04-07 18:03:52 +02003377 stream_sock_get_to_addr(s->req->prod);
Maik Broemme2850cb42009-04-17 18:53:21 +02003378
Willy Tarreau6471afb2011-09-23 10:54:59 +02003379 if (s->req->prod->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003380 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003381 (((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 +02003382 != s->fe->except_to.s_addr) &&
3383 (!s->be->except_mask_to.s_addr ||
Willy Tarreau6471afb2011-09-23 10:54:59 +02003384 (((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 +02003385 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003386 int len;
3387 unsigned char *pn;
Willy Tarreau6471afb2011-09-23 10:54:59 +02003388 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003389
3390 /* Note: we rely on the backend to get the header name to be used for
3391 * x-original-to, because the header is really meant for the backends.
3392 * However, if the backend did not specify any option, we have to rely
3393 * on the frontend's header name.
3394 */
3395 if (s->be->orgto_hdr_len) {
3396 len = s->be->orgto_hdr_len;
3397 memcpy(trash, s->be->orgto_hdr_name, len);
3398 } else {
3399 len = s->fe->orgto_hdr_len;
3400 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003401 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003402 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3403
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003404 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003405 goto return_bad_req;
3406 }
3407 }
3408 }
3409
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003410 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3411 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003412 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003413 unsigned int want_flags = 0;
3414
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003415 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003416 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3417 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3418 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003419 want_flags |= TX_CON_CLO_SET;
3420 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003421 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3422 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3423 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003424 want_flags |= TX_CON_KAL_SET;
3425 }
3426
3427 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003428 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003429 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003430
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003431
Willy Tarreau522d6c02009-12-06 18:49:18 +01003432 /* If we have no server assigned yet and we're balancing on url_param
3433 * with a POST request, we may be interested in checking the body for
3434 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003435 */
3436 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3437 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003438 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003439 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003440 buffer_dont_connect(req);
3441 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003442 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003443
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003444 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003445 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003446#ifdef TCP_QUICKACK
3447 /* We expect some data from the client. Unless we know for sure
3448 * we already have a full request, we have to re-enable quick-ack
3449 * in case we previously disabled it, otherwise we might cause
3450 * the client to delay further data.
3451 */
3452 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003453 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003454 (msg->body_len > req->i - txn->req.eoh - 2)))
Willy Tarreau5e205522011-12-17 16:34:27 +01003455 setsockopt(s->si[0].fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
3456#endif
3457 }
Willy Tarreau03945942009-12-22 16:50:27 +01003458
Willy Tarreau59234e92008-11-30 23:51:27 +01003459 /*************************************************************
3460 * OK, that's finished for the headers. We have done what we *
3461 * could. Let's switch to the DATA state. *
3462 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003463 req->analyse_exp = TICK_ETERNITY;
3464 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003465
Willy Tarreau59234e92008-11-30 23:51:27 +01003466 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003467 /* OK let's go on with the BODY now */
3468 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003469
Willy Tarreau59234e92008-11-30 23:51:27 +01003470 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003471 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003472 /* we detected a parsing error. We want to archive this request
3473 * in the dedicated proxy area for later troubleshooting.
3474 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003475 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003476 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003477
Willy Tarreau59234e92008-11-30 23:51:27 +01003478 txn->req.msg_state = HTTP_MSG_ERROR;
3479 txn->status = 400;
3480 req->analysers = 0;
3481 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003482
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003483 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003484 if (s->listener->counters)
3485 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003486
Willy Tarreau59234e92008-11-30 23:51:27 +01003487 if (!(s->flags & SN_ERR_MASK))
3488 s->flags |= SN_ERR_PRXCOND;
3489 if (!(s->flags & SN_FINST_MASK))
3490 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003491 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003492}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003493
Willy Tarreau60b85b02008-11-30 23:28:40 +01003494/* This function is an analyser which processes the HTTP tarpit. It always
3495 * returns zero, at the beginning because it prevents any other processing
3496 * from occurring, and at the end because it terminates the request.
3497 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003498int http_process_tarpit(struct session *s, struct buffer *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003499{
3500 struct http_txn *txn = &s->txn;
3501
3502 /* This connection is being tarpitted. The CLIENT side has
3503 * already set the connect expiration date to the right
3504 * timeout. We just have to check that the client is still
3505 * there and that the timeout has not expired.
3506 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003507 buffer_dont_connect(req);
Willy Tarreau60b85b02008-11-30 23:28:40 +01003508 if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 &&
3509 !tick_is_expired(req->analyse_exp, now_ms))
3510 return 0;
3511
3512 /* We will set the queue timer to the time spent, just for
3513 * logging purposes. We fake a 500 server error, so that the
3514 * attacker will not suspect his connection has been tarpitted.
3515 * It will not cause trouble to the logs because we can exclude
3516 * the tarpitted connections by filtering on the 'PT' status flags.
3517 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003518 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3519
3520 txn->status = 500;
3521 if (req->flags != BF_READ_ERROR)
3522 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500));
3523
3524 req->analysers = 0;
3525 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003526
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003527 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003528 if (s->listener->counters)
3529 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003530
Willy Tarreau60b85b02008-11-30 23:28:40 +01003531 if (!(s->flags & SN_ERR_MASK))
3532 s->flags |= SN_ERR_PRXCOND;
3533 if (!(s->flags & SN_FINST_MASK))
3534 s->flags |= SN_FINST_T;
3535 return 0;
3536}
3537
Willy Tarreaud34af782008-11-30 23:36:37 +01003538/* This function is an analyser which processes the HTTP request body. It looks
3539 * for parameters to be used for the load balancing algorithm (url_param). It
3540 * must only be called after the standard HTTP request processing has occurred,
3541 * because it expects the request to be parsed. It returns zero if it needs to
3542 * read more data, or 1 once it has completed its analysis.
3543 */
Willy Tarreau3a816292009-07-07 10:55:49 +02003544int http_process_request_body(struct session *s, struct buffer *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003545{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003546 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003547 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003548 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003549
3550 /* We have to parse the HTTP request body to find any required data.
3551 * "balance url_param check_post" should have been the only way to get
3552 * into this. We were brought here after HTTP header analysis, so all
3553 * related structures are ready.
3554 */
3555
Willy Tarreau522d6c02009-12-06 18:49:18 +01003556 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3557 goto missing_data;
3558
3559 if (msg->msg_state < HTTP_MSG_100_SENT) {
3560 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3561 * send an HTTP/1.1 100 Continue intermediate response.
3562 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003563 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003564 struct hdr_ctx ctx;
3565 ctx.idx = 0;
3566 /* Expect is allowed in 1.1, look for it */
Willy Tarreau3a215be2012-03-09 21:39:51 +01003567 if (http_find_header2("Expect", 6, req->p + msg->sol, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003568 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
3569 buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len);
3570 }
3571 }
3572 msg->msg_state = HTTP_MSG_100_SENT;
3573 }
3574
3575 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003576 /* we have msg->sov which points to the first byte of message body.
3577 * msg->som still points to the beginning of the message. We must
3578 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003579 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003580 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003581
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003582 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003583 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3584 else
3585 msg->msg_state = HTTP_MSG_DATA;
3586 }
3587
3588 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003589 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003590 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003591 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003592 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003593 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003594
Willy Tarreau115acb92009-12-26 13:56:06 +01003595 if (!ret)
3596 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003597 else if (ret < 0) {
3598 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003599 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003600 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003601 }
3602
Willy Tarreaud98cf932009-12-27 22:54:55 +01003603 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003604 * We have the first data byte is in msg->sov. We're waiting for at
3605 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003606 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003607
Willy Tarreau124d9912011-03-01 20:30:48 +01003608 if (msg->body_len < limit)
3609 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003610
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003611 if (req->i - (msg->sov - msg->som) >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003612 goto http_end;
3613
3614 missing_data:
3615 /* we get here if we need to wait for more data */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003616 if (req->flags & BF_FULL) {
3617 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003618 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003619 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003620
Willy Tarreau522d6c02009-12-06 18:49:18 +01003621 if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
3622 txn->status = 408;
3623 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003624
3625 if (!(s->flags & SN_ERR_MASK))
3626 s->flags |= SN_ERR_CLITO;
3627 if (!(s->flags & SN_FINST_MASK))
3628 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003629 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003630 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003631
3632 /* we get here if we need to wait for more data */
3633 if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003634 /* Not enough data. We'll re-use the http-request
3635 * timeout here. Ideally, we should set the timeout
3636 * relative to the accept() date. We just set the
3637 * request timeout once at the beginning of the
3638 * request.
3639 */
Willy Tarreau520d95e2009-09-19 21:04:57 +02003640 buffer_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003641 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003642 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003643 return 0;
3644 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003645
3646 http_end:
3647 /* The situation will not evolve, so let's give up on the analysis. */
3648 s->logs.tv_request = now; /* update the request timer to reflect full request */
3649 req->analysers &= ~an_bit;
3650 req->analyse_exp = TICK_ETERNITY;
3651 return 1;
3652
3653 return_bad_req: /* let's centralize all bad requests */
3654 txn->req.msg_state = HTTP_MSG_ERROR;
3655 txn->status = 400;
3656 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
3657
Willy Tarreau79ebac62010-06-07 13:47:49 +02003658 if (!(s->flags & SN_ERR_MASK))
3659 s->flags |= SN_ERR_PRXCOND;
3660 if (!(s->flags & SN_FINST_MASK))
3661 s->flags |= SN_FINST_R;
3662
Willy Tarreau522d6c02009-12-06 18:49:18 +01003663 return_err_msg:
3664 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003665 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003666 if (s->listener->counters)
3667 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003668 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003669}
3670
Willy Tarreau45c0d982012-03-09 12:11:57 +01003671/* send a server's name with an outgoing request over an established connection */
3672int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003673
3674 struct hdr_ctx ctx;
3675
Mark Lamourinec2247f02012-01-04 13:02:01 -05003676 char *hdr_name = be->server_id_hdr_name;
3677 int hdr_name_len = be->server_id_hdr_len;
3678
3679 char *hdr_val;
3680
William Lallemandd9e90662012-01-30 17:27:17 +01003681 ctx.idx = 0;
3682
Willy Tarreau45c0d982012-03-09 12:11:57 +01003683 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 -05003684 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003685 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003686 }
3687
3688 /* Add the new header requested with the server value */
3689 hdr_val = trash;
3690 memcpy(hdr_val, hdr_name, hdr_name_len);
3691 hdr_val += hdr_name_len;
3692 *hdr_val++ = ':';
3693 *hdr_val++ = ' ';
3694 hdr_val += strlcpy2(hdr_val, srv_name, trash + sizeof(trash) - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003695 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003696
3697 return 0;
3698}
3699
Willy Tarreau610ecce2010-01-04 21:15:02 +01003700/* Terminate current transaction and prepare a new one. This is very tricky
3701 * right now but it works.
3702 */
3703void http_end_txn_clean_session(struct session *s)
3704{
3705 /* FIXME: We need a more portable way of releasing a backend's and a
3706 * server's connections. We need a safer way to reinitialize buffer
3707 * flags. We also need a more accurate method for computing per-request
3708 * data.
3709 */
3710 http_silent_debug(__LINE__, s);
3711
3712 s->req->cons->flags |= SI_FL_NOLINGER;
3713 s->req->cons->shutr(s->req->cons);
3714 s->req->cons->shutw(s->req->cons);
3715
3716 http_silent_debug(__LINE__, s);
3717
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003718 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003719 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003720 if (unlikely(s->srv_conn))
3721 sess_change_server(s, NULL);
3722 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003723
3724 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3725 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003726 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003727
3728 if (s->txn.status) {
3729 int n;
3730
3731 n = s->txn.status / 100;
3732 if (n < 1 || n > 5)
3733 n = 0;
3734
3735 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003736 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003737
Willy Tarreau24657792010-02-26 10:30:28 +01003738 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01003739 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003740 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003741 }
3742
3743 /* don't count other requests' data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003744 s->logs.bytes_in -= s->req->i;
3745 s->logs.bytes_out -= s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003746
3747 /* let's do a final log if we need it */
3748 if (s->logs.logwait &&
3749 !(s->flags & SN_MONITOR) &&
3750 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
3751 s->do_log(s);
3752 }
3753
3754 s->logs.accept_date = date; /* user-visible date for logging */
3755 s->logs.tv_accept = now; /* corrected date for internal use */
3756 tv_zero(&s->logs.tv_request);
3757 s->logs.t_queue = -1;
3758 s->logs.t_connect = -1;
3759 s->logs.t_data = -1;
3760 s->logs.t_close = 0;
3761 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
3762 s->logs.srv_queue_size = 0; /* we will get this number soon */
3763
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003764 s->logs.bytes_in = s->req->total = s->req->i;
3765 s->logs.bytes_out = s->rep->total = s->rep->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003766
3767 if (s->pend_pos)
3768 pendconn_free(s->pend_pos);
3769
Willy Tarreau827aee92011-03-10 16:55:02 +01003770 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003771 if (s->flags & SN_CURR_SESS) {
3772 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01003773 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003774 }
Willy Tarreau827aee92011-03-10 16:55:02 +01003775 if (may_dequeue_tasks(target_srv(&s->target), s->be))
3776 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01003777 }
3778
Willy Tarreau9e000c62011-03-10 14:03:36 +01003779 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003780
3781 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
3782 s->req->cons->fd = -1; /* just to help with debugging */
3783 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02003784 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003785 s->req->cons->err_loc = NULL;
3786 s->req->cons->exp = TICK_ETERNITY;
3787 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau96e31212011-05-30 18:10:30 +02003788 s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST|BF_NEVER_WAIT);
3789 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 +02003790 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 +01003791 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
3792 s->txn.meth = 0;
3793 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01003794 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02003795 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01003796 s->req->cons->flags |= SI_FL_INDEP_STR;
3797
Willy Tarreau96e31212011-05-30 18:10:30 +02003798 if (s->fe->options2 & PR_O2_NODELAY) {
3799 s->req->flags |= BF_NEVER_WAIT;
3800 s->rep->flags |= BF_NEVER_WAIT;
3801 }
3802
Willy Tarreau610ecce2010-01-04 21:15:02 +01003803 /* if the request buffer is not empty, it means we're
3804 * about to process another request, so send pending
3805 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01003806 * Just don't do this if the buffer is close to be full,
3807 * because the request will wait for it to flush a little
3808 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003809 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003810 if (s->req->i) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01003811 if (s->rep->o &&
Willy Tarreau065e8332010-01-08 00:30:20 +01003812 !(s->rep->flags & BF_FULL) &&
Willy Tarreau363a5bb2012-03-02 20:14:45 +01003813 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 +01003814 s->rep->flags |= BF_EXPECT_MORE;
3815 }
Willy Tarreau90deb182010-01-07 00:20:41 +01003816
3817 /* we're removing the analysers, we MUST re-enable events detection */
3818 buffer_auto_read(s->req);
3819 buffer_auto_close(s->req);
3820 buffer_auto_read(s->rep);
3821 buffer_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003822
Willy Tarreau342b11c2010-11-24 16:22:09 +01003823 s->req->analysers = s->listener->analysers;
3824 s->req->analysers &= ~AN_REQ_DECODE_PROXY;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003825 s->rep->analysers = 0;
3826
3827 http_silent_debug(__LINE__, s);
3828}
3829
3830
3831/* This function updates the request state machine according to the response
3832 * state machine and buffer flags. It returns 1 if it changes anything (flag
3833 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3834 * it is only used to find when a request/response couple is complete. Both
3835 * this function and its equivalent should loop until both return zero. It
3836 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3837 */
3838int http_sync_req_state(struct session *s)
3839{
3840 struct buffer *buf = s->req;
3841 struct http_txn *txn = &s->txn;
3842 unsigned int old_flags = buf->flags;
3843 unsigned int old_state = txn->req.msg_state;
3844
3845 http_silent_debug(__LINE__, s);
3846 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
3847 return 0;
3848
3849 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01003850 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003851 * We can shut the read side unless we want to abort_on_close,
3852 * or we have a POST request. The issue with POST requests is
3853 * that some browsers still send a CRLF after the request, and
3854 * this CRLF must be read so that it does not remain in the kernel
3855 * buffers, otherwise a close could cause an RST on some systems
3856 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01003857 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02003858 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreau90deb182010-01-07 00:20:41 +01003859 buffer_dont_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003860
3861 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
3862 goto wait_other_side;
3863
3864 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
3865 /* The server has not finished to respond, so we
3866 * don't want to move in order not to upset it.
3867 */
3868 goto wait_other_side;
3869 }
3870
3871 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
3872 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003873 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003874 txn->req.msg_state = HTTP_MSG_TUNNEL;
3875 goto wait_other_side;
3876 }
3877
3878 /* When we get here, it means that both the request and the
3879 * response have finished receiving. Depending on the connection
3880 * mode, we'll have to wait for the last bytes to leave in either
3881 * direction, and sometimes for a close to be effective.
3882 */
3883
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003884 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
3885 /* Server-close mode : queue a connection close to the server */
3886 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW)))
Willy Tarreau610ecce2010-01-04 21:15:02 +01003887 buffer_shutw_now(buf);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003888 }
3889 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
3890 /* Option forceclose is set, or either side wants to close,
3891 * let's enforce it now that we're not expecting any new
3892 * data to come. The caller knows the session is complete
3893 * once both states are CLOSED.
3894 */
3895 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003896 buffer_shutr_now(buf);
3897 buffer_shutw_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003898 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003899 }
3900 else {
3901 /* The last possible modes are keep-alive and tunnel. Since tunnel
3902 * mode does not set the body analyser, we can't reach this place
3903 * in tunnel mode, so we're left with keep-alive only.
3904 * This mode is currently not implemented, we switch to tunnel mode.
3905 */
3906 buffer_auto_read(buf);
3907 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003908 }
3909
3910 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
3911 /* if we've just closed an output, let's switch */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003912 buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
3913
Willy Tarreau610ecce2010-01-04 21:15:02 +01003914 if (!(buf->flags & BF_OUT_EMPTY)) {
3915 txn->req.msg_state = HTTP_MSG_CLOSING;
3916 goto http_msg_closing;
3917 }
3918 else {
3919 txn->req.msg_state = HTTP_MSG_CLOSED;
3920 goto http_msg_closed;
3921 }
3922 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01003923 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01003924 }
3925
3926 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
3927 http_msg_closing:
3928 /* nothing else to forward, just waiting for the output buffer
3929 * to be empty and for the shutw_now to take effect.
3930 */
3931 if (buf->flags & BF_OUT_EMPTY) {
3932 txn->req.msg_state = HTTP_MSG_CLOSED;
3933 goto http_msg_closed;
3934 }
3935 else if (buf->flags & BF_SHUTW) {
3936 txn->req.msg_state = HTTP_MSG_ERROR;
3937 goto wait_other_side;
3938 }
3939 }
3940
3941 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
3942 http_msg_closed:
3943 goto wait_other_side;
3944 }
3945
3946 wait_other_side:
3947 http_silent_debug(__LINE__, s);
3948 return txn->req.msg_state != old_state || buf->flags != old_flags;
3949}
3950
3951
3952/* This function updates the response state machine according to the request
3953 * state machine and buffer flags. It returns 1 if it changes anything (flag
3954 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
3955 * it is only used to find when a request/response couple is complete. Both
3956 * this function and its equivalent should loop until both return zero. It
3957 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
3958 */
3959int http_sync_res_state(struct session *s)
3960{
3961 struct buffer *buf = s->rep;
3962 struct http_txn *txn = &s->txn;
3963 unsigned int old_flags = buf->flags;
3964 unsigned int old_state = txn->rsp.msg_state;
3965
3966 http_silent_debug(__LINE__, s);
3967 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
3968 return 0;
3969
3970 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
3971 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01003972 * still monitor the server connection for a possible close
3973 * while the request is being uploaded, so we don't disable
3974 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01003975 */
Willy Tarreau90deb182010-01-07 00:20:41 +01003976 /* buffer_dont_read(buf); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01003977
3978 if (txn->req.msg_state == HTTP_MSG_ERROR)
3979 goto wait_other_side;
3980
3981 if (txn->req.msg_state < HTTP_MSG_DONE) {
3982 /* The client seems to still be sending data, probably
3983 * because we got an error response during an upload.
3984 * We have the choice of either breaking the connection
3985 * or letting it pass through. Let's do the later.
3986 */
3987 goto wait_other_side;
3988 }
3989
3990 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
3991 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreau90deb182010-01-07 00:20:41 +01003992 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003993 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
3994 goto wait_other_side;
3995 }
3996
3997 /* When we get here, it means that both the request and the
3998 * response have finished receiving. Depending on the connection
3999 * mode, we'll have to wait for the last bytes to leave in either
4000 * direction, and sometimes for a close to be effective.
4001 */
4002
4003 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4004 /* Server-close mode : shut read and wait for the request
4005 * side to close its output buffer. The caller will detect
4006 * when we're in DONE and the other is in CLOSED and will
4007 * catch that for the final cleanup.
4008 */
4009 if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW)))
4010 buffer_shutr_now(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004011 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004012 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4013 /* Option forceclose is set, or either side wants to close,
4014 * let's enforce it now that we're not expecting any new
4015 * data to come. The caller knows the session is complete
4016 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017 */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004018 if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) {
4019 buffer_shutr_now(buf);
4020 buffer_shutw_now(buf);
4021 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004022 }
4023 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004024 /* The last possible modes are keep-alive and tunnel. Since tunnel
4025 * mode does not set the body analyser, we can't reach this place
4026 * in tunnel mode, so we're left with keep-alive only.
4027 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004028 */
Willy Tarreau90deb182010-01-07 00:20:41 +01004029 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004030 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031 }
4032
4033 if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) {
4034 /* if we've just closed an output, let's switch */
4035 if (!(buf->flags & BF_OUT_EMPTY)) {
4036 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4037 goto http_msg_closing;
4038 }
4039 else {
4040 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4041 goto http_msg_closed;
4042 }
4043 }
4044 goto wait_other_side;
4045 }
4046
4047 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4048 http_msg_closing:
4049 /* nothing else to forward, just waiting for the output buffer
4050 * to be empty and for the shutw_now to take effect.
4051 */
4052 if (buf->flags & BF_OUT_EMPTY) {
4053 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4054 goto http_msg_closed;
4055 }
4056 else if (buf->flags & BF_SHUTW) {
4057 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004058 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004059 if (target_srv(&s->target))
4060 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004061 goto wait_other_side;
4062 }
4063 }
4064
4065 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4066 http_msg_closed:
4067 /* drop any pending data */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004068 buffer_ignore(buf, buf->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004069 buffer_auto_close(buf);
Willy Tarreau90deb182010-01-07 00:20:41 +01004070 buffer_auto_read(buf);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004071 goto wait_other_side;
4072 }
4073
4074 wait_other_side:
4075 http_silent_debug(__LINE__, s);
4076 return txn->rsp.msg_state != old_state || buf->flags != old_flags;
4077}
4078
4079
4080/* Resync the request and response state machines. Return 1 if either state
4081 * changes.
4082 */
4083int http_resync_states(struct session *s)
4084{
4085 struct http_txn *txn = &s->txn;
4086 int old_req_state = txn->req.msg_state;
4087 int old_res_state = txn->rsp.msg_state;
4088
4089 http_silent_debug(__LINE__, s);
4090 http_sync_req_state(s);
4091 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004092 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093 if (!http_sync_res_state(s))
4094 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004095 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004096 if (!http_sync_req_state(s))
4097 break;
4098 }
4099 http_silent_debug(__LINE__, s);
4100 /* OK, both state machines agree on a compatible state.
4101 * There are a few cases we're interested in :
4102 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4103 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4104 * directions, so let's simply disable both analysers.
4105 * - HTTP_MSG_CLOSED on the response only means we must abort the
4106 * request.
4107 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4108 * with server-close mode means we've completed one request and we
4109 * must re-initialize the server connection.
4110 */
4111
4112 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4113 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4114 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4115 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4116 s->req->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004117 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004118 buffer_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004119 s->rep->analysers = 0;
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004120 buffer_auto_close(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004121 buffer_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004122 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004123 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4124 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004125 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004126 (s->rep->flags & BF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004127 s->rep->analysers = 0;
4128 buffer_auto_close(s->rep);
4129 buffer_auto_read(s->rep);
4130 s->req->analysers = 0;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004131 buffer_abort(s->req);
4132 buffer_auto_close(s->req);
Willy Tarreau90deb182010-01-07 00:20:41 +01004133 buffer_auto_read(s->req);
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004134 buffer_ignore(s->req, s->req->i);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004135 }
4136 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4137 txn->rsp.msg_state == HTTP_MSG_DONE &&
4138 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4139 /* server-close: terminate this server connection and
4140 * reinitialize a fresh-new transaction.
4141 */
4142 http_end_txn_clean_session(s);
4143 }
4144
4145 http_silent_debug(__LINE__, s);
4146 return txn->req.msg_state != old_req_state ||
4147 txn->rsp.msg_state != old_res_state;
4148}
4149
Willy Tarreaud98cf932009-12-27 22:54:55 +01004150/* This function is an analyser which forwards request body (including chunk
4151 * sizes if any). It is called as soon as we must forward, even if we forward
4152 * zero byte. The only situation where it must not be called is when we're in
4153 * tunnel mode and we want to forward till the close. It's used both to forward
4154 * remaining data and to resync after end of body. It expects the msg_state to
4155 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4156 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004157 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01004158 * bytes of pending data + the headers if not already done (between som and sov).
4159 * It eventually adjusts som to match sov after the data in between have been sent.
4160 */
4161int http_request_forward_body(struct session *s, struct buffer *req, int an_bit)
4162{
4163 struct http_txn *txn = &s->txn;
4164 struct http_msg *msg = &s->txn.req;
4165
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004166 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4167 return 0;
4168
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01004169 if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01004170 ((req->flags & BF_SHUTW) && (req->to_forward || req->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004171 /* Output closed while we were sending data. We must abort and
4172 * wake the other side up.
4173 */
4174 msg->msg_state = HTTP_MSG_ERROR;
4175 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004176 return 1;
4177 }
4178
Willy Tarreau4fe41902010-06-07 22:27:41 +02004179 /* in most states, we should abort in case of early close */
4180 buffer_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004181
4182 /* Note that we don't have to send 100-continue back because we don't
4183 * need the data to complete our job, and it's up to the server to
4184 * decide whether to return 100, 417 or anything else in return of
4185 * an "Expect: 100-continue" header.
4186 */
4187
4188 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004189 /* we have msg->sov which points to the first byte of message body.
4190 * msg->som still points to the beginning of the message. We must
4191 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004192 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004193 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004194
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004195 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004196 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4197 else {
4198 msg->msg_state = HTTP_MSG_DATA;
4199 }
4200 }
4201
Willy Tarreaud98cf932009-12-27 22:54:55 +01004202 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004203 int bytes;
4204
Willy Tarreau610ecce2010-01-04 21:15:02 +01004205 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01004206 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004207 bytes = msg->sov - msg->som;
4208 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01004209 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004210 if (likely(bytes < 0)) /* sov may have wrapped at the end */
4211 bytes += req->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01004212 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004213 msg->chunk_len += (unsigned int)bytes;
4214 msg->chunk_len -= buffer_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004215 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004216
Willy Tarreaucaabe412010-01-03 23:08:28 +01004217 if (msg->msg_state == HTTP_MSG_DATA) {
4218 /* must still forward */
4219 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004220 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004221
4222 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004223 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01004224 msg->msg_state = HTTP_MSG_DATA_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004225 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004226 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004227 }
4228 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004229 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004230 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004231 * TRAILERS state.
4232 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004233 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004234
4235 if (!ret)
4236 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004237 else if (ret < 0) {
4238 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004239 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004240 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004241 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004242 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004243 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004244 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004245 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
4246 /* we want the CRLF after the data */
4247 int ret;
4248
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004249 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004250
4251 if (ret == 0)
4252 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004253 else if (ret < 0) {
4254 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004255 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004256 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_DATA_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004257 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004258 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004259 /* we're in MSG_CHUNK_SIZE now */
4260 }
4261 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004262 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004263
4264 if (ret == 0)
4265 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004266 else if (ret < 0) {
4267 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004268 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004269 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004270 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004271 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004272 /* we're in HTTP_MSG_DONE now */
4273 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004274 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004275 int old_state = msg->msg_state;
4276
Willy Tarreau610ecce2010-01-04 21:15:02 +01004277 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004278 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004279 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4280 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
4281 buffer_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004282 if (http_resync_states(s)) {
4283 /* some state changes occurred, maybe the analyser
4284 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004285 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004286 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4287 if (req->flags & BF_SHUTW) {
4288 /* request errors are most likely due to
4289 * the server aborting the transfer.
4290 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004291 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004292 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004293 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004294 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004295 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004296 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004298 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004299
4300 /* If "option abortonclose" is set on the backend, we
4301 * want to monitor the client's connection and forward
4302 * any shutdown notification to the server, which will
4303 * decide whether to close or to go on processing the
4304 * request.
4305 */
4306 if (s->be->options & PR_O_ABRT_CLOSE) {
4307 buffer_auto_read(req);
4308 buffer_auto_close(req);
4309 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004310 else if (s->txn.meth == HTTP_METH_POST) {
4311 /* POST requests may require to read extra CRLF
4312 * sent by broken browsers and which could cause
4313 * an RST to be sent upon close on some systems
4314 * (eg: Linux).
4315 */
4316 buffer_auto_read(req);
4317 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004318
Willy Tarreau610ecce2010-01-04 21:15:02 +01004319 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004320 }
4321 }
4322
Willy Tarreaud98cf932009-12-27 22:54:55 +01004323 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004324 /* stop waiting for data if the input is closed before the end */
Willy Tarreau79ebac62010-06-07 13:47:49 +02004325 if (req->flags & BF_SHUTR) {
4326 if (!(s->flags & SN_ERR_MASK))
4327 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004328 if (!(s->flags & SN_FINST_MASK)) {
4329 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4330 s->flags |= SN_FINST_H;
4331 else
4332 s->flags |= SN_FINST_D;
4333 }
4334
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004335 s->fe->fe_counters.cli_aborts++;
4336 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004337 if (target_srv(&s->target))
4338 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004339
4340 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004341 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004342
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004343 /* waiting for the last bits to leave the buffer */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004344 if (req->flags & BF_SHUTW)
4345 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004346
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004347 /* When TE: chunked is used, we need to get there again to parse remaining
4348 * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE.
4349 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004350 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004351 buffer_dont_close(req);
4352
Willy Tarreau5c620922011-05-11 19:56:11 +02004353 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02004354 * what we did. So we always set the BF_EXPECT_MORE flag so that the
4355 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004356 * modes are already handled by the stream sock layer. We must not do
4357 * this in content-length mode because it could present the MSG_MORE
4358 * flag with the last block of forwarded data, which would cause an
4359 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004360 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004361 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004362 req->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004363
Willy Tarreau610ecce2010-01-04 21:15:02 +01004364 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004365 return 0;
4366
Willy Tarreaud98cf932009-12-27 22:54:55 +01004367 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004368 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004369 if (s->listener->counters)
4370 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004371 return_bad_req_stats_ok:
4372 txn->req.msg_state = HTTP_MSG_ERROR;
4373 if (txn->status) {
4374 /* Note: we don't send any error if some data were already sent */
4375 stream_int_retnclose(req->prod, NULL);
4376 } else {
4377 txn->status = 400;
4378 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400));
4379 }
4380 req->analysers = 0;
4381 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004382
4383 if (!(s->flags & SN_ERR_MASK))
4384 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004385 if (!(s->flags & SN_FINST_MASK)) {
4386 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4387 s->flags |= SN_FINST_H;
4388 else
4389 s->flags |= SN_FINST_D;
4390 }
4391 return 0;
4392
4393 aborted_xfer:
4394 txn->req.msg_state = HTTP_MSG_ERROR;
4395 if (txn->status) {
4396 /* Note: we don't send any error if some data were already sent */
4397 stream_int_retnclose(req->prod, NULL);
4398 } else {
4399 txn->status = 502;
4400 stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502));
4401 }
4402 req->analysers = 0;
4403 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4404
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004405 s->fe->fe_counters.srv_aborts++;
4406 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004407 if (target_srv(&s->target))
4408 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004409
4410 if (!(s->flags & SN_ERR_MASK))
4411 s->flags |= SN_ERR_SRVCL;
4412 if (!(s->flags & SN_FINST_MASK)) {
4413 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4414 s->flags |= SN_FINST_H;
4415 else
4416 s->flags |= SN_FINST_D;
4417 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004418 return 0;
4419}
4420
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004421/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4422 * processing can continue on next analysers, or zero if it either needs more
4423 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4424 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4425 * when it has nothing left to do, and may remove any analyser when it wants to
4426 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004427 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004428int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004429{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004430 struct http_txn *txn = &s->txn;
4431 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004432 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004433 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004434 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004435 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004436
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004437 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 +02004438 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004439 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004440 rep,
4441 rep->rex, rep->wex,
4442 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004443 rep->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004444 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004445
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004446 /*
4447 * Now parse the partial (or complete) lines.
4448 * We will check the response syntax, and also join multi-line
4449 * headers. An index of all the lines will be elaborated while
4450 * parsing.
4451 *
4452 * For the parsing, we use a 28 states FSM.
4453 *
4454 * Here is the information we currently have :
Willy Tarreau83e3af02009-12-28 17:39:57 +01004455 * rep->data + msg->som = beginning of response
4456 * rep->data + msg->eoh = end of processed headers / start of current one
4457 * msg->eol = end of current header or line (LF or CRLF)
Willy Tarreaua458b672012-03-05 11:17:50 +01004458 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004459 * rep->r = end of data
Willy Tarreau962c3f42010-01-10 00:15:35 +01004460 * Once we reach MSG_BODY, rep->sol = rep->data + msg->som
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004461 */
4462
Willy Tarreau83e3af02009-12-28 17:39:57 +01004463 /* There's a protected area at the end of the buffer for rewriting
4464 * purposes. We don't want to start to parse the request if the
4465 * protected area is affected, because we may have to move processed
4466 * data later, which is much more complicated.
4467 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004468 if (buffer_not_empty(rep) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004469 if (unlikely((rep->flags & BF_FULL) ||
Willy Tarreaua458b672012-03-05 11:17:50 +01004470 buffer_wrap_add(rep, rep->p + rep->i) < buffer_wrap_add(rep, rep->p + msg->next) ||
Willy Tarreau363a5bb2012-03-02 20:14:45 +01004471 buffer_wrap_add(rep, rep->p + rep->i) > rep->data + rep->size - global.tune.maxrewrite)) {
Willy Tarreau2e046c62012-03-01 16:08:30 +01004472 if (rep->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004473 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau64648412010-03-05 10:41:54 +01004474 if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT))
4475 goto abort_response;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004476 buffer_dont_close(rep);
4477 rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */
4478 return 0;
4479 }
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004480 if (rep->i <= rep->size - global.tune.maxrewrite)
Willy Tarreau21710ff2012-03-09 13:58:04 +01004481 http_message_realign(msg);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004482 }
4483
Willy Tarreaua458b672012-03-05 11:17:50 +01004484 if (likely(msg->next < rep->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004485 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004486 }
4487
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004488 /* 1: we might have to print this header in debug mode */
4489 if (unlikely((global.mode & MODE_DEBUG) &&
4490 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreauc3bfeeb2010-04-16 09:14:45 +02004491 msg->sol &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004492 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004493 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004494
Willy Tarreauea1175a2012-03-05 15:52:30 +01004495 sol = rep->p + msg->som;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02004496 eol = sol + msg->sl.st.l;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004497 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004498
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004499 sol += hdr_idx_first_pos(&txn->hdr_idx);
4500 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004501
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004502 while (cur_idx) {
4503 eol = sol + txn->hdr_idx.v[cur_idx].len;
4504 debug_hdr("srvhdr", s, sol, eol);
4505 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4506 cur_idx = txn->hdr_idx.v[cur_idx].next;
4507 }
4508 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004509
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004510 /*
4511 * Now we quickly check if we have found a full valid response.
4512 * If not so, we check the FD and buffer states before leaving.
4513 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004514 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004515 * responses are checked first.
4516 *
4517 * Depending on whether the client is still there or not, we
4518 * may send an error response back or not. Note that normally
4519 * we should only check for HTTP status there, and check I/O
4520 * errors somewhere else.
4521 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004522
Willy Tarreau655dce92009-11-08 13:10:58 +01004523 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004524 /* Invalid response */
4525 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4526 /* we detected a parsing error. We want to archive this response
4527 * in the dedicated proxy area for later troubleshooting.
4528 */
4529 hdr_response_bad:
4530 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004531 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004532
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004533 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004534 if (target_srv(&s->target)) {
4535 target_srv(&s->target)->counters.failed_resp++;
4536 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004537 }
Willy Tarreau64648412010-03-05 10:41:54 +01004538 abort_response:
Willy Tarreau90deb182010-01-07 00:20:41 +01004539 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004540 rep->analysers = 0;
4541 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004542 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004543 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004544 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
4545
4546 if (!(s->flags & SN_ERR_MASK))
4547 s->flags |= SN_ERR_PRXCOND;
4548 if (!(s->flags & SN_FINST_MASK))
4549 s->flags |= SN_FINST_H;
4550
4551 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004552 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004553
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004554 /* too large response does not fit in buffer. */
4555 else if (rep->flags & BF_FULL) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004556 if (msg->err_pos < 0)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004557 msg->err_pos = rep->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004558 goto hdr_response_bad;
4559 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004560
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004561 /* read error */
4562 else if (rep->flags & BF_READ_ERROR) {
4563 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004564 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004565
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004566 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004567 if (target_srv(&s->target)) {
4568 target_srv(&s->target)->counters.failed_resp++;
4569 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004570 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004571
Willy Tarreau90deb182010-01-07 00:20:41 +01004572 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004573 rep->analysers = 0;
4574 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004575 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004576 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004577 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004578
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004579 if (!(s->flags & SN_ERR_MASK))
4580 s->flags |= SN_ERR_SRVCL;
4581 if (!(s->flags & SN_FINST_MASK))
4582 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004583 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004584 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004585
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004586 /* read timeout : return a 504 to the client. */
4587 else if (rep->flags & BF_READ_TIMEOUT) {
4588 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004589 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004590
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004591 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004592 if (target_srv(&s->target)) {
4593 target_srv(&s->target)->counters.failed_resp++;
4594 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004595 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004596
Willy Tarreau90deb182010-01-07 00:20:41 +01004597 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004598 rep->analysers = 0;
4599 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004600 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004601 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004602 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004603
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004604 if (!(s->flags & SN_ERR_MASK))
4605 s->flags |= SN_ERR_SRVTO;
4606 if (!(s->flags & SN_FINST_MASK))
4607 s->flags |= SN_FINST_H;
4608 return 0;
4609 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004610
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004611 /* close from server, capture the response if the server has started to respond */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004612 else if (rep->flags & BF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004613 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004614 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004615
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004616 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004617 if (target_srv(&s->target)) {
4618 target_srv(&s->target)->counters.failed_resp++;
4619 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004620 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004621
Willy Tarreau90deb182010-01-07 00:20:41 +01004622 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004623 rep->analysers = 0;
4624 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004625 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004626 buffer_ignore(rep, rep->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004627 stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004628
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004629 if (!(s->flags & SN_ERR_MASK))
4630 s->flags |= SN_ERR_SRVCL;
4631 if (!(s->flags & SN_FINST_MASK))
4632 s->flags |= SN_FINST_H;
4633 return 0;
4634 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004635
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004636 /* write error to client (we don't send any message then) */
4637 else if (rep->flags & BF_WRITE_ERROR) {
4638 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004639 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004640
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004641 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004642 rep->analysers = 0;
Willy Tarreau90deb182010-01-07 00:20:41 +01004643 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004644
4645 if (!(s->flags & SN_ERR_MASK))
4646 s->flags |= SN_ERR_CLICL;
4647 if (!(s->flags & SN_FINST_MASK))
4648 s->flags |= SN_FINST_H;
4649
4650 /* process_session() will take care of the error */
4651 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004652 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004653
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004654 buffer_dont_close(rep);
4655 return 0;
4656 }
4657
4658 /* More interesting part now : we know that we have a complete
4659 * response which at least looks like HTTP. We have an indicator
4660 * of each header's length, so we can parse them quickly.
4661 */
4662
4663 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004664 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004665
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004666 /*
4667 * 1: get the status code
4668 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01004669 n = rep->p[msg->sol + msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004670 if (n < 1 || n > 5)
4671 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004672 /* when the client triggers a 4xx from the server, it's most often due
4673 * to a missing object or permission. These events should be tracked
4674 * because if they happen often, it may indicate a brute force or a
4675 * vulnerability scan.
4676 */
4677 if (n == 4)
4678 session_inc_http_err_ctr(s);
4679
Willy Tarreau827aee92011-03-10 16:55:02 +01004680 if (target_srv(&s->target))
4681 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004682
Willy Tarreau5b154472009-12-21 20:11:07 +01004683 /* check if the response is HTTP/1.1 or above */
4684 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004685 ((rep->p[msg->sol + 5] > '1') ||
4686 ((rep->p[msg->sol + 5] == '1') &&
4687 (rep->p[msg->sol + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004688 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004689
4690 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004691 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 +01004692
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004693 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004694 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004695
Willy Tarreau3a215be2012-03-09 21:39:51 +01004696 txn->status = strl2ui(rep->p + msg->sol + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004697
Willy Tarreau39650402010-03-15 19:44:39 +01004698 /* Adjust server's health based on status code. Note: status codes 501
4699 * and 505 are triggered on demand by client request, so we must not
4700 * count them as server failures.
4701 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004702 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004703 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004704 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004705 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004706 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004707 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004708
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004709 /*
4710 * 2: check for cacheability.
4711 */
4712
4713 switch (txn->status) {
4714 case 200:
4715 case 203:
4716 case 206:
4717 case 300:
4718 case 301:
4719 case 410:
4720 /* RFC2616 @13.4:
4721 * "A response received with a status code of
4722 * 200, 203, 206, 300, 301 or 410 MAY be stored
4723 * by a cache (...) unless a cache-control
4724 * directive prohibits caching."
4725 *
4726 * RFC2616 @9.5: POST method :
4727 * "Responses to this method are not cacheable,
4728 * unless the response includes appropriate
4729 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004730 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004731 if (likely(txn->meth != HTTP_METH_POST) &&
4732 (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC)))
4733 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
4734 break;
4735 default:
4736 break;
4737 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004738
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004739 /*
4740 * 3: we may need to capture headers
4741 */
4742 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01004743 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau3a215be2012-03-09 21:39:51 +01004744 capture_headers(rep->p + msg->sol, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004745 txn->rsp.cap, s->fe->rsp_cap);
4746
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004747 /* 4: determine the transfer-length.
4748 * According to RFC2616 #4.4, amended by the HTTPbis working group,
4749 * the presence of a message-body in a RESPONSE and its transfer length
4750 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004751 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004752 * All responses to the HEAD request method MUST NOT include a
4753 * message-body, even though the presence of entity-header fields
4754 * might lead one to believe they do. All 1xx (informational), 204
4755 * (No Content), and 304 (Not Modified) responses MUST NOT include a
4756 * message-body. All other responses do include a message-body,
4757 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004758 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004759 * 1. Any response which "MUST NOT" include a message-body (such as the
4760 * 1xx, 204 and 304 responses and any response to a HEAD request) is
4761 * always terminated by the first empty line after the header fields,
4762 * regardless of the entity-header fields present in the message.
4763 *
4764 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
4765 * the "chunked" transfer-coding (Section 6.2) is used, the
4766 * transfer-length is defined by the use of this transfer-coding.
4767 * If a Transfer-Encoding header field is present and the "chunked"
4768 * transfer-coding is not present, the transfer-length is defined by
4769 * the sender closing the connection.
4770 *
4771 * 3. If a Content-Length header field is present, its decimal value in
4772 * OCTETs represents both the entity-length and the transfer-length.
4773 * If a message is received with both a Transfer-Encoding header
4774 * field and a Content-Length header field, the latter MUST be ignored.
4775 *
4776 * 4. If the message uses the media type "multipart/byteranges", and
4777 * the transfer-length is not otherwise specified, then this self-
4778 * delimiting media type defines the transfer-length. This media
4779 * type MUST NOT be used unless the sender knows that the recipient
4780 * can parse it; the presence in a request of a Range header with
4781 * multiple byte-range specifiers from a 1.1 client implies that the
4782 * client can parse multipart/byteranges responses.
4783 *
4784 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004785 */
4786
4787 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01004788 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004789 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004790 * FIXME: should we parse anyway and return an error on chunked encoding ?
4791 */
4792 if (txn->meth == HTTP_METH_HEAD ||
4793 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004794 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004795 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004796 goto skip_content_length;
4797 }
4798
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004799 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004800 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004801 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004802 http_find_header2("Transfer-Encoding", 17, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004803 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004804 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
4805 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004806 /* bad transfer-encoding (chunked followed by something else) */
4807 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004808 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004809 break;
4810 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004811 }
4812
4813 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
4814 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004815 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau3a215be2012-03-09 21:39:51 +01004816 http_find_header2("Content-Length", 14, rep->p + msg->sol, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004817 signed long long cl;
4818
Willy Tarreauad14f752011-09-02 20:33:27 +02004819 if (!ctx.vlen) {
4820 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004821 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004822 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004823
Willy Tarreauad14f752011-09-02 20:33:27 +02004824 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
4825 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004826 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02004827 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004828
Willy Tarreauad14f752011-09-02 20:33:27 +02004829 if (cl < 0) {
4830 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004831 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02004832 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004833
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004834 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreauad14f752011-09-02 20:33:27 +02004835 msg->err_pos = ctx.line + ctx.val - rep->data;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004836 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02004837 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004838
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004839 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01004840 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004841 }
4842
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004843 /* FIXME: we should also implement the multipart/byterange method.
4844 * For now on, we resort to close mode in this case (unknown length).
4845 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004846skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004847
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004848 /* end of job, return OK */
4849 rep->analysers &= ~an_bit;
4850 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau90deb182010-01-07 00:20:41 +01004851 buffer_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004852 return 1;
4853}
4854
4855/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01004856 * It normally returns 1 unless it wants to break. It relies on buffers flags,
4857 * and updates t->rep->analysers. It might make sense to explode it into several
4858 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004859 */
4860int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px)
4861{
4862 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004863 struct http_msg *msg = &txn->rsp;
4864 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01004865 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004866
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004867 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 +02004868 now_ms, __FUNCTION__,
4869 t,
4870 rep,
4871 rep->rex, rep->wex,
4872 rep->flags,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004873 rep->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004874 rep->analysers);
4875
Willy Tarreau655dce92009-11-08 13:10:58 +01004876 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004877 return 0;
4878
4879 rep->analysers &= ~an_bit;
4880 rep->analyse_exp = TICK_ETERNITY;
4881
Willy Tarreau5b154472009-12-21 20:11:07 +01004882 /* Now we have to check if we need to modify the Connection header.
4883 * This is more difficult on the response than it is on the request,
4884 * because we can have two different HTTP versions and we don't know
4885 * how the client will interprete a response. For instance, let's say
4886 * that the client sends a keep-alive request in HTTP/1.0 and gets an
4887 * HTTP/1.1 response without any header. Maybe it will bound itself to
4888 * HTTP/1.0 because it only knows about it, and will consider the lack
4889 * of header as a close, or maybe it knows HTTP/1.1 and can consider
4890 * the lack of header as a keep-alive. Thus we will use two flags
4891 * indicating how a request MAY be understood by the client. In case
4892 * of multiple possibilities, we'll fix the header to be explicit. If
4893 * ambiguous cases such as both close and keepalive are seen, then we
4894 * will fall back to explicit close. Note that we won't take risks with
4895 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01004896 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01004897 */
4898
Willy Tarreaudc008c52010-02-01 16:20:08 +01004899 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
4900 txn->status == 101)) {
4901 /* Either we've established an explicit tunnel, or we're
4902 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004903 * to understand the next protocols. We have to switch to tunnel
4904 * mode, so that we transfer the request and responses then let
4905 * this protocol pass unmodified. When we later implement specific
4906 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01004907 * header which contains information about that protocol for
4908 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01004909 */
4910 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
4911 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01004912 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
4913 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
4914 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01004915 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01004916
Willy Tarreau60466522010-01-18 19:08:45 +01004917 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004918 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01004919 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
4920 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01004921
Willy Tarreau60466522010-01-18 19:08:45 +01004922 /* now adjust header transformations depending on current state */
4923 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
4924 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4925 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004926 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01004927 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004928 }
Willy Tarreau60466522010-01-18 19:08:45 +01004929 else { /* SCL / KAL */
4930 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004931 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01004932 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01004933 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004934
Willy Tarreau60466522010-01-18 19:08:45 +01004935 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004936 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01004937
Willy Tarreau60466522010-01-18 19:08:45 +01004938 /* Some keep-alive responses are converted to Server-close if
4939 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01004940 */
Willy Tarreau60466522010-01-18 19:08:45 +01004941 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
4942 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004943 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01004944 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004945 }
Willy Tarreau5b154472009-12-21 20:11:07 +01004946 }
4947
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004948 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004949 /*
4950 * 3: we will have to evaluate the filters.
4951 * As opposed to version 1.2, now they will be evaluated in the
4952 * filters order and not in the header order. This means that
4953 * each filter has to be validated among all headers.
4954 *
4955 * Filters are tried with ->be first, then with ->fe if it is
4956 * different from ->be.
4957 */
4958
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004959 cur_proxy = t->be;
4960 while (1) {
4961 struct proxy *rule_set = cur_proxy;
4962
4963 /* try headers filters */
4964 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01004965 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004966 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01004967 if (target_srv(&t->target)) {
4968 target_srv(&t->target)->counters.failed_resp++;
4969 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004970 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004971 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004972 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02004973 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004974 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004975 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004976 buffer_ignore(rep, rep->i);
Willy Tarreau8e89b842009-10-18 23:56:35 +02004977 stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004978 if (!(t->flags & SN_ERR_MASK))
4979 t->flags |= SN_ERR_PRXCOND;
4980 if (!(t->flags & SN_FINST_MASK))
4981 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02004982 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01004983 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004984 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004985
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004986 /* has the response been denied ? */
4987 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01004988 if (target_srv(&t->target))
4989 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004990
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004991 t->be->be_counters.denied_resp++;
4992 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004993 if (t->listener->counters)
4994 t->listener->counters->denied_resp++;
4995
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004996 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01004997 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004998
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004999 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005000 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005001 if (txn->status < 200)
5002 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005003 if (wl->cond) {
5004 int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR);
5005 ret = acl_pass(ret);
5006 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5007 ret = !ret;
5008 if (!ret)
5009 continue;
5010 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005011 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005012 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005013 }
5014
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005015 /* check whether we're already working on the frontend */
5016 if (cur_proxy == t->fe)
5017 break;
5018 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005019 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005020
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005021 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005022 * We may be facing a 100-continue response, in which case this
5023 * is not the right response, and we're waiting for the next one.
5024 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005025 * next one.
5026 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005027 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005028 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau3a215be2012-03-09 21:39:51 +01005029 msg->next -= buffer_forward(rep, msg->next - msg->sol);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005030 msg->msg_state = HTTP_MSG_RPBEFORE;
5031 txn->status = 0;
5032 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5033 return 1;
5034 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005035 else if (unlikely(txn->status < 200))
5036 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005037
5038 /* we don't have any 1xx status code now */
5039
5040 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005041 * 4: check for server cookie.
5042 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005043 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5044 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005045 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005046
Willy Tarreaubaaee002006-06-26 02:48:02 +02005047
Willy Tarreaua15645d2007-03-18 16:22:39 +01005048 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005049 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005050 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005051 if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0)
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005052 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005053
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005054 /*
5055 * 6: add server cookie in the response if needed
5056 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005057 if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) &&
Willy Tarreauba4c5be2010-10-23 12:46:42 +02005058 !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005059 (!(t->flags & SN_DIRECT) ||
5060 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5061 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5062 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5063 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005064 (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) &&
5065 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005066 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005067 /* the server is known, it's not the one the client requested, or the
5068 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005069 * insert a set-cookie here, except if we want to insert only on POST
5070 * requests and this one isn't. Note that servers which don't have cookies
5071 * (eg: some backup servers) will return a full cookie removal request.
5072 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005073 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005074 len = sprintf(trash,
5075 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5076 t->be->cookie_name);
5077 }
5078 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005079 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005080
5081 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5082 /* emit last_date, which is mandatory */
5083 trash[len++] = COOKIE_DELIM_DATE;
5084 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5085 if (t->be->cookie_maxlife) {
5086 /* emit first_date, which is either the original one or
5087 * the current date.
5088 */
5089 trash[len++] = COOKIE_DELIM_DATE;
5090 s30tob64(txn->cookie_first_date ?
5091 txn->cookie_first_date >> 2 :
5092 (date.tv_sec+3) >> 2, trash + len);
5093 len += 5;
5094 }
5095 }
5096 len += sprintf(trash + len, "; path=/");
5097 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005098
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005099 if (t->be->cookie_domain)
5100 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005101
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005102 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005103 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005104
Willy Tarreauf1348312010-10-07 15:54:11 +02005105 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005106 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005107 /* the server did not change, only the date was updated */
5108 txn->flags |= TX_SCK_UPDATED;
5109 else
5110 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005111
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005112 /* Here, we will tell an eventual cache on the client side that we don't
5113 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5114 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5115 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5116 */
5117 if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005118
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005119 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5120
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005121 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005122 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005123 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005124 }
5125 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005126
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005127 /*
5128 * 7: check if result will be cacheable with a cookie.
5129 * We'll block the response if security checks have caught
5130 * nasty things such as a cacheable cookie.
5131 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005132 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5133 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005134 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005135
5136 /* we're in presence of a cacheable response containing
5137 * a set-cookie header. We'll block it as requested by
5138 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005139 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005140 if (target_srv(&t->target))
5141 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005142
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005143 t->be->be_counters.denied_resp++;
5144 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005145 if (t->listener->counters)
5146 t->listener->counters->denied_resp++;
5147
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005148 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005149 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005150 send_log(t->be, LOG_ALERT,
5151 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005152 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005153 goto return_srv_prx_502;
5154 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005155
5156 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005157 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005158 */
Willy Tarreau60466522010-01-18 19:08:45 +01005159 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5160 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5161 unsigned int want_flags = 0;
5162
5163 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5164 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5165 /* we want a keep-alive response here. Keep-alive header
5166 * required if either side is not 1.1.
5167 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005168 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005169 want_flags |= TX_CON_KAL_SET;
5170 }
5171 else {
5172 /* we want a close response here. Close header required if
5173 * the server is 1.1, regardless of the client.
5174 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005175 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005176 want_flags |= TX_CON_CLO_SET;
5177 }
5178
5179 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005180 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005181 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005182
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005183 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005184 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005185 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005186 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005187
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005188 /*************************************************************
5189 * OK, that's finished for the headers. We have done what we *
5190 * could. Let's switch to the DATA state. *
5191 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005192
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005193 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005194
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195 /* if the user wants to log as soon as possible, without counting
5196 * bytes from the server, then this is the right moment. We have
5197 * to temporarily assign bytes_out to log what we currently have.
5198 */
5199 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5200 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5201 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005202 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005203 t->logs.bytes_out = 0;
5204 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005205
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005206 /* Note: we must not try to cheat by jumping directly to DATA,
5207 * otherwise we would not let the client side wake up.
5208 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005209
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005210 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005211 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005212 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005213}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005214
Willy Tarreaud98cf932009-12-27 22:54:55 +01005215/* This function is an analyser which forwards response body (including chunk
5216 * sizes if any). It is called as soon as we must forward, even if we forward
5217 * zero byte. The only situation where it must not be called is when we're in
5218 * tunnel mode and we want to forward till the close. It's used both to forward
5219 * remaining data and to resync after end of body. It expects the msg_state to
5220 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5221 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005222 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreaud98cf932009-12-27 22:54:55 +01005223 * bytes of pending data + the headers if not already done (between som and sov).
5224 * It eventually adjusts som to match sov after the data in between have been sent.
5225 */
5226int http_response_forward_body(struct session *s, struct buffer *res, int an_bit)
5227{
5228 struct http_txn *txn = &s->txn;
5229 struct http_msg *msg = &s->txn.rsp;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005230 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005231
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005232 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5233 return 0;
5234
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005235 if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) ||
Willy Tarreau2e046c62012-03-01 16:08:30 +01005236 ((res->flags & BF_SHUTW) && (res->to_forward || res->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005237 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005238 /* Output closed while we were sending data. We must abort and
5239 * wake the other side up.
5240 */
5241 msg->msg_state = HTTP_MSG_ERROR;
5242 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005243 return 1;
5244 }
5245
Willy Tarreau4fe41902010-06-07 22:27:41 +02005246 /* in most states, we should abort in case of early close */
5247 buffer_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005248
Willy Tarreaud98cf932009-12-27 22:54:55 +01005249 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005250 /* we have msg->sov which points to the first byte of message body.
5251 * msg->som still points to the beginning of the message. We must
5252 * save the body in msg->next because it survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005253 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01005254 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01005255
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005256 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005257 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
5258 else {
5259 msg->msg_state = HTTP_MSG_DATA;
5260 }
5261 }
5262
Willy Tarreaud98cf932009-12-27 22:54:55 +01005263 while (1) {
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005264 int bytes;
5265
Willy Tarreau610ecce2010-01-04 21:15:02 +01005266 http_silent_debug(__LINE__, s);
Willy Tarreau638cd022010-01-03 07:42:04 +01005267 /* we may have some data pending */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005268 bytes = msg->sov - msg->som;
5269 if (msg->chunk_len || bytes) {
Willy Tarreau638cd022010-01-03 07:42:04 +01005270 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005271 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5272 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005273 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005274 msg->chunk_len += (unsigned int)bytes;
5275 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01005276 }
5277
Willy Tarreaucaabe412010-01-03 23:08:28 +01005278 if (msg->msg_state == HTTP_MSG_DATA) {
5279 /* must still forward */
5280 if (res->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005281 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005282
5283 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005284 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaucaabe412010-01-03 23:08:28 +01005285 msg->msg_state = HTTP_MSG_DATA_CRLF;
5286 else
5287 msg->msg_state = HTTP_MSG_DONE;
5288 }
5289 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005290 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005291 * set ->sov and ->next to point to the body and switch to DATA or
5292 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005293 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005294 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005295
5296 if (!ret)
5297 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005298 else if (ret < 0) {
5299 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005300 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005301 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005302 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005303 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005304 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005305 else if (msg->msg_state == HTTP_MSG_DATA_CRLF) {
5306 /* we want the CRLF after the data */
5307 int ret;
5308
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005309 ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005310
5311 if (!ret)
5312 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005313 else if (ret < 0) {
5314 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005315 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_DATA_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005316 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005317 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005318 /* we're in MSG_CHUNK_SIZE now */
5319 }
5320 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005321 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005322
Willy Tarreaud98cf932009-12-27 22:54:55 +01005323 if (ret == 0)
5324 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005325 else if (ret < 0) {
5326 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005327 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005328 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005329 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005330 /* we're in HTTP_MSG_DONE now */
5331 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005332 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005333 int old_state = msg->msg_state;
5334
Willy Tarreau610ecce2010-01-04 21:15:02 +01005335 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005336 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005337 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5338 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5339 buffer_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005340 if (http_resync_states(s)) {
5341 http_silent_debug(__LINE__, s);
5342 /* some state changes occurred, maybe the analyser
5343 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005344 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005345 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5346 if (res->flags & BF_SHUTW) {
5347 /* response errors are most likely due to
5348 * the client aborting the transfer.
5349 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005350 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005351 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005352 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005353 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005354 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005355 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005356 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005357 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005358 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005359 }
5360 }
5361
Willy Tarreaud98cf932009-12-27 22:54:55 +01005362 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005363 /* stop waiting for data if the input is closed before the end */
Willy Tarreau40dba092010-03-04 18:14:51 +01005364 if (res->flags & BF_SHUTR) {
5365 if (!(s->flags & SN_ERR_MASK))
5366 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005367 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005368 if (target_srv(&s->target))
5369 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005370 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005371 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005372
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005373 if (res->flags & BF_SHUTW)
5374 goto aborted_xfer;
5375
Willy Tarreau40dba092010-03-04 18:14:51 +01005376 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005377 if (!s->req->analysers)
5378 goto return_bad_res;
5379
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005380 /* forward any pending data */
5381 bytes = msg->sov - msg->som;
5382 if (msg->chunk_len || bytes) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005383 msg->som = msg->sov;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005384 if (likely(bytes < 0)) /* sov may have wrapped at the end */
5385 bytes += res->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01005386 msg->next -= bytes; /* will be forwarded */
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02005387 msg->chunk_len += (unsigned int)bytes;
5388 msg->chunk_len -= buffer_forward(res, msg->chunk_len);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005389 }
5390
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005391 /* When TE: chunked is used, we need to get there again to parse remaining
5392 * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE.
5393 * Similarly, with keep-alive on the client side, we don't want to forward a
5394 * close.
5395 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005396 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005397 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5398 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
5399 buffer_dont_close(res);
5400
Willy Tarreau5c620922011-05-11 19:56:11 +02005401 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau07293032011-05-30 18:29:28 +02005402 * what we did. So we always set the BF_EXPECT_MORE flag so that the
5403 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005404 * modes are already handled by the stream sock layer. We must not do
5405 * this in content-length mode because it could present the MSG_MORE
5406 * flag with the last block of forwarded data, which would cause an
5407 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005408 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005409 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005410 res->flags |= BF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005411
Willy Tarreaud98cf932009-12-27 22:54:55 +01005412 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005413 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005414 return 0;
5415
Willy Tarreau40dba092010-03-04 18:14:51 +01005416 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005417 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005418 if (target_srv(&s->target))
5419 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005420
5421 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005422 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005423 /* don't send any error message as we're in the body */
5424 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005425 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005426 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005427 if (target_srv(&s->target))
5428 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005429
5430 if (!(s->flags & SN_ERR_MASK))
5431 s->flags |= SN_ERR_PRXCOND;
5432 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005433 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005434 return 0;
5435
5436 aborted_xfer:
5437 txn->rsp.msg_state = HTTP_MSG_ERROR;
5438 /* don't send any error message as we're in the body */
5439 stream_int_retnclose(res->cons, NULL);
5440 res->analysers = 0;
5441 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5442
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005443 s->fe->fe_counters.cli_aborts++;
5444 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005445 if (target_srv(&s->target))
5446 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005447
5448 if (!(s->flags & SN_ERR_MASK))
5449 s->flags |= SN_ERR_CLICL;
5450 if (!(s->flags & SN_FINST_MASK))
5451 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005452 return 0;
5453}
5454
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005455/* Iterate the same filter through all request headers.
5456 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005457 * Since it can manage the switch to another backend, it updates the per-proxy
5458 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005459 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005460int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005461{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005462 char term;
5463 char *cur_ptr, *cur_end, *cur_next;
5464 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005465 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005466 struct hdr_idx_elem *cur_hdr;
5467 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005469 last_hdr = 0;
5470
Willy Tarreau3a215be2012-03-09 21:39:51 +01005471 cur_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005472 old_idx = 0;
5473
5474 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005475 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005476 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005477 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005478 (exp->action == ACT_ALLOW ||
5479 exp->action == ACT_DENY ||
5480 exp->action == ACT_TARPIT))
5481 return 0;
5482
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005483 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005484 if (!cur_idx)
5485 break;
5486
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005487 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005488 cur_ptr = cur_next;
5489 cur_end = cur_ptr + cur_hdr->len;
5490 cur_next = cur_end + cur_hdr->cr + 1;
5491
5492 /* Now we have one header between cur_ptr and cur_end,
5493 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005494 */
5495
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005496 /* The annoying part is that pattern matching needs
5497 * that we modify the contents to null-terminate all
5498 * strings before testing them.
5499 */
5500
5501 term = *cur_end;
5502 *cur_end = '\0';
5503
5504 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5505 switch (exp->action) {
5506 case ACT_SETBE:
5507 /* It is not possible to jump a second time.
5508 * FIXME: should we return an HTTP/500 here so that
5509 * the admin knows there's a problem ?
5510 */
5511 if (t->be != t->fe)
5512 break;
5513
5514 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005515 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005516 last_hdr = 1;
5517 break;
5518
5519 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005520 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005521 last_hdr = 1;
5522 break;
5523
5524 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005525 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005526 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005527
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005528 t->fe->fe_counters.denied_req++;
5529 if (t->fe != t->be)
5530 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005531 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005532 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005533
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005534 break;
5535
5536 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005537 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005538 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005539
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005540 t->fe->fe_counters.denied_req++;
5541 if (t->fe != t->be)
5542 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005543 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005544 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005545
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005546 break;
5547
5548 case ACT_REPLACE:
5549 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5550 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5551 /* FIXME: if the user adds a newline in the replacement, the
5552 * index will not be recalculated for now, and the new line
5553 * will not be counted as a new header.
5554 */
5555
5556 cur_end += delta;
5557 cur_next += delta;
5558 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005559 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005560 break;
5561
5562 case ACT_REMOVE:
5563 delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0);
5564 cur_next += delta;
5565
Willy Tarreaufa355d42009-11-29 18:12:29 +01005566 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005567 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5568 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005569 cur_hdr->len = 0;
5570 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005571 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005572 break;
5573
5574 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005575 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005576 if (cur_end)
5577 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005578
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005579 /* keep the link from this header to next one in case of later
5580 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005581 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005582 old_idx = cur_idx;
5583 }
5584 return 0;
5585}
5586
5587
5588/* Apply the filter to the request line.
5589 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5590 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005591 * Since it can manage the switch to another backend, it updates the per-proxy
5592 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005593 */
5594int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp)
5595{
5596 char term;
5597 char *cur_ptr, *cur_end;
5598 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005599 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005600 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005601
Willy Tarreau58f10d72006-12-04 02:26:12 +01005602
Willy Tarreau3d300592007-03-18 18:34:41 +01005603 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005604 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005605 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005606 (exp->action == ACT_ALLOW ||
5607 exp->action == ACT_DENY ||
5608 exp->action == ACT_TARPIT))
5609 return 0;
5610 else if (exp->action == ACT_REMOVE)
5611 return 0;
5612
5613 done = 0;
5614
Willy Tarreau3a215be2012-03-09 21:39:51 +01005615 cur_ptr = req->p + txn->req.sol;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005616 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005617
5618 /* Now we have the request line between cur_ptr and cur_end */
5619
5620 /* The annoying part is that pattern matching needs
5621 * that we modify the contents to null-terminate all
5622 * strings before testing them.
5623 */
5624
5625 term = *cur_end;
5626 *cur_end = '\0';
5627
5628 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5629 switch (exp->action) {
5630 case ACT_SETBE:
5631 /* It is not possible to jump a second time.
5632 * FIXME: should we return an HTTP/500 here so that
5633 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005634 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005635 if (t->be != t->fe)
5636 break;
5637
5638 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005639 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005640 done = 1;
5641 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005642
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005643 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005644 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005645 done = 1;
5646 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005647
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005648 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005649 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005650
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005651 t->fe->fe_counters.denied_req++;
5652 if (t->fe != t->be)
5653 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005654 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005655 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005656
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005657 done = 1;
5658 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005659
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005660 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005661 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005662
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005663 t->fe->fe_counters.denied_req++;
5664 if (t->fe != t->be)
5665 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005666 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005667 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005668
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005669 done = 1;
5670 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005671
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005672 case ACT_REPLACE:
5673 *cur_end = term; /* restore the string terminator */
5674 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
5675 delta = buffer_replace2(req, cur_ptr, cur_end, trash, len);
5676 /* FIXME: if the user adds a newline in the replacement, the
5677 * index will not be recalculated for now, and the new line
5678 * will not be counted as a new header.
5679 */
Willy Tarreaua496b602006-12-17 23:15:24 +01005680
Willy Tarreaufa355d42009-11-29 18:12:29 +01005681 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005682 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01005683 cur_end = (char *)http_parse_reqline(&txn->req, req->data,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005684 HTTP_MSG_RQMETH,
5685 cur_ptr, cur_end + 1,
5686 NULL, NULL);
5687 if (unlikely(!cur_end))
5688 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01005689
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005690 /* we have a full request and we know that we have either a CR
5691 * or an LF at <ptr>.
5692 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005693 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
5694 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005695 /* there is no point trying this regex on headers */
5696 return 1;
5697 }
5698 }
5699 *cur_end = term; /* restore the string terminator */
5700 return done;
5701}
Willy Tarreau97de6242006-12-27 17:18:38 +01005702
Willy Tarreau58f10d72006-12-04 02:26:12 +01005703
Willy Tarreau58f10d72006-12-04 02:26:12 +01005704
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005705/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01005706 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005707 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01005708 * unparsable request. Since it can manage the switch to another backend, it
5709 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005710 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005711int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005712{
Willy Tarreau6c123b12010-01-28 20:22:06 +01005713 struct http_txn *txn = &s->txn;
5714 struct hdr_exp *exp;
5715
5716 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005717 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005718
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005719 /*
5720 * The interleaving of transformations and verdicts
5721 * makes it difficult to decide to continue or stop
5722 * the evaluation.
5723 */
5724
Willy Tarreau6c123b12010-01-28 20:22:06 +01005725 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
5726 break;
5727
Willy Tarreau3d300592007-03-18 18:34:41 +01005728 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005729 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01005730 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005731 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01005732
5733 /* if this filter had a condition, evaluate it now and skip to
5734 * next filter if the condition does not match.
5735 */
5736 if (exp->cond) {
5737 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ);
5738 ret = acl_pass(ret);
5739 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
5740 ret = !ret;
5741
5742 if (!ret)
5743 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005744 }
5745
5746 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005747 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005748 if (unlikely(ret < 0))
5749 return -1;
5750
5751 if (likely(ret == 0)) {
5752 /* The filter did not match the request, it can be
5753 * iterated through all headers.
5754 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01005755 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005756 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005757 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005758 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005759}
5760
5761
Willy Tarreaua15645d2007-03-18 16:22:39 +01005762
Willy Tarreau58f10d72006-12-04 02:26:12 +01005763/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005764 * Try to retrieve the server associated to the appsession.
5765 * If the server is found, it's assigned to the session.
5766 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01005767void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005768 struct http_txn *txn = &t->txn;
5769 appsess *asession = NULL;
5770 char *sessid_temp = NULL;
5771
Cyril Bontéb21570a2009-11-29 20:04:48 +01005772 if (len > t->be->appsession_len) {
5773 len = t->be->appsession_len;
5774 }
5775
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005776 if (t->be->options2 & PR_O2_AS_REQL) {
5777 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005778 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005779 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005780 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005781 }
5782
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005783 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005784 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5785 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5786 return;
5787 }
5788
Willy Tarreaua3377ee2010-01-10 10:49:11 +01005789 memcpy(txn->sessid, buf, len);
5790 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005791 }
5792
5793 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
5794 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
5795 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
5796 return;
5797 }
5798
Cyril Bontéb21570a2009-11-29 20:04:48 +01005799 memcpy(sessid_temp, buf, len);
5800 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005801
5802 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
5803 /* free previously allocated memory */
5804 pool_free2(apools.sessid, sessid_temp);
5805
5806 if (asession != NULL) {
5807 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
5808 if (!(t->be->options2 & PR_O2_AS_REQL))
5809 asession->request_count++;
5810
5811 if (asession->serverid != NULL) {
5812 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005813
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005814 while (srv) {
5815 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01005816 if ((srv->state & SRV_RUNNING) ||
5817 (t->be->options & PR_O_PERSIST) ||
5818 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005819 /* we found the server and it's usable */
5820 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01005821 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005822 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01005823 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01005824
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005825 break;
5826 } else {
5827 txn->flags &= ~TX_CK_MASK;
5828 txn->flags |= TX_CK_DOWN;
5829 }
5830 }
5831 srv = srv->next;
5832 }
5833 }
5834 }
5835}
5836
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005837/* Find the end of a cookie value contained between <s> and <e>. It works the
5838 * same way as with headers above except that the semi-colon also ends a token.
5839 * See RFC2965 for more information. Note that it requires a valid header to
5840 * return a valid result.
5841 */
5842char *find_cookie_value_end(char *s, const char *e)
5843{
5844 int quoted, qdpair;
5845
5846 quoted = qdpair = 0;
5847 for (; s < e; s++) {
5848 if (qdpair) qdpair = 0;
5849 else if (quoted) {
5850 if (*s == '\\') qdpair = 1;
5851 else if (*s == '"') quoted = 0;
5852 }
5853 else if (*s == '"') quoted = 1;
5854 else if (*s == ',' || *s == ';') return s;
5855 }
5856 return s;
5857}
5858
5859/* Delete a value in a header between delimiters <from> and <next> in buffer
5860 * <buf>. The number of characters displaced is returned, and the pointer to
5861 * the first delimiter is updated if required. The function tries as much as
5862 * possible to respect the following principles :
5863 * - replace <from> delimiter by the <next> one unless <from> points to a
5864 * colon, in which case <next> is simply removed
5865 * - set exactly one space character after the new first delimiter, unless
5866 * there are not enough characters in the block being moved to do so.
5867 * - remove unneeded spaces before the previous delimiter and after the new
5868 * one.
5869 *
5870 * It is the caller's responsibility to ensure that :
5871 * - <from> points to a valid delimiter or the colon ;
5872 * - <next> points to a valid delimiter or the final CR/LF ;
5873 * - there are non-space chars before <from> ;
5874 * - there is a CR/LF at or after <next>.
5875 */
5876int del_hdr_value(struct buffer *buf, char **from, char *next)
5877{
5878 char *prev = *from;
5879
5880 if (*prev == ':') {
5881 /* We're removing the first value, preserve the colon and add a
5882 * space if possible.
5883 */
5884 if (!http_is_crlf[(unsigned char)*next])
5885 next++;
5886 prev++;
5887 if (prev < next)
5888 *prev++ = ' ';
5889
5890 while (http_is_spht[(unsigned char)*next])
5891 next++;
5892 } else {
5893 /* Remove useless spaces before the old delimiter. */
5894 while (http_is_spht[(unsigned char)*(prev-1)])
5895 prev--;
5896 *from = prev;
5897
5898 /* copy the delimiter and if possible a space if we're
5899 * not at the end of the line.
5900 */
5901 if (!http_is_crlf[(unsigned char)*next]) {
5902 *prev++ = *next++;
5903 if (prev + 1 < next)
5904 *prev++ = ' ';
5905 while (http_is_spht[(unsigned char)*next])
5906 next++;
5907 }
5908 }
5909 return buffer_replace2(buf, prev, next, NULL, 0);
5910}
5911
Cyril Bontébf47aeb2009-10-15 00:15:40 +02005912/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01005913 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005914 * desirable to call it only when needed. This code is quite complex because
5915 * of the multiple very crappy and ambiguous syntaxes we have to support. it
5916 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01005917 */
5918void manage_client_side_cookies(struct session *t, struct buffer *req)
5919{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005920 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005921 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005922 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005923 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
5924 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005926 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01005927 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01005928 hdr_next = req->p + txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01005929
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005930 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005931 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005932 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005933
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005934 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005935 hdr_beg = hdr_next;
5936 hdr_end = hdr_beg + cur_hdr->len;
5937 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005938
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005939 /* We have one full header between hdr_beg and hdr_end, and the
5940 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01005941 * "Cookie:" headers.
5942 */
5943
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005944 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01005945 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01005946 old_idx = cur_idx;
5947 continue;
5948 }
5949
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005950 del_from = NULL; /* nothing to be deleted */
5951 preserve_hdr = 0; /* assume we may kill the whole header */
5952
Willy Tarreau58f10d72006-12-04 02:26:12 +01005953 /* Now look for cookies. Conforming to RFC2109, we have to support
5954 * attributes whose name begin with a '$', and associate them with
5955 * the right cookie, if we want to delete this cookie.
5956 * So there are 3 cases for each cookie read :
5957 * 1) it's a special attribute, beginning with a '$' : ignore it.
5958 * 2) it's a server id cookie that we *MAY* want to delete : save
5959 * some pointers on it (last semi-colon, beginning of cookie...)
5960 * 3) it's an application cookie : we *MAY* have to delete a previous
5961 * "special" cookie.
5962 * At the end of loop, if a "special" cookie remains, we may have to
5963 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005964 * *MUST* delete it.
5965 *
5966 * Note: RFC2965 is unclear about the processing of spaces around
5967 * the equal sign in the ATTR=VALUE form. A careful inspection of
5968 * the RFC explicitly allows spaces before it, and not within the
5969 * tokens (attrs or values). An inspection of RFC2109 allows that
5970 * too but section 10.1.3 lets one think that spaces may be allowed
5971 * after the equal sign too, resulting in some (rare) buggy
5972 * implementations trying to do that. So let's do what servers do.
5973 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
5974 * allowed quoted strings in values, with any possible character
5975 * after a backslash, including control chars and delimitors, which
5976 * causes parsing to become ambiguous. Browsers also allow spaces
5977 * within values even without quotes.
5978 *
5979 * We have to keep multiple pointers in order to support cookie
5980 * removal at the beginning, middle or end of header without
5981 * corrupting the header. All of these headers are valid :
5982 *
5983 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
5984 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
5985 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
5986 * | | | | | | | | |
5987 * | | | | | | | | hdr_end <--+
5988 * | | | | | | | +--> next
5989 * | | | | | | +----> val_end
5990 * | | | | | +-----------> val_beg
5991 * | | | | +--------------> equal
5992 * | | | +----------------> att_end
5993 * | | +---------------------> att_beg
5994 * | +--------------------------> prev
5995 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01005996 */
5997
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02005998 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
5999 /* Iterate through all cookies on this line */
6000
6001 /* find att_beg */
6002 att_beg = prev + 1;
6003 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6004 att_beg++;
6005
6006 /* find att_end : this is the first character after the last non
6007 * space before the equal. It may be equal to hdr_end.
6008 */
6009 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006010
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006011 while (equal < hdr_end) {
6012 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006013 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006014 if (http_is_spht[(unsigned char)*equal++])
6015 continue;
6016 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006017 }
6018
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006019 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6020 * is between <att_beg> and <equal>, both may be identical.
6021 */
6022
6023 /* look for end of cookie if there is an equal sign */
6024 if (equal < hdr_end && *equal == '=') {
6025 /* look for the beginning of the value */
6026 val_beg = equal + 1;
6027 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6028 val_beg++;
6029
6030 /* find the end of the value, respecting quotes */
6031 next = find_cookie_value_end(val_beg, hdr_end);
6032
6033 /* make val_end point to the first white space or delimitor after the value */
6034 val_end = next;
6035 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6036 val_end--;
6037 } else {
6038 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006039 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006040
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006041 /* We have nothing to do with attributes beginning with '$'. However,
6042 * they will automatically be removed if a header before them is removed,
6043 * since they're supposed to be linked together.
6044 */
6045 if (*att_beg == '$')
6046 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006047
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006048 /* Ignore cookies with no equal sign */
6049 if (equal == next) {
6050 /* This is not our cookie, so we must preserve it. But if we already
6051 * scheduled another cookie for removal, we cannot remove the
6052 * complete header, but we can remove the previous block itself.
6053 */
6054 preserve_hdr = 1;
6055 if (del_from != NULL) {
6056 int delta = del_hdr_value(req, &del_from, prev);
6057 val_end += delta;
6058 next += delta;
6059 hdr_end += delta;
6060 hdr_next += delta;
6061 cur_hdr->len += delta;
6062 http_msg_move_end(&txn->req, delta);
6063 prev = del_from;
6064 del_from = NULL;
6065 }
6066 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006067 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006068
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006069 /* if there are spaces around the equal sign, we need to
6070 * strip them otherwise we'll get trouble for cookie captures,
6071 * or even for rewrites. Since this happens extremely rarely,
6072 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006073 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006074 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6075 int stripped_before = 0;
6076 int stripped_after = 0;
6077
6078 if (att_end != equal) {
6079 stripped_before = buffer_replace2(req, att_end, equal, NULL, 0);
6080 equal += stripped_before;
6081 val_beg += stripped_before;
6082 }
6083
6084 if (val_beg > equal + 1) {
6085 stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0);
6086 val_beg += stripped_after;
6087 stripped_before += stripped_after;
6088 }
6089
6090 val_end += stripped_before;
6091 next += stripped_before;
6092 hdr_end += stripped_before;
6093 hdr_next += stripped_before;
6094 cur_hdr->len += stripped_before;
6095 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006096 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006097 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006098
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006099 /* First, let's see if we want to capture this cookie. We check
6100 * that we don't already have a client side cookie, because we
6101 * can only capture one. Also as an optimisation, we ignore
6102 * cookies shorter than the declared name.
6103 */
6104 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6105 (val_end - att_beg >= t->fe->capture_namelen) &&
6106 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6107 int log_len = val_end - att_beg;
6108
6109 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6110 Alert("HTTP logging : out of memory.\n");
6111 } else {
6112 if (log_len > t->fe->capture_len)
6113 log_len = t->fe->capture_len;
6114 memcpy(txn->cli_cookie, att_beg, log_len);
6115 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006116 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006118
Willy Tarreaubca99692010-10-06 19:25:55 +02006119 /* Persistence cookies in passive, rewrite or insert mode have the
6120 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006121 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006122 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006123 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006124 * For cookies in prefix mode, the form is :
6125 *
6126 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006127 */
6128 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6129 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6130 struct server *srv = t->be->srv;
6131 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006132
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006133 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6134 * have the server ID between val_beg and delim, and the original cookie between
6135 * delim+1 and val_end. Otherwise, delim==val_end :
6136 *
6137 * Cookie: NAME=SRV; # in all but prefix modes
6138 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6139 * | || || | |+-> next
6140 * | || || | +--> val_end
6141 * | || || +---------> delim
6142 * | || |+------------> val_beg
6143 * | || +-------------> att_end = equal
6144 * | |+-----------------> att_beg
6145 * | +------------------> prev
6146 * +-------------------------> hdr_beg
6147 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006148
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006149 if (t->be->options & PR_O_COOK_PFX) {
6150 for (delim = val_beg; delim < val_end; delim++)
6151 if (*delim == COOKIE_DELIM)
6152 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006153 } else {
6154 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006155 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006156 /* Now check if the cookie contains a date field, which would
6157 * appear after a vertical bar ('|') just after the server name
6158 * and before the delimiter.
6159 */
6160 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6161 if (vbar1) {
6162 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006163 * right is the last seen date. It is a base64 encoded
6164 * 30-bit value representing the UNIX date since the
6165 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006166 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006167 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006168 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006169 if (val_end - vbar1 >= 5) {
6170 val = b64tos30(vbar1);
6171 if (val > 0)
6172 txn->cookie_last_date = val << 2;
6173 }
6174 /* look for a second vertical bar */
6175 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6176 if (vbar1 && (val_end - vbar1 > 5)) {
6177 val = b64tos30(vbar1 + 1);
6178 if (val > 0)
6179 txn->cookie_first_date = val << 2;
6180 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006181 }
6182 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006183
Willy Tarreauf64d1412010-10-07 20:06:11 +02006184 /* if the cookie has an expiration date and the proxy wants to check
6185 * it, then we do that now. We first check if the cookie is too old,
6186 * then only if it has expired. We detect strict overflow because the
6187 * time resolution here is not great (4 seconds). Cookies with dates
6188 * in the future are ignored if their offset is beyond one day. This
6189 * allows an admin to fix timezone issues without expiring everyone
6190 * and at the same time avoids keeping unwanted side effects for too
6191 * long.
6192 */
6193 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006194 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6195 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006196 txn->flags &= ~TX_CK_MASK;
6197 txn->flags |= TX_CK_OLD;
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 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006203 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6204 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006205 txn->flags &= ~TX_CK_MASK;
6206 txn->flags |= TX_CK_EXPIRED;
6207 delim = val_beg; // let's pretend we have not found the cookie
6208 txn->cookie_first_date = 0;
6209 txn->cookie_last_date = 0;
6210 }
6211
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006212 /* Here, we'll look for the first running server which supports the cookie.
6213 * This allows to share a same cookie between several servers, for example
6214 * to dedicate backup servers to specific servers only.
6215 * However, to prevent clients from sticking to cookie-less backup server
6216 * when they have incidentely learned an empty cookie, we simply ignore
6217 * empty cookies and mark them as invalid.
6218 * The same behaviour is applied when persistence must be ignored.
6219 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006220 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006221 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006222
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006223 while (srv) {
6224 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6225 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6226 if ((srv->state & SRV_RUNNING) ||
6227 (t->be->options & PR_O_PERSIST) ||
6228 (t->flags & SN_FORCE_PRST)) {
6229 /* we found the server and we can use it */
6230 txn->flags &= ~TX_CK_MASK;
6231 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6232 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006233 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006234 break;
6235 } else {
6236 /* we found a server, but it's down,
6237 * mark it as such and go on in case
6238 * another one is available.
6239 */
6240 txn->flags &= ~TX_CK_MASK;
6241 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006242 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006243 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244 srv = srv->next;
6245 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006246
Willy Tarreauf64d1412010-10-07 20:06:11 +02006247 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006248 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006250 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6251 txn->flags |= TX_CK_UNUSED;
6252 else
6253 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006254 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006255
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 /* depending on the cookie mode, we may have to either :
6257 * - delete the complete cookie if we're in insert+indirect mode, so that
6258 * the server never sees it ;
6259 * - remove the server id from the cookie value, and tag the cookie as an
6260 * application cookie so that it does not get accidentely removed later,
6261 * if we're in cookie prefix mode
6262 */
6263 if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) {
6264 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006265
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006266 delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0);
6267 val_end += delta;
6268 next += delta;
6269 hdr_end += delta;
6270 hdr_next += delta;
6271 cur_hdr->len += delta;
6272 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006273
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006274 del_from = NULL;
6275 preserve_hdr = 1; /* we want to keep this cookie */
6276 }
6277 else if (del_from == NULL &&
6278 (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) {
6279 del_from = prev;
6280 }
6281 } else {
6282 /* This is not our cookie, so we must preserve it. But if we already
6283 * scheduled another cookie for removal, we cannot remove the
6284 * complete header, but we can remove the previous block itself.
6285 */
6286 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006287
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 if (del_from != NULL) {
6289 int delta = del_hdr_value(req, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006290 if (att_beg >= del_from)
6291 att_beg += delta;
6292 if (att_end >= del_from)
6293 att_end += delta;
6294 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006295 val_end += delta;
6296 next += delta;
6297 hdr_end += delta;
6298 hdr_next += delta;
6299 cur_hdr->len += delta;
6300 http_msg_move_end(&txn->req, delta);
6301 prev = del_from;
6302 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006303 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006304 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006305
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006306 /* Look for the appsession cookie unless persistence must be ignored */
6307 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6308 int cmp_len, value_len;
6309 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006310
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006311 if (t->be->options2 & PR_O2_AS_PFX) {
6312 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6313 value_begin = att_beg + t->be->appsession_name_len;
6314 value_len = val_end - att_beg - t->be->appsession_name_len;
6315 } else {
6316 cmp_len = att_end - att_beg;
6317 value_begin = val_beg;
6318 value_len = val_end - val_beg;
6319 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006320
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 /* let's see if the cookie is our appcookie */
6322 if (cmp_len == t->be->appsession_name_len &&
6323 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6324 manage_client_side_appsession(t, value_begin, value_len);
6325 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006326 }
6327
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006328 /* continue with next cookie on this header line */
6329 att_beg = next;
6330 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006331
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006332 /* There are no more cookies on this line.
6333 * We may still have one (or several) marked for deletion at the
6334 * end of the line. We must do this now in two ways :
6335 * - if some cookies must be preserved, we only delete from the
6336 * mark to the end of line ;
6337 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006341 if (preserve_hdr) {
6342 delta = del_hdr_value(req, &del_from, hdr_end);
6343 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006344 cur_hdr->len += delta;
6345 } else {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006346 delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006347
6348 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006349 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6350 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006351 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006352 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006354 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006355 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006356 }
6357
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006358 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006359 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006360 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006361}
6362
6363
Willy Tarreaua15645d2007-03-18 16:22:39 +01006364/* Iterate the same filter through all response headers contained in <rtr>.
6365 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6366 */
6367int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6368{
6369 char term;
6370 char *cur_ptr, *cur_end, *cur_next;
6371 int cur_idx, old_idx, last_hdr;
6372 struct http_txn *txn = &t->txn;
6373 struct hdr_idx_elem *cur_hdr;
6374 int len, delta;
6375
6376 last_hdr = 0;
6377
Willy Tarreau3a215be2012-03-09 21:39:51 +01006378 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006379 old_idx = 0;
6380
6381 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006382 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006383 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006384 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006385 (exp->action == ACT_ALLOW ||
6386 exp->action == ACT_DENY))
6387 return 0;
6388
6389 cur_idx = txn->hdr_idx.v[old_idx].next;
6390 if (!cur_idx)
6391 break;
6392
6393 cur_hdr = &txn->hdr_idx.v[cur_idx];
6394 cur_ptr = cur_next;
6395 cur_end = cur_ptr + cur_hdr->len;
6396 cur_next = cur_end + cur_hdr->cr + 1;
6397
6398 /* Now we have one header between cur_ptr and cur_end,
6399 * and the next header starts at cur_next.
6400 */
6401
6402 /* The annoying part is that pattern matching needs
6403 * that we modify the contents to null-terminate all
6404 * strings before testing them.
6405 */
6406
6407 term = *cur_end;
6408 *cur_end = '\0';
6409
6410 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6411 switch (exp->action) {
6412 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006413 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006414 last_hdr = 1;
6415 break;
6416
6417 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006418 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006419 last_hdr = 1;
6420 break;
6421
6422 case ACT_REPLACE:
6423 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6424 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6425 /* FIXME: if the user adds a newline in the replacement, the
6426 * index will not be recalculated for now, and the new line
6427 * will not be counted as a new header.
6428 */
6429
6430 cur_end += delta;
6431 cur_next += delta;
6432 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006433 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006434 break;
6435
6436 case ACT_REMOVE:
6437 delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0);
6438 cur_next += delta;
6439
Willy Tarreaufa355d42009-11-29 18:12:29 +01006440 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006441 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6442 txn->hdr_idx.used--;
6443 cur_hdr->len = 0;
6444 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006445 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006446 break;
6447
6448 }
6449 }
6450 if (cur_end)
6451 *cur_end = term; /* restore the string terminator */
6452
6453 /* keep the link from this header to next one in case of later
6454 * removal of next header.
6455 */
6456 old_idx = cur_idx;
6457 }
6458 return 0;
6459}
6460
6461
6462/* Apply the filter to the status line in the response buffer <rtr>.
6463 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6464 * or -1 if a replacement resulted in an invalid status line.
6465 */
6466int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp)
6467{
6468 char term;
6469 char *cur_ptr, *cur_end;
6470 int done;
6471 struct http_txn *txn = &t->txn;
6472 int len, delta;
6473
6474
Willy Tarreau3d300592007-03-18 18:34:41 +01006475 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006476 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006477 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006478 (exp->action == ACT_ALLOW ||
6479 exp->action == ACT_DENY))
6480 return 0;
6481 else if (exp->action == ACT_REMOVE)
6482 return 0;
6483
6484 done = 0;
6485
Willy Tarreau3a215be2012-03-09 21:39:51 +01006486 cur_ptr = rtr->p + txn->rsp.sol;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006487 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006488
6489 /* Now we have the status line between cur_ptr and cur_end */
6490
6491 /* The annoying part is that pattern matching needs
6492 * that we modify the contents to null-terminate all
6493 * strings before testing them.
6494 */
6495
6496 term = *cur_end;
6497 *cur_end = '\0';
6498
6499 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6500 switch (exp->action) {
6501 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006502 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006503 done = 1;
6504 break;
6505
6506 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006507 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006508 done = 1;
6509 break;
6510
6511 case ACT_REPLACE:
6512 *cur_end = term; /* restore the string terminator */
6513 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
6514 delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len);
6515 /* FIXME: if the user adds a newline in the replacement, the
6516 * index will not be recalculated for now, and the new line
6517 * will not be counted as a new header.
6518 */
6519
Willy Tarreaufa355d42009-11-29 18:12:29 +01006520 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006521 cur_end += delta;
Willy Tarreau62f791e2012-03-09 11:32:30 +01006522 cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data,
Willy Tarreau02785762007-04-03 14:45:44 +02006523 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006524 cur_ptr, cur_end + 1,
6525 NULL, NULL);
6526 if (unlikely(!cur_end))
6527 return -1;
6528
6529 /* we have a full respnse and we know that we have either a CR
6530 * or an LF at <ptr>.
6531 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01006532 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 +02006533 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006534 /* there is no point trying this regex on headers */
6535 return 1;
6536 }
6537 }
6538 *cur_end = term; /* restore the string terminator */
6539 return done;
6540}
6541
6542
6543
6544/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006545 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006546 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6547 * unparsable response.
6548 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006549int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006550{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006551 struct http_txn *txn = &s->txn;
6552 struct hdr_exp *exp;
6553
6554 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006555 int ret;
6556
6557 /*
6558 * The interleaving of transformations and verdicts
6559 * makes it difficult to decide to continue or stop
6560 * the evaluation.
6561 */
6562
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006563 if (txn->flags & TX_SVDENY)
6564 break;
6565
Willy Tarreau3d300592007-03-18 18:34:41 +01006566 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006567 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6568 exp->action == ACT_PASS)) {
6569 exp = exp->next;
6570 continue;
6571 }
6572
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006573 /* if this filter had a condition, evaluate it now and skip to
6574 * next filter if the condition does not match.
6575 */
6576 if (exp->cond) {
6577 ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR);
6578 ret = acl_pass(ret);
6579 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6580 ret = !ret;
6581 if (!ret)
6582 continue;
6583 }
6584
Willy Tarreaua15645d2007-03-18 16:22:39 +01006585 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006586 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006587 if (unlikely(ret < 0))
6588 return -1;
6589
6590 if (likely(ret == 0)) {
6591 /* The filter did not match the response, it can be
6592 * iterated through all headers.
6593 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006594 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006595 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006596 }
6597 return 0;
6598}
6599
6600
Willy Tarreaua15645d2007-03-18 16:22:39 +01006601/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006602 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006603 * desirable to call it only when needed. This function is also used when we
6604 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006605 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006606void manage_server_side_cookies(struct session *t, struct buffer *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006607{
6608 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006609 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006610 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006611 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006612 char *hdr_beg, *hdr_end, *hdr_next;
6613 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006614
Willy Tarreaua15645d2007-03-18 16:22:39 +01006615 /* Iterate through the headers.
6616 * we start with the start line.
6617 */
6618 old_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006619 hdr_next = res->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006620
6621 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6622 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006623 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006624
6625 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006626 hdr_beg = hdr_next;
6627 hdr_end = hdr_beg + cur_hdr->len;
6628 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006629
Willy Tarreau24581ba2010-08-31 22:39:35 +02006630 /* We have one full header between hdr_beg and hdr_end, and the
6631 * next header starts at hdr_next. We're only interested in
6632 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006633 */
6634
Willy Tarreau24581ba2010-08-31 22:39:35 +02006635 is_cookie2 = 0;
6636 prev = hdr_beg + 10;
6637 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006638 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006639 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6640 if (!val) {
6641 old_idx = cur_idx;
6642 continue;
6643 }
6644 is_cookie2 = 1;
6645 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006646 }
6647
Willy Tarreau24581ba2010-08-31 22:39:35 +02006648 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6649 * <prev> points to the colon.
6650 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006651 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006652
Willy Tarreau24581ba2010-08-31 22:39:35 +02006653 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6654 * check-cache is enabled) and we are not interested in checking
6655 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006656 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006657 if (t->be->cookie_name == NULL &&
6658 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006659 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006660 return;
6661
Willy Tarreau24581ba2010-08-31 22:39:35 +02006662 /* OK so now we know we have to process this response cookie.
6663 * The format of the Set-Cookie header is slightly different
6664 * from the format of the Cookie header in that it does not
6665 * support the comma as a cookie delimiter (thus the header
6666 * cannot be folded) because the Expires attribute described in
6667 * the original Netscape's spec may contain an unquoted date
6668 * with a comma inside. We have to live with this because
6669 * many browsers don't support Max-Age and some browsers don't
6670 * support quoted strings. However the Set-Cookie2 header is
6671 * clean.
6672 *
6673 * We have to keep multiple pointers in order to support cookie
6674 * removal at the beginning, middle or end of header without
6675 * corrupting the header (in case of set-cookie2). A special
6676 * pointer, <scav> points to the beginning of the set-cookie-av
6677 * fields after the first semi-colon. The <next> pointer points
6678 * either to the end of line (set-cookie) or next unquoted comma
6679 * (set-cookie2). All of these headers are valid :
6680 *
6681 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
6682 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6683 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
6684 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
6685 * | | | | | | | | | |
6686 * | | | | | | | | +-> next hdr_end <--+
6687 * | | | | | | | +------------> scav
6688 * | | | | | | +--------------> val_end
6689 * | | | | | +--------------------> val_beg
6690 * | | | | +----------------------> equal
6691 * | | | +------------------------> att_end
6692 * | | +----------------------------> att_beg
6693 * | +------------------------------> prev
6694 * +-----------------------------------------> hdr_beg
6695 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006696
Willy Tarreau24581ba2010-08-31 22:39:35 +02006697 for (; prev < hdr_end; prev = next) {
6698 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006699
Willy Tarreau24581ba2010-08-31 22:39:35 +02006700 /* find att_beg */
6701 att_beg = prev + 1;
6702 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6703 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006704
Willy Tarreau24581ba2010-08-31 22:39:35 +02006705 /* find att_end : this is the first character after the last non
6706 * space before the equal. It may be equal to hdr_end.
6707 */
6708 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006709
Willy Tarreau24581ba2010-08-31 22:39:35 +02006710 while (equal < hdr_end) {
6711 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
6712 break;
6713 if (http_is_spht[(unsigned char)*equal++])
6714 continue;
6715 att_end = equal;
6716 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006717
Willy Tarreau24581ba2010-08-31 22:39:35 +02006718 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6719 * is between <att_beg> and <equal>, both may be identical.
6720 */
6721
6722 /* look for end of cookie if there is an equal sign */
6723 if (equal < hdr_end && *equal == '=') {
6724 /* look for the beginning of the value */
6725 val_beg = equal + 1;
6726 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6727 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006728
Willy Tarreau24581ba2010-08-31 22:39:35 +02006729 /* find the end of the value, respecting quotes */
6730 next = find_cookie_value_end(val_beg, hdr_end);
6731
6732 /* make val_end point to the first white space or delimitor after the value */
6733 val_end = next;
6734 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6735 val_end--;
6736 } else {
6737 /* <equal> points to next comma, semi-colon or EOL */
6738 val_beg = val_end = next = equal;
6739 }
6740
6741 if (next < hdr_end) {
6742 /* Set-Cookie2 supports multiple cookies, and <next> points to
6743 * a colon or semi-colon before the end. So skip all attr-value
6744 * pairs and look for the next comma. For Set-Cookie, since
6745 * commas are permitted in values, skip to the end.
6746 */
6747 if (is_cookie2)
6748 next = find_hdr_value_end(next, hdr_end);
6749 else
6750 next = hdr_end;
6751 }
6752
6753 /* Now everything is as on the diagram above */
6754
6755 /* Ignore cookies with no equal sign */
6756 if (equal == val_end)
6757 continue;
6758
6759 /* If there are spaces around the equal sign, we need to
6760 * strip them otherwise we'll get trouble for cookie captures,
6761 * or even for rewrites. Since this happens extremely rarely,
6762 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006763 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006764 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6765 int stripped_before = 0;
6766 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767
Willy Tarreau24581ba2010-08-31 22:39:35 +02006768 if (att_end != equal) {
6769 stripped_before = buffer_replace2(res, att_end, equal, NULL, 0);
6770 equal += stripped_before;
6771 val_beg += stripped_before;
6772 }
6773
6774 if (val_beg > equal + 1) {
6775 stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0);
6776 val_beg += stripped_after;
6777 stripped_before += stripped_after;
6778 }
6779
6780 val_end += stripped_before;
6781 next += stripped_before;
6782 hdr_end += stripped_before;
6783 hdr_next += stripped_before;
6784 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02006785 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006786 }
6787
6788 /* First, let's see if we want to capture this cookie. We check
6789 * that we don't already have a server side cookie, because we
6790 * can only capture one. Also as an optimisation, we ignore
6791 * cookies shorter than the declared name.
6792 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006793 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01006794 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006795 (val_end - att_beg >= t->fe->capture_namelen) &&
6796 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6797 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02006798 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799 Alert("HTTP logging : out of memory.\n");
6800 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01006801 else {
6802 if (log_len > t->fe->capture_len)
6803 log_len = t->fe->capture_len;
6804 memcpy(txn->srv_cookie, att_beg, log_len);
6805 txn->srv_cookie[log_len] = 0;
6806 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006807 }
6808
Willy Tarreau827aee92011-03-10 16:55:02 +01006809 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006810 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02006811 if (!(t->flags & SN_IGNORE_PRST) &&
6812 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6813 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02006814 /* assume passive cookie by default */
6815 txn->flags &= ~TX_SCK_MASK;
6816 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006817
6818 /* If the cookie is in insert mode on a known server, we'll delete
6819 * this occurrence because we'll insert another one later.
6820 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02006821 * a direct access.
6822 */
Willy Tarreauba4c5be2010-10-23 12:46:42 +02006823 if (t->be->options2 & PR_O2_COOK_PSV) {
6824 /* The "preserve" flag was set, we don't want to touch the
6825 * server's cookie.
6826 */
6827 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006828 else if ((srv && (t->be->options & PR_O_COOK_INS)) ||
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006829 ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006830 /* this cookie must be deleted */
6831 if (*prev == ':' && next == hdr_end) {
6832 /* whole header */
6833 delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0);
6834 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6835 txn->hdr_idx.used--;
6836 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006837 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006838 hdr_next += delta;
6839 http_msg_move_end(&txn->rsp, delta);
6840 /* note: while both invalid now, <next> and <hdr_end>
6841 * are still equal, so the for() will stop as expected.
6842 */
6843 } else {
6844 /* just remove the value */
6845 int delta = del_hdr_value(res, &prev, next);
6846 next = prev;
6847 hdr_end += delta;
6848 hdr_next += delta;
6849 cur_hdr->len += delta;
6850 http_msg_move_end(&txn->rsp, delta);
6851 }
Willy Tarreauf1348312010-10-07 15:54:11 +02006852 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01006853 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006854 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006855 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006856 else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006857 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01006858 * with this server since we know it.
6859 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006860 delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006861 next += delta;
6862 hdr_end += delta;
6863 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006864 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006865 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006866
Willy Tarreauf1348312010-10-07 15:54:11 +02006867 txn->flags &= ~TX_SCK_MASK;
6868 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006869 }
Willy Tarreau827aee92011-03-10 16:55:02 +01006870 else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006871 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02006872 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873 */
Willy Tarreau827aee92011-03-10 16:55:02 +01006874 delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02006875 next += delta;
6876 hdr_end += delta;
6877 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006878 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006879 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880
Willy Tarreau827aee92011-03-10 16:55:02 +01006881 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02006882 txn->flags &= ~TX_SCK_MASK;
6883 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006884 }
6885 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006886 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
6887 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006888 int cmp_len, value_len;
6889 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006890
Cyril Bontéb21570a2009-11-29 20:04:48 +01006891 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006892 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6893 value_begin = att_beg + t->be->appsession_name_len;
6894 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01006895 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006896 cmp_len = att_end - att_beg;
6897 value_begin = val_beg;
6898 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006899 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006900
Cyril Bonté17530c32010-04-06 21:11:10 +02006901 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02006902 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
6903 /* free a possibly previously allocated memory */
6904 pool_free2(apools.sessid, txn->sessid);
6905
Cyril Bontéb21570a2009-11-29 20:04:48 +01006906 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006907 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01006908 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6909 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
6910 return;
6911 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006912 memcpy(txn->sessid, value_begin, value_len);
6913 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006914 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02006915 }
6916 /* that's done for this cookie, check the next one on the same
6917 * line when next != hdr_end (only if is_cookie2).
6918 */
6919 }
6920 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01006921 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006922 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006923
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006924 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006925 appsess *asession = NULL;
6926 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006927 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006928 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01006929 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006930 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
6931 Alert("Not enough Memory process_srv():asession:calloc().\n");
6932 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
6933 return;
6934 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006935 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
6936
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006937 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
6938 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
6939 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006940 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006941 return;
6942 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006943 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006944 asession->sessid[t->be->appsession_len] = 0;
6945
Willy Tarreau827aee92011-03-10 16:55:02 +01006946 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006947 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01006948 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006949 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01006950 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006951 return;
6952 }
6953 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01006954 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006955
6956 asession->request_count = 0;
6957 appsession_hash_insert(&(t->be->htbl_proxy), asession);
6958 }
6959
6960 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6961 asession->request_count++;
6962 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006963}
6964
6965
Willy Tarreaua15645d2007-03-18 16:22:39 +01006966/*
6967 * Check if response is cacheable or not. Updates t->flags.
6968 */
6969void check_response_for_cacheability(struct session *t, struct buffer *rtr)
6970{
6971 struct http_txn *txn = &t->txn;
6972 char *p1, *p2;
6973
6974 char *cur_ptr, *cur_end, *cur_next;
6975 int cur_idx;
6976
Willy Tarreau5df51872007-11-25 16:20:08 +01006977 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006978 return;
6979
6980 /* Iterate through the headers.
6981 * we start with the start line.
6982 */
6983 cur_idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01006984 cur_next = rtr->p + txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985
6986 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
6987 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006988 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006989
6990 cur_hdr = &txn->hdr_idx.v[cur_idx];
6991 cur_ptr = cur_next;
6992 cur_end = cur_ptr + cur_hdr->len;
6993 cur_next = cur_end + cur_hdr->cr + 1;
6994
6995 /* We have one full header between cur_ptr and cur_end, and the
6996 * next header starts at cur_next. We're only interested in
6997 * "Cookie:" headers.
6998 */
6999
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007000 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7001 if (val) {
7002 if ((cur_end - (cur_ptr + val) >= 8) &&
7003 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7004 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7005 return;
7006 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007007 }
7008
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007009 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7010 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011 continue;
7012
7013 /* OK, right now we know we have a cache-control header at cur_ptr */
7014
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007015 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007016
7017 if (p1 >= cur_end) /* no more info */
7018 continue;
7019
7020 /* p1 is at the beginning of the value */
7021 p2 = p1;
7022
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007023 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007024 p2++;
7025
7026 /* we have a complete value between p1 and p2 */
7027 if (p2 < cur_end && *p2 == '=') {
7028 /* we have something of the form no-cache="set-cookie" */
7029 if ((cur_end - p1 >= 21) &&
7030 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7031 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007032 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007033 continue;
7034 }
7035
7036 /* OK, so we know that either p2 points to the end of string or to a comma */
7037 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7038 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7039 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7040 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007041 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042 return;
7043 }
7044
7045 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007046 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007047 continue;
7048 }
7049 }
7050}
7051
7052
Willy Tarreau58f10d72006-12-04 02:26:12 +01007053/*
7054 * Try to retrieve a known appsession in the URI, then the associated server.
7055 * If the server is found, it's assigned to the session.
7056 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007057void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007058{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007059 char *end_params, *first_param, *cur_param, *next_param;
7060 char separator;
7061 int value_len;
7062
7063 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007064
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007065 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007066 (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 +01007067 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007068 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007069
Cyril Bontéb21570a2009-11-29 20:04:48 +01007070 first_param = NULL;
7071 switch (mode) {
7072 case PR_O2_AS_M_PP:
7073 first_param = memchr(begin, ';', len);
7074 break;
7075 case PR_O2_AS_M_QS:
7076 first_param = memchr(begin, '?', len);
7077 break;
7078 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007079
Cyril Bontéb21570a2009-11-29 20:04:48 +01007080 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007081 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007082 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007083
Cyril Bontéb21570a2009-11-29 20:04:48 +01007084 switch (mode) {
7085 case PR_O2_AS_M_PP:
7086 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7087 end_params = (char *) begin + len;
7088 }
7089 separator = ';';
7090 break;
7091 case PR_O2_AS_M_QS:
7092 end_params = (char *) begin + len;
7093 separator = '&';
7094 break;
7095 default:
7096 /* unknown mode, shouldn't happen */
7097 return;
7098 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007099
Cyril Bontéb21570a2009-11-29 20:04:48 +01007100 cur_param = next_param = end_params;
7101 while (cur_param > first_param) {
7102 cur_param--;
7103 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7104 /* let's see if this is the appsession parameter */
7105 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7106 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7107 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7108 /* Cool... it's the right one */
7109 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7110 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7111 if (value_len > 0) {
7112 manage_client_side_appsession(t, cur_param, value_len);
7113 }
7114 break;
7115 }
7116 next_param = cur_param;
7117 }
7118 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007119#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007120 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007121 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007122#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007123}
7124
Willy Tarreaub2513902006-12-17 14:52:38 +01007125/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007126 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007127 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007128 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007129 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007130 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007131 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007132 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007133 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007134int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007135{
7136 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007137 struct http_msg *msg = &txn->req;
7138 const char *uri = msg->buf->p + msg->sol + msg->sl.rq.u;
7139 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007140
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007141 if (!uri_auth)
7142 return 0;
7143
Cyril Bonté70be45d2010-10-12 00:14:35 +02007144 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007145 return 0;
7146
Willy Tarreau295a8372011-03-10 11:25:07 +01007147 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007148 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007149
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007150 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007151 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007152 return 0;
7153
Willy Tarreau3a215be2012-03-09 21:39:51 +01007154 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007155 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007156 return 0;
7157
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007158 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007159 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007160 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007161 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007162 break;
7163 }
7164 h++;
7165 }
7166
7167 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007168 h = uri + uri_auth->uri_len;
7169 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007170 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007171 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007172 break;
7173 }
7174 h++;
7175 }
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 - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007180 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007181 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007182 break;
7183 }
7184 h++;
7185 }
7186
Willy Tarreau3a215be2012-03-09 21:39:51 +01007187 h = uri + uri_auth->uri_len;
7188 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007189 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007190 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007191 h += 4;
Cyril Bonté19979e12012-04-04 12:57:21 +02007192 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7193 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7194 si->applet.ctx.stats.st_code = i;
7195 break;
7196 }
7197 }
7198 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007199 break;
7200 }
7201 h++;
7202 }
7203
Willy Tarreau295a8372011-03-10 11:25:07 +01007204 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007205
Willy Tarreaub2513902006-12-17 14:52:38 +01007206 return 1;
7207}
7208
Willy Tarreau4076a152009-04-02 15:18:36 +02007209/*
7210 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau962c3f42010-01-10 00:15:35 +01007211 * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007212 * assume that msg->sol = msg->buf->p + msg->som. Also, while HTTP requests
7213 * or response messages cannot wrap, this function may also be used with chunks
7214 * which may wrap.
Willy Tarreau4076a152009-04-02 15:18:36 +02007215 */
7216void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007217 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007218 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007219{
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007220 struct buffer *buf = msg->buf;
7221
Willy Tarreauea1175a2012-03-05 15:52:30 +01007222 if (buffer_wrap_add(buf, buf->p + buf->i) <= (buf->p + msg->som)) { /* message wraps */
7223 int len1 = buf->size - msg->som - (buf->p - buf->data);
7224 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som) + buf->size;
7225 memcpy(es->buf, buf->p + msg->som, MIN(len1, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007226 if (es->len > len1 && len1 < sizeof(es->buf))
7227 memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1);
7228 }
7229 else {
Willy Tarreauea1175a2012-03-05 15:52:30 +01007230 es->len = buffer_wrap_add(buf, buf->p + buf->i) - (buf->p + msg->som);
7231 memcpy(es->buf, buf->p + msg->som, MIN(es->len, sizeof(es->buf)));
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007232 }
7233
Willy Tarreau4076a152009-04-02 15:18:36 +02007234 if (msg->err_pos >= 0)
Willy Tarreauea1175a2012-03-05 15:52:30 +01007235 es->pos = msg->err_pos - msg->som - (buf->p - buf->data);
7236 else if (buffer_wrap_add(buf, buf->p + msg->next) >= (buf->p + msg->som))
7237 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007238 else
Willy Tarreauea1175a2012-03-05 15:52:30 +01007239 es->pos = buffer_wrap_add(buf, buf->p + msg->next) - (buf->p + msg->som) + buf->size;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007240
Willy Tarreau4076a152009-04-02 15:18:36 +02007241 es->when = date; // user-visible date
7242 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007243 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007244 es->oe = other_end;
Willy Tarreau6471afb2011-09-23 10:54:59 +02007245 es->src = s->req->prod->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007246 es->state = state;
7247 es->flags = buf->flags;
Willy Tarreau10479e42010-12-12 14:00:34 +01007248 es->ev_id = error_snapshot_id++;
Willy Tarreau4076a152009-04-02 15:18:36 +02007249}
Willy Tarreaub2513902006-12-17 14:52:38 +01007250
Willy Tarreau294c4732011-12-16 21:35:50 +01007251/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7252 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7253 * performed over the whole headers. Otherwise it must contain a valid header
7254 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7255 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7256 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7257 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7258 * -1.
7259 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007260 */
Willy Tarreau294c4732011-12-16 21:35:50 +01007261unsigned int http_get_hdr(struct http_msg *msg, const char *hname, int hlen,
7262 struct hdr_idx *idx, int occ,
7263 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007264{
Willy Tarreau294c4732011-12-16 21:35:50 +01007265 struct hdr_ctx local_ctx;
7266 char *ptr_hist[MAX_HDR_HISTORY];
7267 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007268 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007269 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007270
Willy Tarreau294c4732011-12-16 21:35:50 +01007271 if (!ctx) {
7272 local_ctx.idx = 0;
7273 ctx = &local_ctx;
7274 }
7275
Willy Tarreaubce70882009-09-07 11:51:47 +02007276 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007277 /* search from the beginning */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007278 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007279 occ--;
7280 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007281 *vptr = ctx->line + ctx->val;
7282 *vlen = ctx->vlen;
7283 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007284 }
7285 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007286 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007287 }
7288
7289 /* negative occurrence, we scan all the list then walk back */
7290 if (-occ > MAX_HDR_HISTORY)
7291 return 0;
7292
Willy Tarreau294c4732011-12-16 21:35:50 +01007293 found = hist_ptr = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007294 while (http_find_header2(hname, hlen, msg->buf->p + msg->sol, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007295 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7296 len_hist[hist_ptr] = ctx->vlen;
7297 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007298 hist_ptr = 0;
7299 found++;
7300 }
7301 if (-occ > found)
7302 return 0;
7303 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7304 * find occurrence -occ, so we have to check [hist_ptr+occ].
7305 */
7306 hist_ptr += occ;
7307 if (hist_ptr >= MAX_HDR_HISTORY)
7308 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007309 *vptr = ptr_hist[hist_ptr];
7310 *vlen = len_hist[hist_ptr];
7311 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007312}
7313
Willy Tarreaubaaee002006-06-26 02:48:02 +02007314/*
Willy Tarreau58f10d72006-12-04 02:26:12 +01007315 * Print a debug line with a header
7316 */
7317void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7318{
7319 int len, max;
7320 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02007321 dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007322 max = end - start;
7323 UBOUND(max, sizeof(trash) - len - 1);
7324 len += strlcpy2(trash + len, start, max + 1);
7325 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007326 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007327}
7328
Willy Tarreau0937bc42009-12-22 15:03:09 +01007329/*
7330 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7331 * the required fields are properly allocated and that we only need to (re)init
7332 * them. This should be used before processing any new request.
7333 */
7334void http_init_txn(struct session *s)
7335{
7336 struct http_txn *txn = &s->txn;
7337 struct proxy *fe = s->fe;
7338
7339 txn->flags = 0;
7340 txn->status = -1;
7341
William Lallemand5f232402012-04-05 18:02:55 +02007342 global.req_count++;
7343
Willy Tarreauf64d1412010-10-07 20:06:11 +02007344 txn->cookie_first_date = 0;
7345 txn->cookie_last_date = 0;
7346
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007347 txn->req.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007348 txn->req.sol = txn->req.eol = txn->req.som = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007349 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007350 txn->rsp.flags = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007351 txn->rsp.sol = txn->rsp.eol = txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007352 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007353 txn->req.chunk_len = 0LL;
7354 txn->req.body_len = 0LL;
7355 txn->rsp.chunk_len = 0LL;
7356 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007357 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7358 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau62f791e2012-03-09 11:32:30 +01007359 txn->req.buf = s->req;
7360 txn->rsp.buf = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007361
7362 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007363
7364 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7365 if (fe->options2 & PR_O2_REQBUG_OK)
7366 txn->req.err_pos = -1; /* let buggy requests pass */
7367
Willy Tarreau46023632010-01-07 22:51:47 +01007368 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007369 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7370
Willy Tarreau46023632010-01-07 22:51:47 +01007371 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007372 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7373
7374 if (txn->hdr_idx.v)
7375 hdr_idx_init(&txn->hdr_idx);
7376}
7377
7378/* to be used at the end of a transaction */
7379void http_end_txn(struct session *s)
7380{
7381 struct http_txn *txn = &s->txn;
7382
7383 /* these ones will have been dynamically allocated */
7384 pool_free2(pool2_requri, txn->uri);
7385 pool_free2(pool2_capture, txn->cli_cookie);
7386 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007387 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007388 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007389
William Lallemanda73203e2012-03-12 12:48:57 +01007390 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007391 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007392 txn->uri = NULL;
7393 txn->srv_cookie = NULL;
7394 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007395
7396 if (txn->req.cap) {
7397 struct cap_hdr *h;
7398 for (h = s->fe->req_cap; h; h = h->next)
7399 pool_free2(h->pool, txn->req.cap[h->index]);
7400 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7401 }
7402
7403 if (txn->rsp.cap) {
7404 struct cap_hdr *h;
7405 for (h = s->fe->rsp_cap; h; h = h->next)
7406 pool_free2(h->pool, txn->rsp.cap[h->index]);
7407 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7408 }
7409
Willy Tarreau0937bc42009-12-22 15:03:09 +01007410}
7411
7412/* to be used at the end of a transaction to prepare a new one */
7413void http_reset_txn(struct session *s)
7414{
7415 http_end_txn(s);
7416 http_init_txn(s);
7417
7418 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007419 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007420 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007421 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007422 /* re-init store persistence */
7423 s->store_count = 0;
7424
Willy Tarreau0937bc42009-12-22 15:03:09 +01007425 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007426
7427 s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */
7428
Willy Tarreau739cfba2010-01-25 23:11:14 +01007429 /* We must trim any excess data from the response buffer, because we
7430 * may have blocked an invalid response from a server that we don't
7431 * want to accidentely forward once we disable the analysers, nor do
7432 * we want those data to come along with next response. A typical
7433 * example of such data would be from a buggy server responding to
7434 * a HEAD with some data, or sending more than the advertised
7435 * content-length.
7436 */
Willy Tarreau363a5bb2012-03-02 20:14:45 +01007437 if (unlikely(s->rep->i))
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01007438 s->rep->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007439
Willy Tarreau0937bc42009-12-22 15:03:09 +01007440 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007441 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007442
Willy Tarreaud04e8582010-05-31 12:31:35 +02007443 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007444 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007445
7446 s->req->rex = TICK_ETERNITY;
7447 s->req->wex = TICK_ETERNITY;
7448 s->req->analyse_exp = TICK_ETERNITY;
7449 s->rep->rex = TICK_ETERNITY;
7450 s->rep->wex = TICK_ETERNITY;
7451 s->rep->analyse_exp = TICK_ETERNITY;
7452}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007453
Willy Tarreauff011f22011-01-06 17:51:27 +01007454void free_http_req_rules(struct list *r) {
7455 struct http_req_rule *tr, *pr;
7456
7457 list_for_each_entry_safe(pr, tr, r, list) {
7458 LIST_DEL(&pr->list);
7459 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7460 free(pr->http_auth.realm);
7461
7462 free(pr);
7463 }
7464}
7465
7466struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7467{
7468 struct http_req_rule *rule;
7469 int cur_arg;
7470
7471 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7472 if (!rule) {
7473 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7474 return NULL;
7475 }
7476
7477 if (!*args[0]) {
7478 goto req_error_parsing;
7479 } else if (!strcmp(args[0], "allow")) {
7480 rule->action = HTTP_REQ_ACT_ALLOW;
7481 cur_arg = 1;
7482 } else if (!strcmp(args[0], "deny")) {
7483 rule->action = HTTP_REQ_ACT_DENY;
7484 cur_arg = 1;
7485 } else if (!strcmp(args[0], "auth")) {
7486 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7487 cur_arg = 1;
7488
7489 while(*args[cur_arg]) {
7490 if (!strcmp(args[cur_arg], "realm")) {
7491 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7492 cur_arg+=2;
7493 continue;
7494 } else
7495 break;
7496 }
7497 } else {
7498req_error_parsing:
7499 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7500 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7501 return NULL;
7502 }
7503
7504 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7505 struct acl_cond *cond;
7506
7507 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) {
7508 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n",
7509 file, linenum, args[0]);
7510 return NULL;
7511 }
7512 rule->cond = cond;
7513 }
7514 else if (*args[cur_arg]) {
7515 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7516 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7517 file, linenum, args[0], args[cur_arg]);
7518 return NULL;
7519 }
7520
7521 return rule;
7522}
7523
Willy Tarreau8797c062007-05-07 00:55:35 +02007524/************************************************************************/
7525/* The code below is dedicated to ACL parsing and matching */
7526/************************************************************************/
7527
7528
7529
7530
7531/* 1. Check on METHOD
7532 * We use the pre-parsed method if it is known, and store its number as an
7533 * integer. If it is unknown, we use the pointer and the length.
7534 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007535static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007536{
7537 int len, meth;
7538
Willy Tarreauae8b7962007-06-09 23:10:04 +02007539 len = strlen(*text);
7540 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007541
7542 pattern->val.i = meth;
7543 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007544 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007545 if (!pattern->ptr.str)
7546 return 0;
7547 pattern->len = len;
7548 }
7549 return 1;
7550}
7551
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007552/* This function fetches the method of current HTTP request and stores
7553 * it in the global pattern struct as a chunk. There are two possibilities :
7554 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7555 * in <len> and <ptr> is NULL ;
7556 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7557 * <len> to its length.
7558 * This is intended to be used with acl_match_meth() only.
7559 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007560static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007561acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir,
7562 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007563{
7564 int meth;
7565 struct http_txn *txn = l7;
7566
Willy Tarreaub6866442008-07-14 23:54:42 +02007567 if (!txn)
7568 return 0;
7569
Willy Tarreau655dce92009-11-08 13:10:58 +01007570 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007571 return 0;
7572
Willy Tarreau8797c062007-05-07 00:55:35 +02007573 meth = txn->meth;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007574 temp_pattern.data.str.len = meth;
7575 temp_pattern.data.str.str = NULL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007576 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007577 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7578 /* ensure the indexes are not affected */
7579 return 0;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007580 temp_pattern.data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007581 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007582 }
7583 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7584 return 1;
7585}
7586
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007587/* See above how the method is stored in the global pattern */
Willy Tarreau8797c062007-05-07 00:55:35 +02007588static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern)
7589{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007590 int icase;
7591
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007592
7593 if (temp_pattern.data.str.str == NULL) {
7594 /* well-known method */
7595 if (temp_pattern.data.str.len == pattern->val.i)
7596 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02007597 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007598 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007599
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007600 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
7601 if (pattern->val.i != HTTP_METH_OTHER)
7602 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02007603
7604 /* Other method, we must compare the strings */
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007605 if (pattern->len != temp_pattern.data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02007606 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02007607
7608 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007609 if ((icase && strncasecmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0) ||
7610 (!icase && strncmp(pattern->ptr.str, temp_pattern.data.str.str, temp_pattern.data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02007611 return ACL_PAT_FAIL;
7612 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02007613}
7614
7615/* 2. Check on Request/Status Version
7616 * We simply compare strings here.
7617 */
Willy Tarreauae8b7962007-06-09 23:10:04 +02007618static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque)
Willy Tarreau8797c062007-05-07 00:55:35 +02007619{
Willy Tarreauae8b7962007-06-09 23:10:04 +02007620 pattern->ptr.str = strdup(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007621 if (!pattern->ptr.str)
7622 return 0;
Willy Tarreauae8b7962007-06-09 23:10:04 +02007623 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02007624 return 1;
7625}
7626
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007627static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007628acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir,
7629 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007630{
7631 struct http_txn *txn = l7;
7632 char *ptr;
7633 int len;
7634
Willy Tarreaub6866442008-07-14 23:54:42 +02007635 if (!txn)
7636 return 0;
7637
Willy Tarreau655dce92009-11-08 13:10:58 +01007638 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007639 return 0;
7640
Willy Tarreau8797c062007-05-07 00:55:35 +02007641 len = txn->req.sl.rq.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007642 ptr = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02007643
7644 while ((len-- > 0) && (*ptr++ != '/'));
7645 if (len <= 0)
7646 return 0;
7647
Willy Tarreau664092c2011-12-16 19:11:42 +01007648 temp_pattern.data.str.str = ptr;
7649 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007650
7651 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7652 return 1;
7653}
7654
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007655static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007656acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir,
7657 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007658{
7659 struct http_txn *txn = l7;
7660 char *ptr;
7661 int len;
7662
Willy Tarreaub6866442008-07-14 23:54:42 +02007663 if (!txn)
7664 return 0;
7665
Willy Tarreau655dce92009-11-08 13:10:58 +01007666 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007667 return 0;
7668
Willy Tarreau8797c062007-05-07 00:55:35 +02007669 len = txn->rsp.sl.st.v_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007670 ptr = txn->rsp.buf->p + txn->rsp.sol;
Willy Tarreau8797c062007-05-07 00:55:35 +02007671
7672 while ((len-- > 0) && (*ptr++ != '/'));
7673 if (len <= 0)
7674 return 0;
7675
Willy Tarreau664092c2011-12-16 19:11:42 +01007676 temp_pattern.data.str.str = ptr;
7677 temp_pattern.data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02007678
7679 test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST;
7680 return 1;
7681}
7682
7683/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007684static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007685acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir,
7686 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007687{
7688 struct http_txn *txn = l7;
7689 char *ptr;
7690 int len;
7691
Willy Tarreaub6866442008-07-14 23:54:42 +02007692 if (!txn)
7693 return 0;
7694
Willy Tarreau655dce92009-11-08 13:10:58 +01007695 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007696 return 0;
7697
Willy Tarreau8797c062007-05-07 00:55:35 +02007698 len = txn->rsp.sl.st.c_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007699 ptr = txn->rsp.buf->p + txn->rsp.sol + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02007700
Willy Tarreaua5e37562011-12-16 17:06:15 +01007701 temp_pattern.data.integer = __strl2ui(ptr, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007702 test->flags = ACL_TEST_F_VOL_1ST;
7703 return 1;
7704}
7705
7706/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007707static int
Willy Tarreau97be1452007-06-10 11:47:14 +02007708acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir,
7709 struct acl_expr *expr, struct acl_test *test)
Willy Tarreau8797c062007-05-07 00:55:35 +02007710{
7711 struct http_txn *txn = l7;
7712
Willy Tarreaub6866442008-07-14 23:54:42 +02007713 if (!txn)
7714 return 0;
7715
Willy Tarreau655dce92009-11-08 13:10:58 +01007716 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007717 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007718
Willy Tarreauc11416f2007-06-17 16:58:38 +02007719 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7720 /* ensure the indexes are not affected */
7721 return 0;
7722
Willy Tarreau664092c2011-12-16 19:11:42 +01007723 temp_pattern.data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007724 temp_pattern.data.str.str = txn->req.buf->p + txn->req.sol + txn->req.sl.rq.u;
Willy Tarreau8797c062007-05-07 00:55:35 +02007725
Willy Tarreauf3d25982007-05-08 22:45:09 +02007726 /* we do not need to set READ_ONLY because the data is in a buffer */
7727 test->flags = ACL_TEST_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02007728 return 1;
7729}
7730
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007731static int
7732acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir,
7733 struct acl_expr *expr, struct acl_test *test)
7734{
7735 struct http_txn *txn = l7;
7736
Willy Tarreaub6866442008-07-14 23:54:42 +02007737 if (!txn)
7738 return 0;
7739
Willy Tarreau655dce92009-11-08 13:10:58 +01007740 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007741 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007742
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007743 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7744 /* ensure the indexes are not affected */
7745 return 0;
7746
7747 /* Parse HTTP request */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007748 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 +01007749 if (((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_family != AF_INET)
7750 return 0;
7751 temp_pattern.type = PATTERN_TYPE_IP;
7752 temp_pattern.data.ip = ((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007753
7754 /*
7755 * If we are parsing url in frontend space, we prepare backend stage
7756 * to not parse again the same url ! optimization lazyness...
7757 */
7758 if (px->options & PR_O_HTTP_PROXY)
7759 l4->flags |= SN_ADDR_SET;
7760
Willy Tarreauf4362b32011-12-16 17:49:52 +01007761 test->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007762 return 1;
7763}
7764
7765static int
7766acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir,
7767 struct acl_expr *expr, struct acl_test *test)
7768{
7769 struct http_txn *txn = l7;
7770
Willy Tarreaub6866442008-07-14 23:54:42 +02007771 if (!txn)
7772 return 0;
7773
Willy Tarreau655dce92009-11-08 13:10:58 +01007774 if (txn->req.msg_state < HTTP_MSG_BODY)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007775 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007776
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007777 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7778 /* ensure the indexes are not affected */
7779 return 0;
7780
7781 /* Same optimization as url_ip */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007782 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 +01007783 temp_pattern.data.integer = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01007784
7785 if (px->options & PR_O_HTTP_PROXY)
7786 l4->flags |= SN_ADDR_SET;
7787
7788 test->flags = ACL_TEST_F_READ_ONLY;
7789 return 1;
7790}
7791
Willy Tarreauc11416f2007-06-17 16:58:38 +02007792/* 5. Check on HTTP header. A pointer to the beginning of the value is returned.
7793 * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr().
7794 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02007795static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007796acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007797 struct acl_expr *expr, struct acl_test *test)
7798{
7799 struct http_txn *txn = l7;
7800 struct hdr_idx *idx = &txn->hdr_idx;
7801 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007802
Willy Tarreaub6866442008-07-14 23:54:42 +02007803 if (!txn)
7804 return 0;
7805
Willy Tarreau33a7e692007-06-10 19:45:56 +02007806 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7807 /* search for header from the beginning */
7808 ctx->idx = 0;
7809
Willy Tarreau33a7e692007-06-10 19:45:56 +02007810 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7811 test->flags |= ACL_TEST_F_FETCH_MORE;
7812 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreau664092c2011-12-16 19:11:42 +01007813 temp_pattern.data.str.str = (char *)ctx->line + ctx->val;
7814 temp_pattern.data.str.len = ctx->vlen;
7815
Willy Tarreau33a7e692007-06-10 19:45:56 +02007816 return 1;
7817 }
7818
7819 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7820 test->flags |= ACL_TEST_F_VOL_HDR;
7821 return 0;
7822}
7823
Willy Tarreau33a7e692007-06-10 19:45:56 +02007824static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007825acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir,
7826 struct acl_expr *expr, struct acl_test *test)
7827{
7828 struct http_txn *txn = l7;
7829
Willy Tarreaub6866442008-07-14 23:54:42 +02007830 if (!txn)
7831 return 0;
7832
Willy Tarreau655dce92009-11-08 13:10:58 +01007833 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007834 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007835
Willy Tarreauc11416f2007-06-17 16:58:38 +02007836 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7837 /* ensure the indexes are not affected */
7838 return 0;
7839
Willy Tarreau3a215be2012-03-09 21:39:51 +01007840 return acl_fetch_hdr(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007841}
7842
7843static int
7844acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir,
7845 struct acl_expr *expr, struct acl_test *test)
7846{
7847 struct http_txn *txn = l7;
7848
Willy Tarreaub6866442008-07-14 23:54:42 +02007849 if (!txn)
7850 return 0;
7851
Willy Tarreau655dce92009-11-08 13:10:58 +01007852 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007853 return 0;
7854
Willy Tarreau3a215be2012-03-09 21:39:51 +01007855 return acl_fetch_hdr(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007856}
7857
7858/* 6. Check on HTTP header count. The number of occurrences is returned.
7859 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7860 */
7861static int
7862acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007863 struct acl_expr *expr, struct acl_test *test)
7864{
7865 struct http_txn *txn = l7;
7866 struct hdr_idx *idx = &txn->hdr_idx;
7867 struct hdr_ctx ctx;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007868 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02007869
Willy Tarreaub6866442008-07-14 23:54:42 +02007870 if (!txn)
7871 return 0;
7872
Willy Tarreau33a7e692007-06-10 19:45:56 +02007873 ctx.idx = 0;
7874 cnt = 0;
7875 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx))
7876 cnt++;
7877
Willy Tarreaua5e37562011-12-16 17:06:15 +01007878 temp_pattern.data.integer = cnt;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007879 test->flags = ACL_TEST_F_VOL_HDR;
7880 return 1;
7881}
7882
Willy Tarreauc11416f2007-06-17 16:58:38 +02007883static int
7884acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7885 struct acl_expr *expr, struct acl_test *test)
7886{
7887 struct http_txn *txn = l7;
7888
Willy Tarreaub6866442008-07-14 23:54:42 +02007889 if (!txn)
7890 return 0;
7891
Willy Tarreau655dce92009-11-08 13:10:58 +01007892 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007893 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007894
Willy Tarreauc11416f2007-06-17 16:58:38 +02007895 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7896 /* ensure the indexes are not affected */
7897 return 0;
7898
Willy Tarreau3a215be2012-03-09 21:39:51 +01007899 return acl_fetch_hdr_cnt(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007900}
7901
7902static int
7903acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
7904 struct acl_expr *expr, struct acl_test *test)
7905{
7906 struct http_txn *txn = l7;
7907
Willy Tarreaub6866442008-07-14 23:54:42 +02007908 if (!txn)
7909 return 0;
7910
Willy Tarreau655dce92009-11-08 13:10:58 +01007911 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007912 return 0;
7913
Willy Tarreau3a215be2012-03-09 21:39:51 +01007914 return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007915}
7916
Willy Tarreau33a7e692007-06-10 19:45:56 +02007917/* 7. Check on HTTP header's integer value. The integer value is returned.
7918 * FIXME: the type is 'int', it may not be appropriate for everything.
Willy Tarreauc11416f2007-06-17 16:58:38 +02007919 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
Willy Tarreau33a7e692007-06-10 19:45:56 +02007920 */
7921static int
Willy Tarreauc11416f2007-06-17 16:58:38 +02007922acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol,
Willy Tarreau33a7e692007-06-10 19:45:56 +02007923 struct acl_expr *expr, struct acl_test *test)
7924{
7925 struct http_txn *txn = l7;
7926 struct hdr_idx *idx = &txn->hdr_idx;
7927 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
Willy Tarreau33a7e692007-06-10 19:45:56 +02007928
Willy Tarreaub6866442008-07-14 23:54:42 +02007929 if (!txn)
7930 return 0;
7931
Willy Tarreau33a7e692007-06-10 19:45:56 +02007932 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7933 /* search for header from the beginning */
7934 ctx->idx = 0;
7935
Willy Tarreau33a7e692007-06-10 19:45:56 +02007936 if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
7937 test->flags |= ACL_TEST_F_FETCH_MORE;
7938 test->flags |= ACL_TEST_F_VOL_HDR;
Willy Tarreaua5e37562011-12-16 17:06:15 +01007939 temp_pattern.data.integer = strl2ic((char *)ctx->line + ctx->val, ctx->vlen);
Willy Tarreau33a7e692007-06-10 19:45:56 +02007940 return 1;
7941 }
7942
7943 test->flags &= ~ACL_TEST_F_FETCH_MORE;
7944 test->flags |= ACL_TEST_F_VOL_HDR;
7945 return 0;
7946}
7947
Willy Tarreauc11416f2007-06-17 16:58:38 +02007948static int
7949acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7950 struct acl_expr *expr, struct acl_test *test)
7951{
7952 struct http_txn *txn = l7;
7953
Willy Tarreaub6866442008-07-14 23:54:42 +02007954 if (!txn)
7955 return 0;
7956
Willy Tarreau655dce92009-11-08 13:10:58 +01007957 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007958 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02007959
Willy Tarreauc11416f2007-06-17 16:58:38 +02007960 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
7961 /* ensure the indexes are not affected */
7962 return 0;
7963
Willy Tarreau3a215be2012-03-09 21:39:51 +01007964 return acl_fetch_hdr_val(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007965}
7966
7967static int
7968acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir,
7969 struct acl_expr *expr, struct acl_test *test)
7970{
7971 struct http_txn *txn = l7;
7972
Willy Tarreaub6866442008-07-14 23:54:42 +02007973 if (!txn)
7974 return 0;
7975
Willy Tarreau655dce92009-11-08 13:10:58 +01007976 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02007977 return 0;
7978
Willy Tarreau3a215be2012-03-09 21:39:51 +01007979 return acl_fetch_hdr_val(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreauc11416f2007-06-17 16:58:38 +02007980}
7981
Willy Tarreau106f9792009-09-19 07:54:16 +02007982/* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned.
7983 * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*.
7984 */
7985static int
7986acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol,
7987 struct acl_expr *expr, struct acl_test *test)
7988{
7989 struct http_txn *txn = l7;
7990 struct hdr_idx *idx = &txn->hdr_idx;
7991 struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a;
7992
7993 if (!txn)
7994 return 0;
7995
7996 if (!(test->flags & ACL_TEST_F_FETCH_MORE))
7997 /* search for header from the beginning */
7998 ctx->idx = 0;
7999
Willy Tarreauf4362b32011-12-16 17:49:52 +01008000 while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) {
Willy Tarreau106f9792009-09-19 07:54:16 +02008001 test->flags |= ACL_TEST_F_FETCH_MORE;
8002 test->flags |= ACL_TEST_F_VOL_HDR;
8003 /* Same optimization as url_ip */
Willy Tarreauf4362b32011-12-16 17:49:52 +01008004 temp_pattern.type = PATTERN_TYPE_IP;
8005 if (url2ipv4((char *)ctx->line + ctx->val, &temp_pattern.data.ip))
8006 return 1;
8007 /* Dods not look like an IP address, let's fetch next one */
Willy Tarreau106f9792009-09-19 07:54:16 +02008008 }
8009
8010 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8011 test->flags |= ACL_TEST_F_VOL_HDR;
8012 return 0;
8013}
8014
8015static int
8016acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8017 struct acl_expr *expr, struct acl_test *test)
8018{
8019 struct http_txn *txn = l7;
8020
8021 if (!txn)
8022 return 0;
8023
Willy Tarreau655dce92009-11-08 13:10:58 +01008024 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008025 return 0;
8026
8027 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8028 /* ensure the indexes are not affected */
8029 return 0;
8030
Willy Tarreau3a215be2012-03-09 21:39:51 +01008031 return acl_fetch_hdr_ip(px, l4, txn, txn->req.buf->p + txn->req.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008032}
8033
8034static int
8035acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir,
8036 struct acl_expr *expr, struct acl_test *test)
8037{
8038 struct http_txn *txn = l7;
8039
8040 if (!txn)
8041 return 0;
8042
Willy Tarreau655dce92009-11-08 13:10:58 +01008043 if (txn->rsp.msg_state < HTTP_MSG_BODY)
Willy Tarreau106f9792009-09-19 07:54:16 +02008044 return 0;
8045
Willy Tarreau3a215be2012-03-09 21:39:51 +01008046 return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.buf->p + txn->rsp.sol, expr, test);
Willy Tarreau106f9792009-09-19 07:54:16 +02008047}
8048
Willy Tarreau737b0c12007-06-10 21:28:46 +02008049/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8050 * the first '/' after the possible hostname, and ends before the possible '?'.
8051 */
8052static int
8053acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir,
8054 struct acl_expr *expr, struct acl_test *test)
8055{
8056 struct http_txn *txn = l7;
8057 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008058
Willy Tarreaub6866442008-07-14 23:54:42 +02008059 if (!txn)
8060 return 0;
8061
Willy Tarreau655dce92009-11-08 13:10:58 +01008062 if (txn->req.msg_state < HTTP_MSG_BODY)
Willy Tarreauc11416f2007-06-17 16:58:38 +02008063 return 0;
Willy Tarreaub6866442008-07-14 23:54:42 +02008064
Willy Tarreauc11416f2007-06-17 16:58:38 +02008065 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8066 /* ensure the indexes are not affected */
8067 return 0;
8068
Willy Tarreau3a215be2012-03-09 21:39:51 +01008069 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 +01008070 ptr = http_get_path(txn);
8071 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008072 return 0;
8073
8074 /* OK, we got the '/' ! */
Willy Tarreau664092c2011-12-16 19:11:42 +01008075 temp_pattern.data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008076
8077 while (ptr < end && *ptr != '?')
8078 ptr++;
8079
Willy Tarreau664092c2011-12-16 19:11:42 +01008080 temp_pattern.data.str.len = ptr - temp_pattern.data.str.str;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008081
8082 /* we do not need to set READ_ONLY because the data is in a buffer */
8083 test->flags = ACL_TEST_F_VOL_1ST;
8084 return 1;
8085}
8086
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008087static int
8088acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir,
8089 struct acl_expr *expr, struct acl_test *test)
8090{
8091 struct buffer *req = s->req;
8092 struct http_txn *txn = &s->txn;
8093 struct http_msg *msg = &txn->req;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008094
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008095 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8096 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8097 */
8098
8099 if (!s || !req)
8100 return 0;
8101
Willy Tarreau655dce92009-11-08 13:10:58 +01008102 if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008103 /* Already decoded as OK */
8104 test->flags |= ACL_TEST_F_SET_RES_PASS;
8105 return 1;
8106 }
8107
8108 /* Try to decode HTTP request */
Willy Tarreaua458b672012-03-05 11:17:50 +01008109 if (likely(msg->next < req->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01008110 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008111
Willy Tarreau655dce92009-11-08 13:10:58 +01008112 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008113 if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) {
8114 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8115 return 1;
8116 }
8117 /* wait for final state */
8118 test->flags |= ACL_TEST_F_MAY_CHANGE;
8119 return 0;
8120 }
8121
8122 /* OK we got a valid HTTP request. We have some minor preparation to
8123 * perform so that further checks can rely on HTTP tests.
8124 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008125 txn->meth = find_http_meth(msg->buf->p + msg->sol, msg->sl.rq.m_l);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008126 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8127 s->flags |= SN_REDIRECTABLE;
8128
Willy Tarreau418bfcc2012-03-09 13:56:20 +01008129 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008130 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8131 return 1;
8132 }
8133
8134 test->flags |= ACL_TEST_F_SET_RES_PASS;
8135 return 1;
8136}
8137
Willy Tarreau7f18e522010-10-22 20:04:13 +02008138/* return a valid test if the current request is the first one on the connection */
8139static int
8140acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir,
8141 struct acl_expr *expr, struct acl_test *test)
8142{
8143 if (!s)
8144 return 0;
8145
8146 if (s->txn.flags & TX_NOT_FIRST)
8147 test->flags |= ACL_TEST_F_SET_RES_FAIL;
8148 else
8149 test->flags |= ACL_TEST_F_SET_RES_PASS;
8150
8151 return 1;
8152}
8153
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008154static int
8155acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir,
8156 struct acl_expr *expr, struct acl_test *test)
8157{
8158
8159 if (!s)
8160 return 0;
8161
8162 if (!get_http_auth(s))
8163 return 0;
8164
8165 test->ctx.a[0] = expr->arg.ul;
8166 test->ctx.a[1] = s->txn.auth.user;
8167 test->ctx.a[2] = s->txn.auth.pass;
8168
8169 test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH;
8170
8171 return 1;
8172}
Willy Tarreau8797c062007-05-07 00:55:35 +02008173
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008174/* Try to find the next occurrence of a cookie name in a cookie header value.
8175 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8176 * the cookie value is returned into *value and *value_l, and the function
8177 * returns a pointer to the next pointer to search from if the value was found.
8178 * Otherwise if the cookie was not found, NULL is returned and neither value
8179 * nor value_l are touched. The input <hdr> string should first point to the
8180 * header's value, and the <hdr_end> pointer must point to the first character
8181 * not part of the value. <list> must be non-zero if value may represent a list
8182 * of values (cookie headers). This makes it faster to abort parsing when no
8183 * list is expected.
8184 */
8185static char *
8186extract_cookie_value(char *hdr, const char *hdr_end,
8187 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008188 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008189{
8190 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8191 char *next;
8192
8193 /* we search at least a cookie name followed by an equal, and more
8194 * generally something like this :
8195 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8196 */
8197 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8198 /* Iterate through all cookies on this line */
8199
8200 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8201 att_beg++;
8202
8203 /* find att_end : this is the first character after the last non
8204 * space before the equal. It may be equal to hdr_end.
8205 */
8206 equal = att_end = att_beg;
8207
8208 while (equal < hdr_end) {
8209 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8210 break;
8211 if (http_is_spht[(unsigned char)*equal++])
8212 continue;
8213 att_end = equal;
8214 }
8215
8216 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8217 * is between <att_beg> and <equal>, both may be identical.
8218 */
8219
8220 /* look for end of cookie if there is an equal sign */
8221 if (equal < hdr_end && *equal == '=') {
8222 /* look for the beginning of the value */
8223 val_beg = equal + 1;
8224 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8225 val_beg++;
8226
8227 /* find the end of the value, respecting quotes */
8228 next = find_cookie_value_end(val_beg, hdr_end);
8229
8230 /* make val_end point to the first white space or delimitor after the value */
8231 val_end = next;
8232 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8233 val_end--;
8234 } else {
8235 val_beg = val_end = next = equal;
8236 }
8237
8238 /* We have nothing to do with attributes beginning with '$'. However,
8239 * they will automatically be removed if a header before them is removed,
8240 * since they're supposed to be linked together.
8241 */
8242 if (*att_beg == '$')
8243 continue;
8244
8245 /* Ignore cookies with no equal sign */
8246 if (equal == next)
8247 continue;
8248
8249 /* Now we have the cookie name between att_beg and att_end, and
8250 * its value between val_beg and val_end.
8251 */
8252
8253 if (att_end - att_beg == cookie_name_l &&
8254 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8255 /* let's return this value and indicate where to go on from */
8256 *value = val_beg;
8257 *value_l = val_end - val_beg;
8258 return next + 1;
8259 }
8260
8261 /* Set-Cookie headers only have the name in the first attr=value part */
8262 if (!list)
8263 break;
8264 }
8265
8266 return NULL;
8267}
8268
8269/* Iterate over all cookies present in a request. The context is stored in
8270 * test->ctx.a[0] for the in-header position, test->ctx.a[1] for the
8271 * end-of-header-value, and test->ctx.a[2] for the hdr_idx. If <multi> is
8272 * non-null, then multiple cookies may be parsed on the same line.
8273 * The cookie name is in expr->arg and the name length in expr->arg_len.
8274 */
8275static int
8276acl_fetch_any_cookie_value(struct proxy *px, struct session *l4, void *l7, char *sol,
8277 const char *hdr_name, int hdr_name_len, int multi,
8278 struct acl_expr *expr, struct acl_test *test)
8279{
8280 struct http_txn *txn = l7;
8281 struct hdr_idx *idx = &txn->hdr_idx;
8282 struct hdr_ctx *ctx = (struct hdr_ctx *)&test->ctx.a[2];
8283
8284 if (!txn)
8285 return 0;
8286
8287 if (!(test->flags & ACL_TEST_F_FETCH_MORE)) {
8288 /* search for the header from the beginning, we must first initialize
8289 * the search parameters.
8290 */
8291 test->ctx.a[0] = NULL;
8292 ctx->idx = 0;
8293 }
8294
8295 while (1) {
8296 /* Note: test->ctx.a[0] == NULL every time we need to fetch a new header */
8297 if (!test->ctx.a[0]) {
8298 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8299 goto out;
8300
8301 if (ctx->vlen < expr->arg_len + 1)
8302 continue;
8303
8304 test->ctx.a[0] = ctx->line + ctx->val;
8305 test->ctx.a[1] = test->ctx.a[0] + ctx->vlen;
8306 }
8307
8308 test->ctx.a[0] = extract_cookie_value(test->ctx.a[0], test->ctx.a[1],
8309 expr->arg.str, expr->arg_len, multi,
8310 &temp_pattern.data.str.str,
8311 &temp_pattern.data.str.len);
8312 if (test->ctx.a[0]) {
8313 /* one value was returned into temp_pattern.data.str.{str,len} */
8314 test->flags |= ACL_TEST_F_FETCH_MORE;
8315 test->flags |= ACL_TEST_F_VOL_HDR;
8316 return 1;
8317 }
8318 }
8319
8320 out:
8321 test->flags &= ~ACL_TEST_F_FETCH_MORE;
8322 test->flags |= ACL_TEST_F_VOL_HDR;
8323 return 0;
8324}
8325
8326static int
8327acl_fetch_cookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8328 struct acl_expr *expr, struct acl_test *test)
8329{
8330 struct http_txn *txn = l7;
8331
8332 if (!txn)
8333 return 0;
8334
8335 if (txn->req.msg_state < HTTP_MSG_BODY)
8336 return 0;
8337
8338 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8339 /* ensure the indexes are not affected */
8340 return 0;
8341
8342 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008343 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 +02008344}
8345
8346static int
8347acl_fetch_scookie_value(struct proxy *px, struct session *l4, void *l7, int dir,
8348 struct acl_expr *expr, struct acl_test *test)
8349{
8350 struct http_txn *txn = l7;
8351
8352 if (!txn)
8353 return 0;
8354
8355 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8356 return 0;
8357
8358 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008359 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 +02008360}
8361
8362/* Iterate over all cookies present in a request to count how many occurrences
8363 * match the name in expr->arg and expr->arg_len. If <multi> is non-null, then
8364 * multiple cookies may be parsed on the same line.
8365 */
8366static int
8367acl_fetch_any_cookie_cnt(struct proxy *px, struct session *l4, void *l7, char *sol,
8368 const char *hdr_name, int hdr_name_len, int multi,
8369 struct acl_expr *expr, struct acl_test *test)
8370{
8371 struct http_txn *txn = l7;
8372 struct hdr_idx *idx = &txn->hdr_idx;
8373 struct hdr_ctx ctx;
8374 int cnt;
8375 char *val_beg, *val_end;
8376
8377 if (!txn)
8378 return 0;
8379
Willy Tarreau46787ed2012-04-11 17:28:40 +02008380 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008381 ctx.idx = 0;
8382 cnt = 0;
8383
8384 while (1) {
8385 /* Note: val_beg == NULL every time we need to fetch a new header */
8386 if (!val_beg) {
8387 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8388 break;
8389
8390 if (ctx.vlen < expr->arg_len + 1)
8391 continue;
8392
8393 val_beg = ctx.line + ctx.val;
8394 val_end = val_beg + ctx.vlen;
8395 }
8396
8397 while ((val_beg = extract_cookie_value(val_beg, val_end,
8398 expr->arg.str, expr->arg_len, multi,
8399 &temp_pattern.data.str.str,
8400 &temp_pattern.data.str.len))) {
8401 cnt++;
8402 }
8403 }
8404
8405 temp_pattern.data.integer = cnt;
8406 test->flags |= ACL_TEST_F_VOL_HDR;
8407 return 1;
8408}
8409
8410static int
8411acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8412 struct acl_expr *expr, struct acl_test *test)
8413{
8414 struct http_txn *txn = l7;
8415
8416 if (!txn)
8417 return 0;
8418
8419 if (txn->req.msg_state < HTTP_MSG_BODY)
8420 return 0;
8421
8422 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8423 /* ensure the indexes are not affected */
8424 return 0;
8425
8426 /* The Cookie header allows multiple cookies on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008427 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 +02008428}
8429
8430static int
8431acl_fetch_scookie_cnt(struct proxy *px, struct session *l4, void *l7, int dir,
8432 struct acl_expr *expr, struct acl_test *test)
8433{
8434 struct http_txn *txn = l7;
8435
8436 if (!txn)
8437 return 0;
8438
8439 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8440 return 0;
8441
8442 /* The Set-Cookie header allows only one cookie on the same line */
Willy Tarreau3a215be2012-03-09 21:39:51 +01008443 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 +02008444}
8445
Willy Tarreau8797c062007-05-07 00:55:35 +02008446/************************************************************************/
8447/* All supported keywords must be declared here. */
8448/************************************************************************/
8449
8450/* Note: must not be declared <const> as its list will be overwritten */
8451static struct acl_kw_list acl_kws = {{ },{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008452 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT },
8453
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008454 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT },
Willy Tarreauc4262962010-05-10 23:42:40 +02008455 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8456 { "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 +02008457 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT },
Willy Tarreau8797c062007-05-07 00:55:35 +02008458
Willy Tarreauc4262962010-05-10 23:42:40 +02008459 { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008460 { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8461 { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8462 { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8463 { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8464 { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8465 { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008466 { "url_len", acl_parse_int, acl_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008467 { "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 +02008468 { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau8797c062007-05-07 00:55:35 +02008469
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008470 /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */
Willy Tarreauc4262962010-05-10 23:42:40 +02008471 { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008472 { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8473 { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8474 { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8475 { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8476 { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8477 { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8478 { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008479 { "hdr_len", acl_parse_int, acl_fetch_chdr, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008480 { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008481 { "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 +02008482
Willy Tarreauc4262962010-05-10 23:42:40 +02008483 { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008484 { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8485 { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8486 { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8487 { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8488 { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8489 { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8490 { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008491 { "shdr_len", acl_parse_int, acl_fetch_shdr, acl_match_len, ACL_USE_L7RTR_VOLATILE },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008492 { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE },
Willy Tarreaub337b532010-05-13 20:03:41 +02008493 { "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 +02008494
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008495 { "cook", acl_parse_str, acl_fetch_cookie_value, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
8496 { "cook_reg", acl_parse_reg, acl_fetch_cookie_value, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8497 { "cook_beg", acl_parse_str, acl_fetch_cookie_value, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8498 { "cook_end", acl_parse_str, acl_fetch_cookie_value, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8499 { "cook_sub", acl_parse_str, acl_fetch_cookie_value, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8500 { "cook_dir", acl_parse_str, acl_fetch_cookie_value, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8501 { "cook_dom", acl_parse_str, acl_fetch_cookie_value, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
8502 { "cook_len", acl_parse_int, acl_fetch_cookie_value, acl_match_len, ACL_USE_L7REQ_VOLATILE },
8503 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE },
8504
8505 { "scook", acl_parse_str, acl_fetch_scookie_value, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP },
8506 { "scook_reg", acl_parse_reg, acl_fetch_scookie_value, acl_match_reg, ACL_USE_L7RTR_VOLATILE },
8507 { "scook_beg", acl_parse_str, acl_fetch_scookie_value, acl_match_beg, ACL_USE_L7RTR_VOLATILE },
8508 { "scook_end", acl_parse_str, acl_fetch_scookie_value, acl_match_end, ACL_USE_L7RTR_VOLATILE },
8509 { "scook_sub", acl_parse_str, acl_fetch_scookie_value, acl_match_sub, ACL_USE_L7RTR_VOLATILE },
8510 { "scook_dir", acl_parse_str, acl_fetch_scookie_value, acl_match_dir, ACL_USE_L7RTR_VOLATILE },
8511 { "scook_dom", acl_parse_str, acl_fetch_scookie_value, acl_match_dom, ACL_USE_L7RTR_VOLATILE },
8512 { "scook_len", acl_parse_int, acl_fetch_scookie_value, acl_match_len, ACL_USE_L7RTR_VOLATILE },
8513 { "scook_cnt", acl_parse_int, acl_fetch_scookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE },
8514
Willy Tarreauc4262962010-05-10 23:42:40 +02008515 { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP },
Willy Tarreau0ceba5a2008-07-25 19:31:03 +02008516 { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE },
8517 { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE },
8518 { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE },
8519 { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE },
8520 { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE },
8521 { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau0e698542011-09-16 08:32:32 +02008522 { "path_len", acl_parse_int, acl_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE },
Willy Tarreau737b0c12007-06-10 21:28:46 +02008523
Willy Tarreau7f18e522010-10-22 20:04:13 +02008524 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8525 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT },
8526 { "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 +02008527 { NULL, NULL, NULL, NULL },
Willy Tarreau8797c062007-05-07 00:55:35 +02008528}};
8529
Willy Tarreau4a568972010-05-12 08:08:50 +02008530/************************************************************************/
8531/* The code below is dedicated to pattern fetching and matching */
8532/************************************************************************/
8533
Willy Tarreaue428fb72011-12-16 21:50:30 +01008534/* Returns the last occurrence of specified header. */
Willy Tarreau4a568972010-05-12 08:08:50 +02008535static int
Willy Tarreaue428fb72011-12-16 21:50:30 +01008536pattern_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir,
8537 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
Willy Tarreau4a568972010-05-12 08:08:50 +02008538{
8539 struct http_txn *txn = l7;
Willy Tarreau294c4732011-12-16 21:35:50 +01008540
Willy Tarreaue428fb72011-12-16 21:50:30 +01008541 return http_get_hdr(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx,
8542 -1, NULL, &data->str.str, &data->str.len);
Willy Tarreau4a568972010-05-12 08:08:50 +02008543}
8544
David Cournapeau16023ee2010-12-23 20:55:41 +09008545/*
8546 * Given a path string and its length, find the position of beginning of the
8547 * query string. Returns NULL if no query string is found in the path.
8548 *
8549 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8550 *
8551 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8552 */
8553static inline char *find_query_string(char *path, size_t path_l)
8554{
8555 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008556
David Cournapeau16023ee2010-12-23 20:55:41 +09008557 p = memchr(path, '?', path_l);
8558 return p ? p + 1 : NULL;
8559}
8560
8561static inline int is_param_delimiter(char c)
8562{
8563 return c == '&' || c == ';';
8564}
8565
8566/*
8567 * Given a url parameter, find the starting position of the first occurence,
8568 * or NULL if the parameter is not found.
8569 *
8570 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8571 * the function will return query_string+8.
8572 */
8573static char*
8574find_url_param_pos(char* query_string, size_t query_string_l,
8575 char* url_param_name, size_t url_param_name_l)
8576{
8577 char *pos, *last;
8578
8579 pos = query_string;
8580 last = query_string + query_string_l - url_param_name_l - 1;
8581
8582 while (pos <= last) {
8583 if (pos[url_param_name_l] == '=') {
8584 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8585 return pos;
8586 pos += url_param_name_l + 1;
8587 }
8588 while (pos <= last && !is_param_delimiter(*pos))
8589 pos++;
8590 pos++;
8591 }
8592 return NULL;
8593}
8594
8595/*
8596 * Given a url parameter name, returns its value and size into *value and
8597 * *value_l respectively, and returns non-zero. If the parameter is not found,
8598 * zero is returned and value/value_l are not touched.
8599 */
8600static int
8601find_url_param_value(char* path, size_t path_l,
8602 char* url_param_name, size_t url_param_name_l,
8603 char** value, size_t* value_l)
8604{
8605 char *query_string, *qs_end;
8606 char *arg_start;
8607 char *value_start, *value_end;
8608
8609 query_string = find_query_string(path, path_l);
8610 if (!query_string)
8611 return 0;
8612
8613 qs_end = path + path_l;
8614 arg_start = find_url_param_pos(query_string, qs_end - query_string,
8615 url_param_name, url_param_name_l);
8616 if (!arg_start)
8617 return 0;
8618
8619 value_start = arg_start + url_param_name_l + 1;
8620 value_end = value_start;
8621
8622 while ((value_end < qs_end) && !is_param_delimiter(*value_end))
8623 value_end++;
8624
8625 *value = value_start;
8626 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008627 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008628}
8629
8630static int
8631pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir,
8632 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8633{
8634 struct http_txn *txn = l7;
8635 struct http_msg *msg = &txn->req;
8636 char *url_param_value;
8637 size_t url_param_value_l;
8638
Willy Tarreau3a215be2012-03-09 21:39:51 +01008639 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 +09008640 arg_p->data.str.str, arg_p->data.str.len,
8641 &url_param_value, &url_param_value_l))
8642 return 0;
8643
8644 data->str.str = url_param_value;
8645 data->str.len = url_param_value_l;
8646 return 1;
8647}
8648
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008649/* Try to find in request or response message is in <msg> and whose transaction
8650 * is in <txn> the last occurrence of a cookie name in all cookie header values
8651 * whose header name is <hdr_name> with name of length <hdr_name_len>. The
8652 * pointer and size of the last occurrence of the cookie value is returned into
8653 * <value> and <value_l>, and the function returns non-zero if the value was
8654 * found. Otherwise if the cookie was not found, zero is returned and neither
8655 * value nor value_l are touched. The input hdr string should begin at the
8656 * header's value, and its size should be in hdr_l. <list> must be non-zero if
8657 * value may represent a list of values (cookie headers).
8658 */
8659
8660static int
8661find_cookie_value(struct http_msg *msg, struct http_txn *txn,
8662 const char *hdr_name, int hdr_name_len,
8663 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008664 char **value, int *value_l)
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008665{
8666 struct hdr_ctx ctx;
8667 int found = 0;
8668
8669 ctx.idx = 0;
Willy Tarreau3a215be2012-03-09 21:39:51 +01008670 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 +02008671 char *hdr, *end;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008672 if (ctx.vlen < cookie_name_l + 1)
8673 continue;
8674
Willy Tarreau4573af92012-04-06 18:20:06 +02008675 hdr = ctx.line + ctx.val;
8676 end = hdr + ctx.vlen;
8677 while ((hdr = extract_cookie_value(hdr, end, cookie_name, cookie_name_l, 1, value, value_l)))
8678 found = 1;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008679 }
8680 return found;
8681}
8682
8683static int
8684pattern_fetch_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8685 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8686{
8687 struct http_txn *txn = l7;
8688 struct http_msg *msg = &txn->req;
8689 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008690 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008691 int found = 0;
8692
8693 found = find_cookie_value(msg, txn, "Cookie", 6,
8694 arg_p->data.str.str, arg_p->data.str.len, 1,
8695 &cookie_value, &cookie_value_l);
8696 if (found) {
8697 data->str.str = cookie_value;
8698 data->str.len = cookie_value_l;
8699 }
8700
8701 return found;
8702}
8703
8704
8705static int
8706pattern_fetch_set_cookie(struct proxy *px, struct session *l4, void *l7, int dir,
8707 const struct pattern_arg *arg_p, int arg_i, union pattern_data *data)
8708{
8709 struct http_txn *txn = l7;
8710 struct http_msg *msg = &txn->rsp;
8711 char *cookie_value;
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008712 int cookie_value_l;
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008713 int found = 0;
8714
8715 found = find_cookie_value(msg, txn, "Set-Cookie", 10,
8716 arg_p->data.str.str, arg_p->data.str.len, 1,
8717 &cookie_value, &cookie_value_l);
8718 if (found) {
8719 data->str.str = cookie_value;
8720 data->str.len = cookie_value_l;
8721 }
8722
8723 return found;
8724}
8725
Emeric Brun485479d2010-09-23 18:02:19 +02008726
Willy Tarreau4a568972010-05-12 08:08:50 +02008727/************************************************************************/
8728/* All supported keywords must be declared here. */
8729/************************************************************************/
8730/* Note: must not be declared <const> as its list will be overwritten */
8731static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{
Willy Tarreaue428fb72011-12-16 21:50:30 +01008732 { "hdr", pattern_fetch_hdr, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
David Cournapeau16023ee2010-12-23 20:55:41 +09008733 { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
Willy Tarreaub3eb2212011-07-01 16:16:17 +02008734 { "cookie", pattern_fetch_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ },
8735 { "set-cookie", pattern_fetch_set_cookie, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_RTR },
Emeric Brun485479d2010-09-23 18:02:19 +02008736 { NULL, NULL, NULL, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02008737}};
8738
Willy Tarreau8797c062007-05-07 00:55:35 +02008739
8740__attribute__((constructor))
8741static void __http_protocol_init(void)
8742{
8743 acl_register_keywords(&acl_kws);
Willy Tarreau4a568972010-05-12 08:08:50 +02008744 pattern_register_fetches(&pattern_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02008745}
8746
8747
Willy Tarreau58f10d72006-12-04 02:26:12 +01008748/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02008749 * Local variables:
8750 * c-indent-level: 8
8751 * c-basic-offset: 8
8752 * End:
8753 */