blob: e410b6dddc2f36882d4141cf236be68e2337461a [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 Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020057#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010062#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010064#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/task.h>
66
Willy Tarreau522d6c02009-12-06 18:49:18 +010067const char HTTP_100[] =
68 "HTTP/1.1 100 Continue\r\n\r\n";
69
70const struct chunk http_100_chunk = {
71 .str = (char *)&HTTP_100,
72 .len = sizeof(HTTP_100)-1
73};
74
Willy Tarreaua9679ac2010-01-03 17:32:57 +010075/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Willy Tarreaubaaee002006-06-26 02:48:02 +020095/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
96const char *HTTP_401_fmt =
97 "HTTP/1.0 401 Unauthorized\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200100 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
102 "\r\n"
103 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
104
Willy Tarreau844a7e72010-01-31 21:46:18 +0100105const char *HTTP_407_fmt =
106 "HTTP/1.0 407 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
109 "Content-Type: text/html\r\n"
110 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200116 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100117 [HTTP_ERR_400] = 400,
118 [HTTP_ERR_403] = 403,
119 [HTTP_ERR_408] = 408,
120 [HTTP_ERR_500] = 500,
121 [HTTP_ERR_502] = 502,
122 [HTTP_ERR_503] = 503,
123 [HTTP_ERR_504] = 504,
124};
125
Willy Tarreau80587432006-12-24 17:47:20 +0100126static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200127 [HTTP_ERR_200] =
128 "HTTP/1.0 200 OK\r\n"
129 "Cache-Control: no-cache\r\n"
130 "Connection: close\r\n"
131 "Content-Type: text/html\r\n"
132 "\r\n"
133 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100136 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 "Cache-Control: no-cache\r\n"
138 "Connection: close\r\n"
139 "Content-Type: text/html\r\n"
140 "\r\n"
141 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
142
143 [HTTP_ERR_403] =
144 "HTTP/1.0 403 Forbidden\r\n"
145 "Cache-Control: no-cache\r\n"
146 "Connection: close\r\n"
147 "Content-Type: text/html\r\n"
148 "\r\n"
149 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
150
151 [HTTP_ERR_408] =
152 "HTTP/1.0 408 Request Time-out\r\n"
153 "Cache-Control: no-cache\r\n"
154 "Connection: close\r\n"
155 "Content-Type: text/html\r\n"
156 "\r\n"
157 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
158
159 [HTTP_ERR_500] =
160 "HTTP/1.0 500 Server Error\r\n"
161 "Cache-Control: no-cache\r\n"
162 "Connection: close\r\n"
163 "Content-Type: text/html\r\n"
164 "\r\n"
165 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
166
167 [HTTP_ERR_502] =
168 "HTTP/1.0 502 Bad Gateway\r\n"
169 "Cache-Control: no-cache\r\n"
170 "Connection: close\r\n"
171 "Content-Type: text/html\r\n"
172 "\r\n"
173 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
174
175 [HTTP_ERR_503] =
176 "HTTP/1.0 503 Service Unavailable\r\n"
177 "Cache-Control: no-cache\r\n"
178 "Connection: close\r\n"
179 "Content-Type: text/html\r\n"
180 "\r\n"
181 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
182
183 [HTTP_ERR_504] =
184 "HTTP/1.0 504 Gateway Time-out\r\n"
185 "Cache-Control: no-cache\r\n"
186 "Connection: close\r\n"
187 "Content-Type: text/html\r\n"
188 "\r\n"
189 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
190
191};
192
Cyril Bonté19979e12012-04-04 12:57:21 +0200193/* status codes available for the stats admin page (strictly 4 chars length) */
194const char *stat_status_codes[STAT_STATUS_SIZE] = {
195 [STAT_STATUS_DENY] = "DENY",
196 [STAT_STATUS_DONE] = "DONE",
197 [STAT_STATUS_ERRP] = "ERRP",
198 [STAT_STATUS_EXCD] = "EXCD",
199 [STAT_STATUS_NONE] = "NONE",
200 [STAT_STATUS_PART] = "PART",
201 [STAT_STATUS_UNKN] = "UNKN",
202};
203
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205/* We must put the messages here since GCC cannot initialize consts depending
206 * on strlen().
207 */
208struct chunk http_err_chunks[HTTP_ERR_SIZE];
209
Willy Tarreau42250582007-04-01 01:30:43 +0200210#define FD_SETS_ARE_BITFIELDS
211#ifdef FD_SETS_ARE_BITFIELDS
212/*
213 * This map is used with all the FD_* macros to check whether a particular bit
214 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
215 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
216 * byte should be encoded. Be careful to always pass bytes from 0 to 255
217 * exclusively to the macros.
218 */
219fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
220fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
221
222#else
223#error "Check if your OS uses bitfields for fd_sets"
224#endif
225
Willy Tarreau80587432006-12-24 17:47:20 +0100226void init_proto_http()
227{
Willy Tarreau42250582007-04-01 01:30:43 +0200228 int i;
229 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100230 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200231
Willy Tarreau80587432006-12-24 17:47:20 +0100232 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
233 if (!http_err_msgs[msg]) {
234 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
235 abort();
236 }
237
238 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
239 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
240 }
Willy Tarreau42250582007-04-01 01:30:43 +0200241
242 /* initialize the log header encoding map : '{|}"#' should be encoded with
243 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
244 * URL encoding only requires '"', '#' to be encoded as well as non-
245 * printable characters above.
246 */
247 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
248 memset(url_encode_map, 0, sizeof(url_encode_map));
249 for (i = 0; i < 32; i++) {
250 FD_SET(i, hdr_encode_map);
251 FD_SET(i, url_encode_map);
252 }
253 for (i = 127; i < 256; i++) {
254 FD_SET(i, hdr_encode_map);
255 FD_SET(i, url_encode_map);
256 }
257
258 tmp = "\"#{|}";
259 while (*tmp) {
260 FD_SET(*tmp, hdr_encode_map);
261 tmp++;
262 }
263
264 tmp = "\"#";
265 while (*tmp) {
266 FD_SET(*tmp, url_encode_map);
267 tmp++;
268 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200269
270 /* memory allocations */
271 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200272 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100273 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100274}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275
Willy Tarreau53b6c742006-12-17 13:37:46 +0100276/*
277 * We have 26 list of methods (1 per first letter), each of which can have
278 * up to 3 entries (2 valid, 1 null).
279 */
280struct http_method_desc {
281 http_meth_t meth;
282 int len;
283 const char text[8];
284};
285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100286const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100287 ['C' - 'A'] = {
288 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
289 },
290 ['D' - 'A'] = {
291 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
292 },
293 ['G' - 'A'] = {
294 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
295 },
296 ['H' - 'A'] = {
297 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
298 },
299 ['P' - 'A'] = {
300 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
301 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
302 },
303 ['T' - 'A'] = {
304 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
305 },
306 /* rest is empty like this :
307 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
308 */
309};
310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100311/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200312 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100313 * RFC2616 for those chars.
314 */
315
316const char http_is_spht[256] = {
317 [' '] = 1, ['\t'] = 1,
318};
319
320const char http_is_crlf[256] = {
321 ['\r'] = 1, ['\n'] = 1,
322};
323
324const char http_is_lws[256] = {
325 [' '] = 1, ['\t'] = 1,
326 ['\r'] = 1, ['\n'] = 1,
327};
328
329const char http_is_sep[256] = {
330 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
331 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
332 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
333 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
334 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
335};
336
337const char http_is_ctl[256] = {
338 [0 ... 31] = 1,
339 [127] = 1,
340};
341
342/*
343 * A token is any ASCII char that is neither a separator nor a CTL char.
344 * Do not overwrite values in assignment since gcc-2.95 will not handle
345 * them correctly. Instead, define every non-CTL char's status.
346 */
347const char http_is_token[256] = {
348 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
349 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
350 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
351 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
352 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
353 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
354 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
355 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
356 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
357 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
358 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
359 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
360 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
361 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
362 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
363 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
364 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
365 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
366 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
367 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
368 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
369 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
370 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
371 ['|'] = 1, ['}'] = 0, ['~'] = 1,
372};
373
374
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100375/*
376 * An http ver_token is any ASCII which can be found in an HTTP version,
377 * which includes 'H', 'T', 'P', '/', '.' and any digit.
378 */
379const char http_is_ver_token[256] = {
380 ['.'] = 1, ['/'] = 1,
381 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
382 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
383 ['H'] = 1, ['P'] = 1, ['T'] = 1,
384};
385
386
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100387/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100388 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
389 */
390#if defined(DEBUG_FSM)
391static void http_silent_debug(int line, struct session *s)
392{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100393 chunk_printf(&trash,
394 "[%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",
395 line,
396 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
397 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
398 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100399
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100400 chunk_printf(&trash,
401 " %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",
402 line,
403 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
404 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
405 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100406}
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 Tarreau9b28e032012-10-12 23:49:43 +0200423 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->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 Tarreau9b28e032012-10-12 23:49:43 +0200443 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->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 Tarreau9b28e032012-10-12 23:49:43 +0200612 delta = buffer_replace2(msg->chn->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 Tarreau9b28e032012-10-12 23:49:43 +0200632 delta = buffer_replace2(msg->chn->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 Tarreau8263d2b2012-08-28 00:06:31 +0200651 channel_auto_read(si->ob);
652 channel_abort(si->ob);
653 channel_auto_close(si->ob);
654 channel_erase(si->ob);
655 channel_auto_close(si->ib);
656 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100657 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100658 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200659 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660 }
661 if (!(t->flags & SN_ERR_MASK))
662 t->flags |= err;
663 if (!(t->flags & SN_FINST_MASK))
664 t->flags |= finst;
665}
666
Willy Tarreau80587432006-12-24 17:47:20 +0100667/* This function returns the appropriate error location for the given session
668 * and message.
669 */
670
Willy Tarreau783f2582012-09-04 12:19:04 +0200671struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100672{
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 Tarreau9b28e032012-10-12 23:49:43 +0200714 ptr = txn->req.chn->buf->p + 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.
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200756 * NOTE: this function is designed to support being called once data are scheduled
757 * for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100758 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760{
761 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100762 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100763 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200764 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100765
766 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100767 trash.len = strlen(HTTP_302);
768 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100769
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100770 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100771
Willy Tarreauefb453c2008-10-26 20:49:47 +0100772 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100773 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100774 return;
775
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100776 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100778 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
779 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100780 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100781
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200782 /* 3: add the request URI. Since it was already forwarded, we need
783 * to temporarily rewind the buffer.
784 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200786 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200787
Willy Tarreauefb453c2008-10-26 20:49:47 +0100788 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200789 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200790
Willy Tarreau9b28e032012-10-12 23:49:43 +0200791 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200792
Willy Tarreauefb453c2008-10-26 20:49:47 +0100793 if (!path)
794 return;
795
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100796 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100797 return;
798
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100799 memcpy(trash.str + trash.len, path, len);
800 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100801
802 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100803 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
804 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100805 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100806 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
807 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100808 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100809
810 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200811 si_shutr(si);
812 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813 si->err_type = SI_ET_NONE;
814 si->err_loc = NULL;
815 si->state = SI_ST_CLO;
816
817 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100818 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100819
820 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100821 if (srv)
822 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823}
824
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100825/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 * that the server side is closed. Note that err_type is actually a
827 * bitmask, where almost only aborts may be cumulated with other
828 * values. We consider that aborted operations are more important
829 * than timeouts or errors due to the fact that nobody else in the
830 * logs might explain incomplete retries. All others should avoid
831 * being cumulated. It should normally not be possible to have multiple
832 * aborts at once, but just in case, the first one in sequence is reported.
833 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100834void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100836 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837
838 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100839 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200840 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100841 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100842 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200843 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100844 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100845 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200846 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100847 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100848 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200849 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100851 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200852 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100853 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100854 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200855 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100856 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100857 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200858 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100859}
860
Willy Tarreau42250582007-04-01 01:30:43 +0200861extern const char sess_term_cond[8];
862extern const char sess_fin_state[8];
863extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200864struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200865struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100866struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100867
Willy Tarreau117f59e2007-03-04 18:17:17 +0100868/*
869 * Capture headers from message starting at <som> according to header list
870 * <cap_hdr>, and fill the <idx> structure appropriately.
871 */
872void capture_headers(char *som, struct hdr_idx *idx,
873 char **cap, struct cap_hdr *cap_hdr)
874{
875 char *eol, *sol, *col, *sov;
876 int cur_idx;
877 struct cap_hdr *h;
878 int len;
879
880 sol = som + hdr_idx_first_pos(idx);
881 cur_idx = hdr_idx_first_idx(idx);
882
883 while (cur_idx) {
884 eol = sol + idx->v[cur_idx].len;
885
886 col = sol;
887 while (col < eol && *col != ':')
888 col++;
889
890 sov = col + 1;
891 while (sov < eol && http_is_lws[(unsigned char)*sov])
892 sov++;
893
894 for (h = cap_hdr; h; h = h->next) {
895 if ((h->namelen == col - sol) &&
896 (strncasecmp(sol, h->name, h->namelen) == 0)) {
897 if (cap[h->index] == NULL)
898 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200899 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100900
901 if (cap[h->index] == NULL) {
902 Alert("HTTP capture : out of memory.\n");
903 continue;
904 }
905
906 len = eol - sov;
907 if (len > h->len)
908 len = h->len;
909
910 memcpy(cap[h->index], sov, len);
911 cap[h->index][len]=0;
912 }
913 }
914 sol = eol + idx->v[cur_idx].cr + 1;
915 cur_idx = idx->v[cur_idx].next;
916 }
917}
918
919
Willy Tarreau42250582007-04-01 01:30:43 +0200920/* either we find an LF at <ptr> or we jump to <bad>.
921 */
922#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
923
924/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
925 * otherwise to <http_msg_ood> with <state> set to <st>.
926 */
927#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
928 ptr++; \
929 if (likely(ptr < end)) \
930 goto good; \
931 else { \
932 state = (st); \
933 goto http_msg_ood; \
934 } \
935 } while (0)
936
937
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100939 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100940 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
941 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
942 * will give undefined results.
943 * Note that it is upon the caller's responsibility to ensure that ptr < end,
944 * and that msg->sol points to the beginning of the response.
945 * If a complete line is found (which implies that at least one CR or LF is
946 * found before <end>, the updated <ptr> is returned, otherwise NULL is
947 * returned indicating an incomplete line (which does not mean that parts have
948 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
949 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
950 * upon next call.
951 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200952 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100953 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
954 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200955 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200957const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100958 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100959 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100960{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200961 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100962
Willy Tarreau8973c702007-01-21 23:58:29 +0100963 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100964 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200965 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100966 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
968
969 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100970 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
972 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100973 state = HTTP_MSG_ERROR;
974 break;
975
Willy Tarreau8973c702007-01-21 23:58:29 +0100976 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200977 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100978 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100979 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100980 goto http_msg_rpcode;
981 }
982 if (likely(HTTP_IS_SPHT(*ptr)))
983 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
984 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100985 state = HTTP_MSG_ERROR;
986 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100987
Willy Tarreau8973c702007-01-21 23:58:29 +0100988 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200989 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 if (likely(!HTTP_IS_LWS(*ptr)))
991 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
992
993 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100994 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100995 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
996 }
997
998 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100999 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001000 http_msg_rsp_reason:
1001 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001002 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001003 msg->sl.st.r_l = 0;
1004 goto http_msg_rpline_eol;
1005
Willy Tarreau8973c702007-01-21 23:58:29 +01001006 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001007 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001008 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001009 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001010 goto http_msg_rpreason;
1011 }
1012 if (likely(HTTP_IS_SPHT(*ptr)))
1013 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1014 /* so it's a CR/LF, so there is no reason phrase */
1015 goto http_msg_rsp_reason;
1016
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001018 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001019 if (likely(!HTTP_IS_CRLF(*ptr)))
1020 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001021 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001022 http_msg_rpline_eol:
1023 /* We have seen the end of line. Note that we do not
1024 * necessarily have the \n yet, but at least we know that we
1025 * have EITHER \r OR \n, otherwise the response would not be
1026 * complete. We can then record the response length and return
1027 * to the caller which will be able to register it.
1028 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001029 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001030 return ptr;
1031
1032#ifdef DEBUG_FULL
1033 default:
1034 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1035 exit(1);
1036#endif
1037 }
1038
1039 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001040 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001041 if (ret_state)
1042 *ret_state = state;
1043 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001044 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001045 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001046}
1047
Willy Tarreau8973c702007-01-21 23:58:29 +01001048/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001049 * This function parses a request line between <ptr> and <end>, starting with
1050 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1051 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1052 * will give undefined results.
1053 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1054 * and that msg->sol points to the beginning of the request.
1055 * If a complete line is found (which implies that at least one CR or LF is
1056 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1057 * returned indicating an incomplete line (which does not mean that parts have
1058 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1059 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1060 * upon next call.
1061 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001062 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001063 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1064 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001065 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001067const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001068 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001069 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001071 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001075 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 if (likely(HTTP_IS_TOKEN(*ptr)))
1077 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001080 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1082 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 if (likely(HTTP_IS_CRLF(*ptr))) {
1085 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001088 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001089 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001090 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001092 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 msg->sl.rq.v_l = 0;
1094 goto http_msg_rqline_eol;
1095 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001096 state = HTTP_MSG_ERROR;
1097 break;
1098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001099 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001100 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001102 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 goto http_msg_rquri;
1104 }
1105 if (likely(HTTP_IS_SPHT(*ptr)))
1106 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1107 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1108 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001111 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001112 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001116 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1118 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001119
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001120 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001121 /* non-ASCII chars are forbidden unless option
1122 * accept-invalid-http-request is enabled in the frontend.
1123 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001124 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001125 if (msg->err_pos < -1)
1126 goto invalid_char;
1127 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001128 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1130 }
1131
1132 if (likely(HTTP_IS_CRLF(*ptr))) {
1133 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1134 goto http_msg_req09_uri_e;
1135 }
1136
1137 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001138 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001139 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001140 state = HTTP_MSG_ERROR;
1141 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001142
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001143 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001144 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001145 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001146 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 goto http_msg_rqver;
1148 }
1149 if (likely(HTTP_IS_SPHT(*ptr)))
1150 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1151 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1152 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001153
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001154 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001155 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001156 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001158
1159 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001160 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001161 http_msg_rqline_eol:
1162 /* We have seen the end of line. Note that we do not
1163 * necessarily have the \n yet, but at least we know that we
1164 * have EITHER \r OR \n, otherwise the request would not be
1165 * complete. We can then record the request length and return
1166 * to the caller which will be able to register it.
1167 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001168 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001169 return ptr;
1170 }
1171
1172 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001173 state = HTTP_MSG_ERROR;
1174 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176#ifdef DEBUG_FULL
1177 default:
1178 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1179 exit(1);
1180#endif
1181 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001182
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001184 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185 if (ret_state)
1186 *ret_state = state;
1187 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001188 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001189 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001191
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001192/*
1193 * Returns the data from Authorization header. Function may be called more
1194 * than once so data is stored in txn->auth_data. When no header is found
1195 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1196 * searching again for something we are unable to find anyway.
1197 */
1198
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001199char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001200
1201int
1202get_http_auth(struct session *s)
1203{
1204
1205 struct http_txn *txn = &s->txn;
1206 struct chunk auth_method;
1207 struct hdr_ctx ctx;
1208 char *h, *p;
1209 int len;
1210
1211#ifdef DEBUG_AUTH
1212 printf("Auth for session %p: %d\n", s, txn->auth.method);
1213#endif
1214
1215 if (txn->auth.method == HTTP_AUTH_WRONG)
1216 return 0;
1217
1218 if (txn->auth.method)
1219 return 1;
1220
1221 txn->auth.method = HTTP_AUTH_WRONG;
1222
1223 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001224
1225 if (txn->flags & TX_USE_PX_CONN) {
1226 h = "Proxy-Authorization";
1227 len = strlen(h);
1228 } else {
1229 h = "Authorization";
1230 len = strlen(h);
1231 }
1232
Willy Tarreau9b28e032012-10-12 23:49:43 +02001233 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001234 return 0;
1235
1236 h = ctx.line + ctx.val;
1237
1238 p = memchr(h, ' ', ctx.vlen);
1239 if (!p || p == h)
1240 return 0;
1241
1242 chunk_initlen(&auth_method, h, 0, p-h);
1243 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1244
1245 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1246
1247 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001248 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001249
1250 if (len < 0)
1251 return 0;
1252
1253
1254 get_http_auth_buff[len] = '\0';
1255
1256 p = strchr(get_http_auth_buff, ':');
1257
1258 if (!p)
1259 return 0;
1260
1261 txn->auth.user = get_http_auth_buff;
1262 *p = '\0';
1263 txn->auth.pass = p+1;
1264
1265 txn->auth.method = HTTP_AUTH_BASIC;
1266 return 1;
1267 }
1268
1269 return 0;
1270}
1271
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8973c702007-01-21 23:58:29 +01001273/*
1274 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001275 * depending on the initial msg->msg_state. The caller is responsible for
1276 * ensuring that the message does not wrap. The function can be preempted
1277 * everywhere when data are missing and recalled at the exact same location
1278 * with no information loss. The message may even be realigned between two
1279 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001280 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001281 * fields. Note that msg->sol will be initialized after completing the first
1282 * state, so that none of the msg pointers has to be initialized prior to the
1283 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001285void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001287 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001289 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001290
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001291 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001292 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001293 ptr = buf->p + msg->next;
1294 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 if (unlikely(ptr >= end))
1297 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 /*
1301 * First, states that are specific to the response only.
1302 * We check them first so that request and headers are
1303 * closer to each other (accessed more often).
1304 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001306 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001308 /* we have a start of message, but we have to check
1309 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001310 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001311 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001312 if (unlikely(ptr != buf->p)) {
1313 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001314 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001315 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001316 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001317 }
Willy Tarreau26927362012-05-18 23:22:52 +02001318 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001319 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 hdr_idx_init(idx);
1321 state = HTTP_MSG_RPVER;
1322 goto http_msg_rpver;
1323 }
1324
1325 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1326 goto http_msg_invalid;
1327
1328 if (unlikely(*ptr == '\n'))
1329 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1330 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1331 /* stop here */
1332
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001334 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 EXPECT_LF_HERE(ptr, http_msg_invalid);
1336 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1337 /* stop here */
1338
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001340 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 case HTTP_MSG_RPVER_SP:
1342 case HTTP_MSG_RPCODE:
1343 case HTTP_MSG_RPCODE_SP:
1344 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001345 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001346 state, ptr, end,
1347 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 if (unlikely(!ptr))
1349 return;
1350
1351 /* we have a full response and we know that we have either a CR
1352 * or an LF at <ptr>.
1353 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1355
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001356 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001357 if (likely(*ptr == '\r'))
1358 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1359 goto http_msg_rpline_end;
1360
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001362 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001363 /* msg->sol must point to the first of CR or LF. */
1364 EXPECT_LF_HERE(ptr, http_msg_invalid);
1365 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1366 /* stop here */
1367
1368 /*
1369 * Second, states that are specific to the request only
1370 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001372 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001374 /* we have a start of message, but we have to check
1375 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001376 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001377 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001378 if (likely(ptr != buf->p)) {
1379 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001380 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001381 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001382 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 }
Willy Tarreau26927362012-05-18 23:22:52 +02001384 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001385 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001386 state = HTTP_MSG_RQMETH;
1387 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1391 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 if (unlikely(*ptr == '\n'))
1394 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1395 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001396 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001399 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 EXPECT_LF_HERE(ptr, http_msg_invalid);
1401 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001402 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001405 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 case HTTP_MSG_RQMETH_SP:
1407 case HTTP_MSG_RQURI:
1408 case HTTP_MSG_RQURI_SP:
1409 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001410 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001411 state, ptr, end,
1412 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 if (unlikely(!ptr))
1414 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 /* we have a full request and we know that we have either a CR
1417 * or an LF at <ptr>.
1418 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001421 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 if (likely(*ptr == '\r'))
1423 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001427 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 /* check for HTTP/0.9 request : no version information available.
1429 * msg->sol must point to the first of CR or LF.
1430 */
1431 if (unlikely(msg->sl.rq.v_l == 0))
1432 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 EXPECT_LF_HERE(ptr, http_msg_invalid);
1435 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001437
Willy Tarreau8973c702007-01-21 23:58:29 +01001438 /*
1439 * Common states below
1440 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001442 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001443 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 if (likely(!HTTP_IS_CRLF(*ptr))) {
1445 goto http_msg_hdr_name;
1446 }
1447
1448 if (likely(*ptr == '\r'))
1449 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1450 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001453 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 /* assumes msg->sol points to the first char */
1455 if (likely(HTTP_IS_TOKEN(*ptr)))
1456 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001458 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001461 if (likely(msg->err_pos < -1) || *ptr == '\n')
1462 goto http_msg_invalid;
1463
1464 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001465 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001466
1467 /* and we still accept this non-token character */
1468 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001471 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001472 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 if (likely(HTTP_IS_SPHT(*ptr)))
1474 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001477 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001478
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 if (likely(!HTTP_IS_CRLF(*ptr))) {
1480 goto http_msg_hdr_val;
1481 }
1482
1483 if (likely(*ptr == '\r'))
1484 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1485 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001488 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 EXPECT_LF_HERE(ptr, http_msg_invalid);
1490 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 if (likely(HTTP_IS_SPHT(*ptr))) {
1495 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001496 for (; buf->p + msg->sov < ptr; msg->sov++)
1497 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 goto http_msg_hdr_l1_sp;
1499 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001500 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001501 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 goto http_msg_complete_header;
1503
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001505 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001506 /* assumes msg->sol points to the first char, and msg->sov
1507 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 */
1509 if (likely(!HTTP_IS_CRLF(*ptr)))
1510 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001511
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001512 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 /* Note: we could also copy eol into ->eoh so that we have the
1514 * real header end in case it ends with lots of LWS, but is this
1515 * really needed ?
1516 */
1517 if (likely(*ptr == '\r'))
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1519 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001522 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 EXPECT_LF_HERE(ptr, http_msg_invalid);
1524 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001527 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1529 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001530 for (; buf->p + msg->eol < ptr; msg->eol++)
1531 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 goto http_msg_hdr_val;
1533 }
1534 http_msg_complete_header:
1535 /*
1536 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001537 * Assumes msg->sol points to the first char, msg->sov points
1538 * to the first character of the value and msg->eol to the
1539 * first CR or LF so we know how the line ends. We insert last
1540 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001542 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 idx, idx->tail) < 0))
1544 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001545
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 if (likely(!HTTP_IS_CRLF(*ptr))) {
1548 goto http_msg_hdr_name;
1549 }
1550
1551 if (likely(*ptr == '\r'))
1552 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1553 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001554
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001556 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 /* Assumes msg->sol points to the first of either CR or LF */
1558 EXPECT_LF_HERE(ptr, http_msg_invalid);
1559 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001560 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001561 msg->eoh = msg->sol;
1562 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001563 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001565
1566 case HTTP_MSG_ERROR:
1567 /* this may only happen if we call http_msg_analyser() twice with an error */
1568 break;
1569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570#ifdef DEBUG_FULL
1571 default:
1572 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1573 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001574#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 }
1576 http_msg_ood:
1577 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001578 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001579 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 http_msg_invalid:
1583 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001584 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001585 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 return;
1587}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001588
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001589/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1590 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1591 * nothing is done and 1 is returned.
1592 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001593static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001594{
1595 int delta;
1596 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001597 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001598
1599 if (msg->sl.rq.v_l != 0)
1600 return 1;
1601
Willy Tarreau9b28e032012-10-12 23:49:43 +02001602 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001603 delta = 0;
1604
1605 if (msg->sl.rq.u_l == 0) {
1606 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001607 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001609 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001610 }
1611 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001612 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001613 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001615 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001616 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001617 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001618 NULL, NULL);
1619 if (unlikely(!cur_end))
1620 return 0;
1621
1622 /* we have a full HTTP/1.0 request now and we know that
1623 * we have either a CR or an LF at <ptr>.
1624 */
1625 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1626 return 1;
1627}
1628
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001629/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001630 * and "keep-alive" values. If we already know that some headers may safely
1631 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001632 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1633 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001634 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001635 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1636 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1637 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001638 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001639 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001640void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001641{
Willy Tarreau5b154472009-12-21 20:11:07 +01001642 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001643 const char *hdr_val = "Connection";
1644 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001645
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001646 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001647 return;
1648
Willy Tarreau88d349d2010-01-25 12:15:43 +01001649 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1650 hdr_val = "Proxy-Connection";
1651 hdr_len = 16;
1652 }
1653
Willy Tarreau5b154472009-12-21 20:11:07 +01001654 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001655 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001656 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001657 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1658 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001659 if (to_del & 2)
1660 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001661 else
1662 txn->flags |= TX_CON_KAL_SET;
1663 }
1664 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1665 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001666 if (to_del & 1)
1667 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001668 else
1669 txn->flags |= TX_CON_CLO_SET;
1670 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001671 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1672 txn->flags |= TX_HDR_CONN_UPG;
1673 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001674 }
1675
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001676 txn->flags |= TX_HDR_CONN_PRS;
1677 return;
1678}
Willy Tarreau5b154472009-12-21 20:11:07 +01001679
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001680/* Apply desired changes on the Connection: header. Values may be removed and/or
1681 * added depending on the <wanted> flags, which are exclusively composed of
1682 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1683 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1684 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001685void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001686{
1687 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001688 const char *hdr_val = "Connection";
1689 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001690
1691 ctx.idx = 0;
1692
Willy Tarreau88d349d2010-01-25 12:15:43 +01001693
1694 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1695 hdr_val = "Proxy-Connection";
1696 hdr_len = 16;
1697 }
1698
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001699 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001700 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001701 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1702 if (wanted & TX_CON_KAL_SET)
1703 txn->flags |= TX_CON_KAL_SET;
1704 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001705 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001706 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001707 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1708 if (wanted & TX_CON_CLO_SET)
1709 txn->flags |= TX_CON_CLO_SET;
1710 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001711 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001712 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001713 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001714
1715 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1716 return;
1717
1718 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1719 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001720 hdr_val = "Connection: close";
1721 hdr_len = 17;
1722 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1723 hdr_val = "Proxy-Connection: close";
1724 hdr_len = 23;
1725 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001726 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001727 }
1728
1729 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1730 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001731 hdr_val = "Connection: keep-alive";
1732 hdr_len = 22;
1733 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1734 hdr_val = "Proxy-Connection: keep-alive";
1735 hdr_len = 28;
1736 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001737 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001738 }
1739 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001740}
1741
Willy Tarreaua458b672012-03-05 11:17:50 +01001742/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001743 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001744 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001745 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001746 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001747 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001748static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001749{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001750 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001751 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001752 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001753 const char *end = buf->data + buf->size;
1754 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001755 unsigned int chunk = 0;
1756
1757 /* The chunk size is in the following form, though we are only
1758 * interested in the size and CRLF :
1759 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1760 */
1761 while (1) {
1762 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001763 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001764 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001765 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001766 if (c < 0) /* not a hex digit anymore */
1767 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001768 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001769 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001770 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001771 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001772 chunk = (chunk << 4) + c;
1773 }
1774
Willy Tarreaud98cf932009-12-27 22:54:55 +01001775 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001776 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001777 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001778
1779 while (http_is_spht[(unsigned char)*ptr]) {
1780 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001781 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001782 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001783 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001784 }
1785
Willy Tarreaud98cf932009-12-27 22:54:55 +01001786 /* Up to there, we know that at least one byte is present at *ptr. Check
1787 * for the end of chunk size.
1788 */
1789 while (1) {
1790 if (likely(HTTP_IS_CRLF(*ptr))) {
1791 /* we now have a CR or an LF at ptr */
1792 if (likely(*ptr == '\r')) {
1793 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001794 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001795 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001796 return 0;
1797 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001798
Willy Tarreaud98cf932009-12-27 22:54:55 +01001799 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001800 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001801 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001802 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001803 /* done */
1804 break;
1805 }
1806 else if (*ptr == ';') {
1807 /* chunk extension, ends at next CRLF */
1808 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001809 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001810 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001811 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001812
1813 while (!HTTP_IS_CRLF(*ptr)) {
1814 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001815 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001816 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817 return 0;
1818 }
1819 /* we have a CRLF now, loop above */
1820 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001821 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001823 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001824 }
1825
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001827 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001828 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001829 */
Willy Tarreaub8ffd372012-09-27 15:08:56 +02001830 if (ptr < ptr_old)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001831 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001832 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001833 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001834 msg->chunk_len = chunk;
1835 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001837 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001838 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001839 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001840 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001841}
1842
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001843/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001844 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001845 * the trailers is found, it is automatically scheduled to be forwarded,
1846 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1847 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001848 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001849 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001850 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001851 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1852 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001853 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001854 * matches the length of trailers already parsed and not forwarded. It is also
1855 * important to note that this function is designed to be able to parse wrapped
1856 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001857 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001858static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001860 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001861
Willy Tarreaua458b672012-03-05 11:17:50 +01001862 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001864 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001865 const char *ptr = b_ptr(buf, msg->next);
1866 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001867 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001868
1869 /* scan current line and stop at LF or CRLF */
1870 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001871 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001872 return 0;
1873
1874 if (*ptr == '\n') {
1875 if (!p1)
1876 p1 = ptr;
1877 p2 = ptr;
1878 break;
1879 }
1880
1881 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001882 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001883 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001884 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001885 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 p1 = ptr;
1887 }
1888
1889 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001890 if (ptr >= buf->data + buf->size)
1891 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001892 }
1893
1894 /* after LF; point to beginning of next line */
1895 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001896 if (p2 >= buf->data + buf->size)
1897 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001898
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001899 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001900 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001901 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001902
1903 /* schedule this line for forwarding */
1904 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001905 if (msg->sov >= buf->size)
1906 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001907
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001908 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001909 /* LF/CRLF at beginning of line => end of trailers at p2.
1910 * Everything was scheduled for forwarding, there's nothing
1911 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001912 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001913 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001914 msg->msg_state = HTTP_MSG_DONE;
1915 return 1;
1916 }
1917 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001918 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 }
1920}
1921
Willy Tarreau54d23df2012-10-25 19:04:45 +02001922/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001923 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001924 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001925 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001926 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1927 * not enough data are available, the function does not change anything and
1928 * returns zero. If a parse error is encountered, the function returns < 0 and
1929 * does not change anything. Note: this function is designed to parse wrapped
1930 * CRLF at the end of the buffer.
1931 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001932static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001934 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001935 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001936 int bytes;
1937
1938 /* NB: we'll check data availabilty at the end. It's not a
1939 * problem because whatever we match first will be checked
1940 * against the correct length.
1941 */
1942 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001943 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001944 if (*ptr == '\r') {
1945 bytes++;
1946 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001947 if (ptr >= buf->data + buf->size)
1948 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001949 }
1950
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001951 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 return 0;
1953
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001954 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001955 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001956 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001957 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958
1959 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001960 if (ptr >= buf->data + buf->size)
1961 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001962 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1963 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001964 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001965 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1966 return 1;
1967}
Willy Tarreau5b154472009-12-21 20:11:07 +01001968
William Lallemand82fe75c2012-10-23 10:25:10 +02001969
1970/*
1971 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001972 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001973int select_compression_request_header(struct session *s, struct buffer *req)
1974{
1975 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001976 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001977 struct hdr_ctx ctx;
1978 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001979 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001980
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001981 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1982 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001983 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1984 */
1985 ctx.idx = 0;
1986 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
1987 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001988 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
1989 (ctx.vlen < 31 ||
1990 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
1991 ctx.line[ctx.val + 30] < '6' ||
1992 (ctx.line[ctx.val + 30] == '6' &&
1993 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
1994 s->comp_algo = NULL;
1995 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02001996 }
1997
William Lallemand82fe75c2012-10-23 10:25:10 +02001998 ctx.idx = 0;
1999 /* no compression when Cache-Control: no-transform found */
2000 while (http_find_header2("Cache-Control", 13, req->p, &txn->hdr_idx, &ctx)) {
2001 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) {
2002 s->comp_algo = NULL;
2003 return 0;
2004 }
2005 }
2006
2007 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002008 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002009 ctx.idx = 0;
2010 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002011 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002012 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2013 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002014
2015 /* remove all occurrences of the header when "compression offload" is set */
2016
2017 if ((s->be->comp && s->be->comp->offload) ||
2018 (s->fe->comp && s->fe->comp->offload)) {
2019 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2020 ctx.idx = 0;
2021 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2022 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2023 }
2024 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002025 return 1;
2026 }
2027 }
2028 }
2029 }
2030
2031 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002032 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2033 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002034 if (comp_algo->add_data == identity_add_data) {
2035 s->comp_algo = comp_algo;
2036 return 1;
2037 }
2038 }
2039 }
2040
2041 s->comp_algo = NULL;
2042
2043 return 0;
2044}
2045
2046/*
2047 * Selects a comression algorithm depending of the server response.
2048 */
2049int select_compression_response_header(struct session *s, struct buffer *res)
2050{
2051 struct http_txn *txn = &s->txn;
2052 struct http_msg *msg = &txn->rsp;
2053 struct hdr_ctx ctx;
2054 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002055
2056 /* no common compression algorithm was found in request header */
2057 if (s->comp_algo == NULL)
2058 goto fail;
2059
2060 /* HTTP < 1.1 should not be compressed */
2061 if (!(msg->flags & HTTP_MSGF_VER_11))
2062 goto fail;
2063
William Lallemand82fe75c2012-10-23 10:25:10 +02002064 ctx.idx = 0;
2065
2066 /* Content-Length is null */
2067 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2068 goto fail;
2069
2070 /* content is already compressed */
2071 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2072 goto fail;
2073
2074 comp_type = NULL;
2075
2076 /* if there was a compression content-type option in the backend or the frontend
2077 * The backend have priority.
2078 */
2079 if ((s->be->comp && (comp_type = s->be->comp->types)) || (s->fe->comp && (comp_type = s->fe->comp->types))) {
2080 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2081 for (; comp_type; comp_type = comp_type->next) {
2082 if (strncmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
2083 /* this Content-Type should be compressed */
2084 break;
2085 }
William Lallemandc04ca582012-11-12 13:56:25 +01002086 } else {
2087 /* there is no Content-Type header */
2088 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002089 }
2090 /* this Content-Type should not be compressed */
2091 if (comp_type == NULL)
2092 goto fail;
2093 }
2094
2095 ctx.idx = 0;
2096
William Lallemandd85f9172012-11-09 17:05:39 +01002097 /* limit compression rate */
2098 if (global.comp_rate_lim > 0)
2099 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2100 goto fail;
2101
William Lallemand4c49fae2012-11-07 15:00:23 +01002102 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002103 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002104 goto fail;
2105
William Lallemandec3e3892012-11-12 17:02:18 +01002106 s->flags |= SN_COMP_READY;
2107
William Lallemandf3747832012-11-09 12:33:10 +01002108 s->comp_ctx.cur_lvl = global.tune.comp_maxlevel;
2109
William Lallemand82fe75c2012-10-23 10:25:10 +02002110 /* remove Content-Length header */
2111 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2112 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2113
2114 /* add Transfer-Encoding header */
2115 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2116 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2117
2118 /*
2119 * Add Content-Encoding header when it's not identity encoding.
2120 * RFC 2616 : Identity encoding: This content-coding is used only in the
2121 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2122 * header.
2123 */
2124 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002125 trash.len = 18;
2126 memcpy(trash.str, "Content-Encoding: ", trash.len);
2127 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2128 trash.len += s->comp_algo->name_len;
2129 trash.str[trash.len] = '\0';
2130 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002131 }
2132
William Lallemand82fe75c2012-10-23 10:25:10 +02002133 return 1;
2134
2135fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002136 if (s->flags & SN_COMP_READY)
William Lallemand1c2d6222012-10-30 15:52:53 +01002137 s->comp_algo->end(&s->comp_ctx);
Willy Tarreaub97b6192012-11-19 14:55:02 +01002138 s->comp_algo = NULL;
2139 s->flags &= ~SN_COMP_READY;
William Lallemand82fe75c2012-10-23 10:25:10 +02002140 return 0;
2141}
2142
2143
Willy Tarreaud787e662009-07-07 10:14:51 +02002144/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2145 * processing can continue on next analysers, or zero if it either needs more
2146 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2147 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2148 * when it has nothing left to do, and may remove any analyser when it wants to
2149 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002150 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002151int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002152{
Willy Tarreau59234e92008-11-30 23:51:27 +01002153 /*
2154 * We will parse the partial (or complete) lines.
2155 * We will check the request syntax, and also join multi-line
2156 * headers. An index of all the lines will be elaborated while
2157 * parsing.
2158 *
2159 * For the parsing, we use a 28 states FSM.
2160 *
2161 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002162 * req->buf->p = beginning of request
2163 * req->buf->p + msg->eoh = end of processed headers / start of current one
2164 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002165 * msg->eol = end of current header or line (LF or CRLF)
2166 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002167 *
2168 * At end of parsing, we may perform a capture of the error (if any), and
2169 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2170 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2171 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002172 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002173
Willy Tarreau59234e92008-11-30 23:51:27 +01002174 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002175 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002176 struct http_txn *txn = &s->txn;
2177 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002178 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002179
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002180 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 +01002181 now_ms, __FUNCTION__,
2182 s,
2183 req,
2184 req->rex, req->wex,
2185 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002186 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002187 req->analysers);
2188
Willy Tarreau52a0c602009-08-16 22:45:38 +02002189 /* we're speaking HTTP here, so let's speak HTTP to the client */
2190 s->srv_error = http_return_srv_error;
2191
Willy Tarreau83e3af02009-12-28 17:39:57 +01002192 /* There's a protected area at the end of the buffer for rewriting
2193 * purposes. We don't want to start to parse the request if the
2194 * protected area is affected, because we may have to move processed
2195 * data later, which is much more complicated.
2196 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002197 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002198 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002199 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002200 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2201 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2202 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002203 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002204 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002205 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002206 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002207 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002208 return 0;
2209 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002210 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2211 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2212 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002213 }
2214
Willy Tarreau065e8332010-01-08 00:30:20 +01002215 /* Note that we have the same problem with the response ; we
2216 * may want to send a redirect, error or anything which requires
2217 * some spare space. So we'll ensure that we have at least
2218 * maxrewrite bytes available in the response buffer before
2219 * processing that one. This will only affect pipelined
2220 * keep-alive requests.
2221 */
2222 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002223 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002224 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2225 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2226 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002227 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002228 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002229 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002230 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002231 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002232 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002233 return 0;
2234 }
2235 }
2236
Willy Tarreau9b28e032012-10-12 23:49:43 +02002237 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002238 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002239 }
2240
Willy Tarreau59234e92008-11-30 23:51:27 +01002241 /* 1: we might have to print this header in debug mode */
2242 if (unlikely((global.mode & MODE_DEBUG) &&
2243 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002244 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002245 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002246
Willy Tarreau9b28e032012-10-12 23:49:43 +02002247 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002248 /* this is a bit complex : in case of error on the request line,
2249 * we know that rq.l is still zero, so we display only the part
2250 * up to the end of the line (truncated by debug_hdr).
2251 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002252 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002253 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002254
Willy Tarreau59234e92008-11-30 23:51:27 +01002255 sol += hdr_idx_first_pos(&txn->hdr_idx);
2256 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002257
Willy Tarreau59234e92008-11-30 23:51:27 +01002258 while (cur_idx) {
2259 eol = sol + txn->hdr_idx.v[cur_idx].len;
2260 debug_hdr("clihdr", s, sol, eol);
2261 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2262 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002263 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002264 }
2265
Willy Tarreau58f10d72006-12-04 02:26:12 +01002266
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 /*
2268 * Now we quickly check if we have found a full valid request.
2269 * If not so, we check the FD and buffer states before leaving.
2270 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002271 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002272 * requests are checked first. When waiting for a second request
2273 * on a keep-alive session, if we encounter and error, close, t/o,
2274 * we note the error in the session flags but don't set any state.
2275 * Since the error will be noted there, it will not be counted by
2276 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002277 * Last, we may increase some tracked counters' http request errors on
2278 * the cases that are deliberately the client's fault. For instance,
2279 * a timeout or connection reset is not counted as an error. However
2280 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002281 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002282
Willy Tarreau655dce92009-11-08 13:10:58 +01002283 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002284 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002286 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002287 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002288 session_inc_http_req_ctr(s);
2289 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002290 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002291 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002292 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002293
Willy Tarreau59234e92008-11-30 23:51:27 +01002294 /* 1: Since we are in header mode, if there's no space
2295 * left for headers, we won't be able to free more
2296 * later, so the session will never terminate. We
2297 * must terminate it now.
2298 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002299 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002300 /* FIXME: check if URI is set and return Status
2301 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002302 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002303 session_inc_http_req_ctr(s);
2304 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002305 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002306 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002307 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 goto return_bad_req;
2309 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002310
Willy Tarreau59234e92008-11-30 23:51:27 +01002311 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002312 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002313 if (!(s->flags & SN_ERR_MASK))
2314 s->flags |= SN_ERR_CLICL;
2315
Willy Tarreaufcffa692010-01-10 14:21:19 +01002316 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002317 goto failed_keep_alive;
2318
Willy Tarreau59234e92008-11-30 23:51:27 +01002319 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002320 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002321 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002322 session_inc_http_err_ctr(s);
2323 }
2324
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 msg->msg_state = HTTP_MSG_ERROR;
2326 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002327
Willy Tarreauda7ff642010-06-23 11:44:09 +02002328 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002329 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002330 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002331 if (s->listener->counters)
2332 s->listener->counters->failed_req++;
2333
Willy Tarreau59234e92008-11-30 23:51:27 +01002334 if (!(s->flags & SN_FINST_MASK))
2335 s->flags |= SN_FINST_R;
2336 return 0;
2337 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002338
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002340 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002341 if (!(s->flags & SN_ERR_MASK))
2342 s->flags |= SN_ERR_CLITO;
2343
Willy Tarreaufcffa692010-01-10 14:21:19 +01002344 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002345 goto failed_keep_alive;
2346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002348 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002349 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002350 session_inc_http_err_ctr(s);
2351 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002353 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 msg->msg_state = HTTP_MSG_ERROR;
2355 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002356
Willy Tarreauda7ff642010-06-23 11:44:09 +02002357 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002358 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002359 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002360 if (s->listener->counters)
2361 s->listener->counters->failed_req++;
2362
Willy Tarreau59234e92008-11-30 23:51:27 +01002363 if (!(s->flags & SN_FINST_MASK))
2364 s->flags |= SN_FINST_R;
2365 return 0;
2366 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002367
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002369 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002370 if (!(s->flags & SN_ERR_MASK))
2371 s->flags |= SN_ERR_CLICL;
2372
Willy Tarreaufcffa692010-01-10 14:21:19 +01002373 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002374 goto failed_keep_alive;
2375
Willy Tarreau4076a152009-04-02 15:18:36 +02002376 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002377 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002378 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002379 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002380 msg->msg_state = HTTP_MSG_ERROR;
2381 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002382
Willy Tarreauda7ff642010-06-23 11:44:09 +02002383 session_inc_http_err_ctr(s);
2384 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002385 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002386 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002387 if (s->listener->counters)
2388 s->listener->counters->failed_req++;
2389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 if (!(s->flags & SN_FINST_MASK))
2391 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002392 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002393 }
2394
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002395 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002396 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2397 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002398#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002399 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002400 /* We need more data, we have to re-enable quick-ack in case we
2401 * previously disabled it, otherwise we might cause the client
2402 * to delay next data.
2403 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002404 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002405 }
2406#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002407
Willy Tarreaufcffa692010-01-10 14:21:19 +01002408 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2409 /* If the client starts to talk, let's fall back to
2410 * request timeout processing.
2411 */
2412 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002413 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002414 }
2415
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002417 if (!tick_isset(req->analyse_exp)) {
2418 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2419 (txn->flags & TX_WAIT_NEXT_RQ) &&
2420 tick_isset(s->be->timeout.httpka))
2421 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2422 else
2423 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2424 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002425
Willy Tarreau59234e92008-11-30 23:51:27 +01002426 /* we're not ready yet */
2427 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002428
2429 failed_keep_alive:
2430 /* Here we process low-level errors for keep-alive requests. In
2431 * short, if the request is not the first one and it experiences
2432 * a timeout, read error or shutdown, we just silently close so
2433 * that the client can try again.
2434 */
2435 txn->status = 0;
2436 msg->msg_state = HTTP_MSG_RQBEFORE;
2437 req->analysers = 0;
2438 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002439 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002440 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002441 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002443
Willy Tarreaud787e662009-07-07 10:14:51 +02002444 /* OK now we have a complete HTTP request with indexed headers. Let's
2445 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002446 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002447 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002448 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002449 * byte after the last LF. msg->sov points to the first byte of data.
2450 * msg->eol cannot be trusted because it may have been left uninitialized
2451 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002452 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002453
Willy Tarreauda7ff642010-06-23 11:44:09 +02002454 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002455 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2456
Willy Tarreaub16a5742010-01-10 14:46:16 +01002457 if (txn->flags & TX_WAIT_NEXT_RQ) {
2458 /* kill the pending keep-alive timeout */
2459 txn->flags &= ~TX_WAIT_NEXT_RQ;
2460 req->analyse_exp = TICK_ETERNITY;
2461 }
2462
2463
Willy Tarreaud787e662009-07-07 10:14:51 +02002464 /* Maybe we found in invalid header name while we were configured not
2465 * to block on that, so we have to capture it now.
2466 */
2467 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002468 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002469
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 /*
2471 * 1: identify the method
2472 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002473 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002474
2475 /* we can make use of server redirect on GET and HEAD */
2476 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2477 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002478
Willy Tarreau59234e92008-11-30 23:51:27 +01002479 /*
2480 * 2: check if the URI matches the monitor_uri.
2481 * We have to do this for every request which gets in, because
2482 * the monitor-uri is defined by the frontend.
2483 */
2484 if (unlikely((s->fe->monitor_uri_len != 0) &&
2485 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002486 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002487 s->fe->monitor_uri,
2488 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002489 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002490 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002491 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002492 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002493
Willy Tarreau59234e92008-11-30 23:51:27 +01002494 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002495 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002496
Willy Tarreau59234e92008-11-30 23:51:27 +01002497 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002498 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002499 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002500
Willy Tarreau59234e92008-11-30 23:51:27 +01002501 ret = acl_pass(ret);
2502 if (cond->pol == ACL_COND_UNLESS)
2503 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002504
Willy Tarreau59234e92008-11-30 23:51:27 +01002505 if (ret) {
2506 /* we fail this request, let's return 503 service unavail */
2507 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002508 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002509 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002510 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002511 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 /* nothing to fail, let's reply normaly */
2514 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002515 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002516 goto return_prx_cond;
2517 }
2518
2519 /*
2520 * 3: Maybe we have to copy the original REQURI for the logs ?
2521 * Note: we cannot log anymore if the request has been
2522 * classified as invalid.
2523 */
2524 if (unlikely(s->logs.logwait & LW_REQ)) {
2525 /* we have a complete HTTP request that we must log */
2526 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2527 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002528
Willy Tarreau59234e92008-11-30 23:51:27 +01002529 if (urilen >= REQURI_LEN)
2530 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002531 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002532 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002533
Willy Tarreau59234e92008-11-30 23:51:27 +01002534 if (!(s->logs.logwait &= ~LW_REQ))
2535 s->do_log(s);
2536 } else {
2537 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002538 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002539 }
Willy Tarreau06619262006-12-17 08:37:22 +01002540
William Lallemanda73203e2012-03-12 12:48:57 +01002541 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2542 s->unique_id = pool_alloc2(pool2_uniqueid);
2543 }
2544
Willy Tarreau59234e92008-11-30 23:51:27 +01002545 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002546 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002547 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002548
Willy Tarreau5b154472009-12-21 20:11:07 +01002549 /* ... and check if the request is HTTP/1.1 or above */
2550 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002551 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2552 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2553 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002554 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002555
2556 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002557 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002558
Willy Tarreau88d349d2010-01-25 12:15:43 +01002559 /* if the frontend has "option http-use-proxy-header", we'll check if
2560 * we have what looks like a proxied connection instead of a connection,
2561 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2562 * Note that this is *not* RFC-compliant, however browsers and proxies
2563 * happen to do that despite being non-standard :-(
2564 * We consider that a request not beginning with either '/' or '*' is
2565 * a proxied connection, which covers both "scheme://location" and
2566 * CONNECT ip:port.
2567 */
2568 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002569 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002570 txn->flags |= TX_USE_PX_CONN;
2571
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002572 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002573 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002574
Willy Tarreau59234e92008-11-30 23:51:27 +01002575 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002576 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002577 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002578 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002579
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002580 /* 6: determine the transfer-length.
2581 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2582 * the presence of a message-body in a REQUEST and its transfer length
2583 * must be determined that way (in order of precedence) :
2584 * 1. The presence of a message-body in a request is signaled by the
2585 * inclusion of a Content-Length or Transfer-Encoding header field
2586 * in the request's header fields. When a request message contains
2587 * both a message-body of non-zero length and a method that does
2588 * not define any semantics for that request message-body, then an
2589 * origin server SHOULD either ignore the message-body or respond
2590 * with an appropriate error message (e.g., 413). A proxy or
2591 * gateway, when presented the same request, SHOULD either forward
2592 * the request inbound with the message- body or ignore the
2593 * message-body when determining a response.
2594 *
2595 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2596 * and the "chunked" transfer-coding (Section 6.2) is used, the
2597 * transfer-length is defined by the use of this transfer-coding.
2598 * If a Transfer-Encoding header field is present and the "chunked"
2599 * transfer-coding is not present, the transfer-length is defined
2600 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002601 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002602 * 3. If a Content-Length header field is present, its decimal value in
2603 * OCTETs represents both the entity-length and the transfer-length.
2604 * If a message is received with both a Transfer-Encoding header
2605 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002606 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002607 * 4. By the server closing the connection. (Closing the connection
2608 * cannot be used to indicate the end of a request body, since that
2609 * would leave no possibility for the server to send back a response.)
2610 *
2611 * Whenever a transfer-coding is applied to a message-body, the set of
2612 * transfer-codings MUST include "chunked", unless the message indicates
2613 * it is terminated by closing the connection. When the "chunked"
2614 * transfer-coding is used, it MUST be the last transfer-coding applied
2615 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002616 */
2617
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002618 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002619 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002620 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002621 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002622 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002623 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002624 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2625 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002626 /* bad transfer-encoding (chunked followed by something else) */
2627 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002628 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002629 break;
2630 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002631 }
2632
Willy Tarreau32b47f42009-10-18 20:55:02 +02002633 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002634 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002635 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002636 signed long long cl;
2637
Willy Tarreauad14f752011-09-02 20:33:27 +02002638 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002639 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002640 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002641 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002642
Willy Tarreauad14f752011-09-02 20:33:27 +02002643 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002644 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002645 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002646 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002647
Willy Tarreauad14f752011-09-02 20:33:27 +02002648 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002649 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002650 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002651 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002652
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002653 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002654 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002655 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002656 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002657
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002658 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002659 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002660 }
2661
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002662 /* bodyless requests have a known length */
2663 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002664 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002665
Willy Tarreaud787e662009-07-07 10:14:51 +02002666 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002667 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002668 req->analyse_exp = TICK_ETERNITY;
2669 return 1;
2670
2671 return_bad_req:
2672 /* We centralize bad requests processing here */
2673 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2674 /* we detected a parsing error. We want to archive this request
2675 * in the dedicated proxy area for later troubleshooting.
2676 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002677 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002678 }
2679
2680 txn->req.msg_state = HTTP_MSG_ERROR;
2681 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002682 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002683
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002684 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002685 if (s->listener->counters)
2686 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002687
2688 return_prx_cond:
2689 if (!(s->flags & SN_ERR_MASK))
2690 s->flags |= SN_ERR_PRXCOND;
2691 if (!(s->flags & SN_FINST_MASK))
2692 s->flags |= SN_FINST_R;
2693
2694 req->analysers = 0;
2695 req->analyse_exp = TICK_ETERNITY;
2696 return 0;
2697}
2698
Cyril Bonté70be45d2010-10-12 00:14:35 +02002699/* We reached the stats page through a POST request.
2700 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002701 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002702 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002703int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002704{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002705 struct proxy *px = NULL;
2706 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002707
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002708 char key[LINESIZE];
2709 int action = ST_ADM_ACTION_NONE;
2710 int reprocess = 0;
2711
2712 int total_servers = 0;
2713 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002714
2715 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002716 char *st_cur_param = NULL;
2717 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002718
Willy Tarreau9b28e032012-10-12 23:49:43 +02002719 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002720 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002721
2722 cur_param = next_param = end_params;
2723
Willy Tarreau9b28e032012-10-12 23:49:43 +02002724 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002725 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002726 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002727 return 1;
2728 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002729 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002730 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002731 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002732 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002733 }
2734
2735 *end_params = '\0';
2736
Willy Tarreau295a8372011-03-10 11:25:07 +01002737 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002738
2739 /*
2740 * Parse the parameters in reverse order to only store the last value.
2741 * From the html form, the backend and the action are at the end.
2742 */
2743 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002744 char *value;
2745 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002746
2747 cur_param--;
2748 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002749 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002750 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002751 poffset = (cur_param != first_param ? 1 : 0);
2752 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2753 if ((plen > 0) && (plen <= sizeof(key))) {
2754 strncpy(key, cur_param + poffset, plen);
2755 key[plen - 1] = '\0';
2756 } else {
2757 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2758 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002759 }
2760
2761 /* Parse the value */
2762 value = key;
2763 while (*value != '\0' && *value != '=') {
2764 value++;
2765 }
2766 if (*value == '=') {
2767 /* Ok, a value is found, we can mark the end of the key */
2768 *value++ = '\0';
2769 }
2770
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002771 if (!url_decode(key) || !url_decode(value))
2772 break;
2773
Cyril Bonté70be45d2010-10-12 00:14:35 +02002774 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002775 if (!px && (strcmp(key, "b") == 0)) {
2776 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2777 /* the backend name is unknown or ambiguous (duplicate names) */
2778 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2779 goto out;
2780 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002781 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002782 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002783 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002784 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002785 }
2786 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002787 action = ST_ADM_ACTION_ENABLE;
2788 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002789 else if (strcmp(value, "stop") == 0) {
2790 action = ST_ADM_ACTION_STOP;
2791 }
2792 else if (strcmp(value, "start") == 0) {
2793 action = ST_ADM_ACTION_START;
2794 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002795 else if (strcmp(value, "shutdown") == 0) {
2796 action = ST_ADM_ACTION_SHUTDOWN;
2797 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002798 else {
2799 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2800 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002801 }
2802 }
2803 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002804 if (!(px && action)) {
2805 /*
2806 * Indicates that we'll need to reprocess the parameters
2807 * as soon as backend and action are known
2808 */
2809 if (!reprocess) {
2810 st_cur_param = cur_param;
2811 st_next_param = next_param;
2812 }
2813 reprocess = 1;
2814 }
2815 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002816 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002817 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002818 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002819 /* Not already in maintenance, we can change the server state */
2820 sv->state |= SRV_MAINTAIN;
2821 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002822 altered_servers++;
2823 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002824 }
2825 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002826 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002827 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002828 /* Already in maintenance, we can change the server state */
2829 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002830 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002831 altered_servers++;
2832 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002833 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002834 break;
2835 case ST_ADM_ACTION_STOP:
2836 case ST_ADM_ACTION_START:
2837 if (action == ST_ADM_ACTION_START)
2838 sv->uweight = sv->iweight;
2839 else
2840 sv->uweight = 0;
2841
2842 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2843 /* we must take care of not pushing the server to full throttle during slow starts */
2844 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2845 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2846 else
2847 sv->eweight = BE_WEIGHT_SCALE;
2848 sv->eweight *= sv->uweight;
2849 } else {
2850 sv->eweight = sv->uweight;
2851 }
2852
2853 /* static LB algorithms are a bit harder to update */
2854 if (px->lbprm.update_server_eweight)
2855 px->lbprm.update_server_eweight(sv);
2856 else if (sv->eweight) {
2857 if (px->lbprm.set_server_status_up)
2858 px->lbprm.set_server_status_up(sv);
2859 }
2860 else {
2861 if (px->lbprm.set_server_status_down)
2862 px->lbprm.set_server_status_down(sv);
2863 }
2864 altered_servers++;
2865 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002866 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002867 case ST_ADM_ACTION_SHUTDOWN:
2868 if (px->state != PR_STSTOPPED) {
2869 struct session *sess, *sess_bck;
2870
2871 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2872 if (sess->srv_conn == sv)
2873 session_shutdown(sess, SN_ERR_KILLED);
2874
2875 altered_servers++;
2876 total_servers++;
2877 }
2878 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002879 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002880 } else {
2881 /* the server name is unknown or ambiguous (duplicate names) */
2882 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002883 }
2884 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002885 if (reprocess && px && action) {
2886 /* Now, we know the backend and the action chosen by the user.
2887 * We can safely restart from the first server parameter
2888 * to reprocess them
2889 */
2890 cur_param = st_cur_param;
2891 next_param = st_next_param;
2892 reprocess = 0;
2893 goto reprocess_servers;
2894 }
2895
Cyril Bonté70be45d2010-10-12 00:14:35 +02002896 next_param = cur_param;
2897 }
2898 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002899
2900 if (total_servers == 0) {
2901 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2902 }
2903 else if (altered_servers == 0) {
2904 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2905 }
2906 else if (altered_servers == total_servers) {
2907 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2908 }
2909 else {
2910 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2911 }
2912 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002913 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002914}
2915
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002916/* returns a pointer to the first rule which forbids access (deny or http_auth),
2917 * or NULL if everything's OK.
2918 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002919static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002920http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2921{
Willy Tarreauff011f22011-01-06 17:51:27 +01002922 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002923
Willy Tarreauff011f22011-01-06 17:51:27 +01002924 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002925 int ret = 1;
2926
Willy Tarreauff011f22011-01-06 17:51:27 +01002927 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002928 continue;
2929
2930 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002931 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002932 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002933 ret = acl_pass(ret);
2934
Willy Tarreauff011f22011-01-06 17:51:27 +01002935 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002936 ret = !ret;
2937 }
2938
2939 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002940 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002941 return NULL; /* no problem */
2942 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002943 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002944 }
2945 }
2946 return NULL;
2947}
2948
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002949/* This stream analyser runs all HTTP request processing which is common to
2950 * frontends and backends, which means blocking ACLs, filters, connection-close,
2951 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002952 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002953 * either needs more data or wants to immediately abort the request (eg: deny,
2954 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002955 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002956int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002957{
Willy Tarreaud787e662009-07-07 10:14:51 +02002958 struct http_txn *txn = &s->txn;
2959 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002960 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002961 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002962 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002963 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002964 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002965
Willy Tarreau655dce92009-11-08 13:10:58 +01002966 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002967 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002968 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002969 return 0;
2970 }
2971
Willy Tarreau3a816292009-07-07 10:55:49 +02002972 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002973 req->analyse_exp = TICK_ETERNITY;
2974
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002975 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 +02002976 now_ms, __FUNCTION__,
2977 s,
2978 req,
2979 req->rex, req->wex,
2980 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002981 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002982 req->analysers);
2983
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002984 /* first check whether we have some ACLs set to block this request */
2985 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002986 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002987
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002988 ret = acl_pass(ret);
2989 if (cond->pol == ACL_COND_UNLESS)
2990 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002991
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002992 if (ret) {
2993 txn->status = 403;
2994 /* let's log the request time */
2995 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002996 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002997 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002998 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002999 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003000 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003001
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003002 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003003 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003004
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003005 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003006 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003007 do_stats = stats_check_uri(s->rep->prod, txn, px);
3008 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003009 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 +01003010 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003011 else
3012 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003013
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003014 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003015 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003016 txn->status = 403;
3017 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003018 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003019 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003020 s->fe->fe_counters.denied_req++;
3021 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3022 s->be->be_counters.denied_req++;
3023 if (s->listener->counters)
3024 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003025 goto return_prx_cond;
3026 }
3027
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003028 /* try headers filters */
3029 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003030 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003031 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003032
Willy Tarreau59234e92008-11-30 23:51:27 +01003033 /* has the request been denied ? */
3034 if (txn->flags & TX_CLDENY) {
3035 /* no need to go further */
3036 txn->status = 403;
3037 /* let's log the request time */
3038 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003039 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003040 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003041 goto return_prx_cond;
3042 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003043
3044 /* When a connection is tarpitted, we use the tarpit timeout,
3045 * which may be the same as the connect timeout if unspecified.
3046 * If unset, then set it to zero because we really want it to
3047 * eventually expire. We build the tarpit as an analyser.
3048 */
3049 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003050 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003051 /* wipe the request out so that we can drop the connection early
3052 * if the client closes first.
3053 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003054 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003055 req->analysers = 0; /* remove switching rules etc... */
3056 req->analysers |= AN_REQ_HTTP_TARPIT;
3057 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3058 if (!req->analyse_exp)
3059 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003060 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003061 return 1;
3062 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003063 }
Willy Tarreau06619262006-12-17 08:37:22 +01003064
Willy Tarreau5b154472009-12-21 20:11:07 +01003065 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3066 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003067 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3068 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003069 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3070 * avoid to redo the same work if FE and BE have the same settings (common).
3071 * The method consists in checking if options changed between the two calls
3072 * (implying that either one is non-null, or one of them is non-null and we
3073 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003074 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003075
Willy Tarreaudc008c52010-02-01 16:20:08 +01003076 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3077 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3078 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3079 (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 +01003080 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003081
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003082 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3083 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003084 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003085 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3086 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003087 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003088 tmp = TX_CON_WANT_CLO;
3089
Willy Tarreau5b154472009-12-21 20:11:07 +01003090 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3091 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003092
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003093 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3094 /* parse the Connection header and possibly clean it */
3095 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003096 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003097 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3098 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003099 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003100 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003101 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003102 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003103 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003104
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003105 /* check if client or config asks for explicit close in KAL/SCL */
3106 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3107 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3108 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003109 (!(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 +01003110 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003111 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003112 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003113 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3114 }
Willy Tarreau78599912009-10-17 20:12:21 +02003115
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003116 /* we can be blocked here because the request needs to be authenticated,
3117 * either to pass or to access stats.
3118 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003119 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003120 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003121
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003122 if (!realm)
3123 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3124
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003125 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003126 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003127 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003128 /* on 401 we still count one error, because normal browsing
3129 * won't significantly increase the counter but brute force
3130 * attempts will.
3131 */
3132 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003133 goto return_prx_cond;
3134 }
3135
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003136 /* add request headers from the rule sets in the same order */
3137 list_for_each_entry(wl, &px->req_add, list) {
3138 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003139 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003140 ret = acl_pass(ret);
3141 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3142 ret = !ret;
3143 if (!ret)
3144 continue;
3145 }
3146
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003147 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003148 goto return_bad_req;
3149 }
3150
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003151 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003152 struct stats_admin_rule *stats_admin_rule;
3153
3154 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003155 * FIXME!!! that one is rather dangerous, we want to
3156 * make it follow standard rules (eg: clear req->analysers).
3157 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003158
Cyril Bonté474be412010-10-12 00:14:36 +02003159 /* now check whether we have some admin rules for this request */
3160 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3161 int ret = 1;
3162
3163 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003164 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Cyril Bonté474be412010-10-12 00:14:36 +02003165 ret = acl_pass(ret);
3166 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3167 ret = !ret;
3168 }
3169
3170 if (ret) {
3171 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003172 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003173 break;
3174 }
3175 }
3176
Cyril Bonté70be45d2010-10-12 00:14:35 +02003177 /* Was the status page requested with a POST ? */
3178 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003179 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003180 if (msg->msg_state < HTTP_MSG_100_SENT) {
3181 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3182 * send an HTTP/1.1 100 Continue intermediate response.
3183 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003184 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003185 struct hdr_ctx ctx;
3186 ctx.idx = 0;
3187 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003188 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01003189 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003190 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003191 }
3192 }
3193 msg->msg_state = HTTP_MSG_100_SENT;
3194 s->logs.tv_request = now; /* update the request timer to reflect full request */
3195 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003196 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003197 /* we need more data */
3198 req->analysers |= an_bit;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003199 channel_dont_connect(req);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003200 return 0;
3201 }
Cyril Bonté474be412010-10-12 00:14:36 +02003202 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003203 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003204 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003205 }
3206
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003207 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003208 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003209 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01003210 s->target = s->rep->prod->conn->target; // for logging only
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003211 s->rep->prod->conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01003212 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003213 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003214 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3215 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003216
3217 return 0;
3218
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003219 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003220
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003221 /* check whether we have some ACLs set to redirect this request */
3222 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003223 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003224
Willy Tarreauf285f542010-01-03 20:03:03 +01003225 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003226 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003227 ret = acl_pass(ret);
3228 if (rule->cond->pol == ACL_COND_UNLESS)
3229 ret = !ret;
3230 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003231
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003232 if (ret) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003233 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003234
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003235 /* build redirect message */
3236 switch(rule->code) {
3237 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003238 msg_fmt = HTTP_303;
3239 break;
3240 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003241 msg_fmt = HTTP_301;
3242 break;
3243 case 302:
3244 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003245 msg_fmt = HTTP_302;
3246 break;
3247 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003248
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003249 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003250 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003251
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003252 switch(rule->type) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003253 case REDIRECT_TYPE_SCHEME: {
3254 const char *path;
3255 const char *host;
3256 struct hdr_ctx ctx;
3257 int pathlen;
3258 int hostlen;
3259
3260 host = "";
3261 hostlen = 0;
3262 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003263 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003264 host = ctx.line + ctx.val;
3265 hostlen = ctx.vlen;
3266 }
3267
3268 path = http_get_path(txn);
3269 /* build message using path */
3270 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003271 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003272 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3273 int qs = 0;
3274 while (qs < pathlen) {
3275 if (path[qs] == '?') {
3276 pathlen = qs;
3277 break;
3278 }
3279 qs++;
3280 }
3281 }
3282 } else {
3283 path = "/";
3284 pathlen = 1;
3285 }
3286
3287 /* check if we can add scheme + "://" + host + path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003288 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003289 goto return_bad_req;
3290
3291 /* add scheme */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003292 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3293 trash.len += rule->rdr_len;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003294
3295 /* add "://" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003296 memcpy(trash.str + trash.len, "://", 3);
3297 trash.len += 3;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003298
3299 /* add host */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003300 memcpy(trash.str + trash.len, host, hostlen);
3301 trash.len += hostlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003302
3303 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003304 memcpy(trash.str + trash.len, path, pathlen);
3305 trash.len += pathlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003306
3307 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003308 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003309 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003310 if (trash.len > trash.size - 5)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003311 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003312 trash.str[trash.len] = '/';
3313 trash.len++;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003314 }
3315
3316 break;
3317 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003318 case REDIRECT_TYPE_PREFIX: {
3319 const char *path;
3320 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003321
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003322 path = http_get_path(txn);
3323 /* build message using path */
3324 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003325 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003326 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3327 int qs = 0;
3328 while (qs < pathlen) {
3329 if (path[qs] == '?') {
3330 pathlen = qs;
3331 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003332 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003333 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003334 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003335 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003336 } else {
3337 path = "/";
3338 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003339 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003340
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003341 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003342 goto return_bad_req;
3343
3344 /* add prefix. Note that if prefix == "/", we don't want to
3345 * add anything, otherwise it makes it hard for the user to
3346 * configure a self-redirection.
3347 */
3348 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003349 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3350 trash.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003351 }
3352
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003353 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003354 memcpy(trash.str + trash.len, path, pathlen);
3355 trash.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003356
3357 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003358 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003359 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003360 if (trash.len > trash.size - 5)
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003361 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003362 trash.str[trash.len] = '/';
3363 trash.len++;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003364 }
3365
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003366 break;
3367 }
3368 case REDIRECT_TYPE_LOCATION:
3369 default:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003370 if (trash.len + rule->rdr_len > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003371 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003372
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003373 /* add location */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003374 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3375 trash.len += rule->rdr_len;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003376 break;
3377 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003378
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003379 if (rule->cookie_len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003380 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3381 trash.len += 14;
3382 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3383 trash.len += rule->cookie_len;
3384 memcpy(trash.str + trash.len, "\r\n", 2);
3385 trash.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003386 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003387
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003388 /* add end of headers and the keep-alive/close status.
3389 * We may choose to set keep-alive if the Location begins
3390 * with a slash, because the client will come back to the
3391 * same server.
3392 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003393 txn->status = rule->code;
3394 /* let's log the request time */
3395 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003396
3397 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003398 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3399 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003400 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3401 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3402 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003403 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003404 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003405 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3406 trash.len += 30;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003407 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003408 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3409 trash.len += 24;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003410 }
Willy Tarreau75661452010-01-10 10:35:01 +01003411 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003412 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3413 trash.len += 4;
3414 bo_inject(req->prod->ob, trash.str, trash.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003415 /* "eat" the request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003416 bi_fast_delete(req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003417 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003418 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003419 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3420 txn->req.msg_state = HTTP_MSG_CLOSED;
3421 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003422 break;
3423 } else {
3424 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003425 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003426 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3427 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003428 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003429 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3430 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003431 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003432 stream_int_retnclose(req->prod, &trash);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003433 goto return_prx_cond;
3434 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003435 }
3436 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003437
Willy Tarreau2be39392010-01-03 17:24:51 +01003438 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3439 * If this happens, then the data will not come immediately, so we must
3440 * send all what we have without waiting. Note that due to the small gain
3441 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003442 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003443 * itself once used.
3444 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003445 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003446
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003447 /* that's OK for us now, let's move on to next analysers */
3448 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 return_bad_req:
3451 /* We centralize bad requests processing here */
3452 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3453 /* we detected a parsing error. We want to archive this request
3454 * in the dedicated proxy area for later troubleshooting.
3455 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003456 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003457 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003458
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003459 txn->req.msg_state = HTTP_MSG_ERROR;
3460 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003461 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003462
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003463 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003464 if (s->listener->counters)
3465 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003466
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003467 return_prx_cond:
3468 if (!(s->flags & SN_ERR_MASK))
3469 s->flags |= SN_ERR_PRXCOND;
3470 if (!(s->flags & SN_FINST_MASK))
3471 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003472
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003473 req->analysers = 0;
3474 req->analyse_exp = TICK_ETERNITY;
3475 return 0;
3476}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003477
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003478/* This function performs all the processing enabled for the current request.
3479 * It returns 1 if the processing can continue on next analysers, or zero if it
3480 * needs more data, encounters an error, or wants to immediately abort the
3481 * request. It relies on buffers flags, and updates s->req->analysers.
3482 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003483int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003484{
3485 struct http_txn *txn = &s->txn;
3486 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003487
Willy Tarreau655dce92009-11-08 13:10:58 +01003488 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003489 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003490 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003491 return 0;
3492 }
3493
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003494 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 +02003495 now_ms, __FUNCTION__,
3496 s,
3497 req,
3498 req->rex, req->wex,
3499 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003500 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003501 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003502
William Lallemand82fe75c2012-10-23 10:25:10 +02003503 if (s->fe->comp || s->be->comp)
3504 select_compression_request_header(s, req->buf);
3505
Willy Tarreau59234e92008-11-30 23:51:27 +01003506 /*
3507 * Right now, we know that we have processed the entire headers
3508 * and that unwanted requests have been filtered out. We can do
3509 * whatever we want with the remaining request. Also, now we
3510 * may have separate values for ->fe, ->be.
3511 */
Willy Tarreau06619262006-12-17 08:37:22 +01003512
Willy Tarreau59234e92008-11-30 23:51:27 +01003513 /*
3514 * If HTTP PROXY is set we simply get remote server address
3515 * parsing incoming request.
3516 */
3517 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003518 url2sa(req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->conn->addr.to);
Willy Tarreau59234e92008-11-30 23:51:27 +01003519 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003520
Willy Tarreau59234e92008-11-30 23:51:27 +01003521 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003522 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003523 * Note that doing so might move headers in the request, but
3524 * the fields will stay coherent and the URI will not move.
3525 * This should only be performed in the backend.
3526 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003527 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003528 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3529 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003530
Willy Tarreau59234e92008-11-30 23:51:27 +01003531 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003532 * 8: the appsession cookie was looked up very early in 1.2,
3533 * so let's do the same now.
3534 */
3535
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003536 /* It needs to look into the URI unless persistence must be ignored */
3537 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003538 get_srv_from_appsession(s, req->buf->p + msg->sl.rq.u, msg->sl.rq.u_l);
Cyril Bontéb21570a2009-11-29 20:04:48 +01003539 }
3540
William Lallemanda73203e2012-03-12 12:48:57 +01003541 /* add unique-id if "header-unique-id" is specified */
3542
3543 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3544 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3545
3546 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003547 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3548 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003549 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003550 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003551 goto return_bad_req;
3552 }
3553
Cyril Bontéb21570a2009-11-29 20:04:48 +01003554 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003555 * 9: add X-Forwarded-For if either the frontend or the backend
3556 * asks for it.
3557 */
3558 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003559 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003560 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003561 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3562 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003563 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003564 /* The header is set to be added only if none is present
3565 * and we found it, so don't do anything.
3566 */
3567 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003568 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003569 /* Add an X-Forwarded-For header unless the source IP is
3570 * in the 'except' network range.
3571 */
3572 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003573 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->fe->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003574 != s->fe->except_net.s_addr) &&
3575 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003576 (((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr.s_addr & s->be->except_mask.s_addr)
Willy Tarreau59234e92008-11-30 23:51:27 +01003577 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003578 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003579 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003580 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003581
3582 /* Note: we rely on the backend to get the header name to be used for
3583 * x-forwarded-for, because the header is really meant for the backends.
3584 * However, if the backend did not specify any option, we have to rely
3585 * on the frontend's header name.
3586 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003587 if (s->be->fwdfor_hdr_len) {
3588 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003589 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003590 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003591 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003592 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003593 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003594 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003595
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003596 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003597 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003598 }
3599 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003600 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003601 /* FIXME: for the sake of completeness, we should also support
3602 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003603 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003604 int len;
3605 char pn[INET6_ADDRSTRLEN];
3606 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003607 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003608 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003609
Willy Tarreau59234e92008-11-30 23:51:27 +01003610 /* Note: we rely on the backend to get the header name to be used for
3611 * x-forwarded-for, because the header is really meant for the backends.
3612 * However, if the backend did not specify any option, we have to rely
3613 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003614 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003615 if (s->be->fwdfor_hdr_len) {
3616 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003617 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003618 } else {
3619 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003620 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003621 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003622 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003623
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003624 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003625 goto return_bad_req;
3626 }
3627 }
3628
3629 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003630 * 10: add X-Original-To if either the frontend or the backend
3631 * asks for it.
3632 */
3633 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3634
3635 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003636 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003637 /* Add an X-Original-To header unless the destination IP is
3638 * in the 'except' network range.
3639 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003640 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003641
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003642 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003643 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003644 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003645 != s->fe->except_to.s_addr) &&
3646 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003647 (((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr)
Emeric Brun5bd86a82010-10-22 17:23:04 +02003648 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003649 int len;
3650 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003651 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003652
3653 /* Note: we rely on the backend to get the header name to be used for
3654 * x-original-to, because the header is really meant for the backends.
3655 * However, if the backend did not specify any option, we have to rely
3656 * on the frontend's header name.
3657 */
3658 if (s->be->orgto_hdr_len) {
3659 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003660 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003661 } else {
3662 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003663 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003664 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003665 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003666
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003667 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003668 goto return_bad_req;
3669 }
3670 }
3671 }
3672
Willy Tarreau50fc7772012-11-11 22:19:57 +01003673 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3674 * If an "Upgrade" token is found, the header is left untouched in order not to have
3675 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3676 * "Upgrade" is present in the Connection header.
3677 */
3678 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3679 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3680 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003681 unsigned int want_flags = 0;
3682
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003683 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003684 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3685 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3686 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003687 want_flags |= TX_CON_CLO_SET;
3688 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003689 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3690 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3691 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003692 want_flags |= TX_CON_KAL_SET;
3693 }
3694
3695 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003696 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003697 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003698
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003699
Willy Tarreau522d6c02009-12-06 18:49:18 +01003700 /* If we have no server assigned yet and we're balancing on url_param
3701 * with a POST request, we may be interested in checking the body for
3702 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003703 */
3704 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3705 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003706 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003707 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003708 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003709 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003710 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003711
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003712 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003713 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003714#ifdef TCP_QUICKACK
3715 /* We expect some data from the client. Unless we know for sure
3716 * we already have a full request, we have to re-enable quick-ack
3717 * in case we previously disabled it, otherwise we might cause
3718 * the client to delay further data.
3719 */
3720 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003721 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003722 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003723 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003724#endif
3725 }
Willy Tarreau03945942009-12-22 16:50:27 +01003726
Willy Tarreau59234e92008-11-30 23:51:27 +01003727 /*************************************************************
3728 * OK, that's finished for the headers. We have done what we *
3729 * could. Let's switch to the DATA state. *
3730 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003731 req->analyse_exp = TICK_ETERNITY;
3732 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003733
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003734 /* if the server closes the connection, we want to immediately react
3735 * and close the socket to save packets and syscalls.
3736 */
3737 req->cons->flags |= SI_FL_NOHALF;
3738
Willy Tarreau59234e92008-11-30 23:51:27 +01003739 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003740 /* OK let's go on with the BODY now */
3741 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003742
Willy Tarreau59234e92008-11-30 23:51:27 +01003743 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003744 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003745 /* we detected a parsing error. We want to archive this request
3746 * in the dedicated proxy area for later troubleshooting.
3747 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003748 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003749 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003750
Willy Tarreau59234e92008-11-30 23:51:27 +01003751 txn->req.msg_state = HTTP_MSG_ERROR;
3752 txn->status = 400;
3753 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003754 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003755
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003756 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003757 if (s->listener->counters)
3758 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003759
Willy Tarreau59234e92008-11-30 23:51:27 +01003760 if (!(s->flags & SN_ERR_MASK))
3761 s->flags |= SN_ERR_PRXCOND;
3762 if (!(s->flags & SN_FINST_MASK))
3763 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003764 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003765}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003766
Willy Tarreau60b85b02008-11-30 23:28:40 +01003767/* This function is an analyser which processes the HTTP tarpit. It always
3768 * returns zero, at the beginning because it prevents any other processing
3769 * from occurring, and at the end because it terminates the request.
3770 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003771int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003772{
3773 struct http_txn *txn = &s->txn;
3774
3775 /* This connection is being tarpitted. The CLIENT side has
3776 * already set the connect expiration date to the right
3777 * timeout. We just have to check that the client is still
3778 * there and that the timeout has not expired.
3779 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003780 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003781 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003782 !tick_is_expired(req->analyse_exp, now_ms))
3783 return 0;
3784
3785 /* We will set the queue timer to the time spent, just for
3786 * logging purposes. We fake a 500 server error, so that the
3787 * attacker will not suspect his connection has been tarpitted.
3788 * It will not cause trouble to the logs because we can exclude
3789 * the tarpitted connections by filtering on the 'PT' status flags.
3790 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003791 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3792
3793 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003794 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003795 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003796
3797 req->analysers = 0;
3798 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003799
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003800 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003801 if (s->listener->counters)
3802 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003803
Willy Tarreau60b85b02008-11-30 23:28:40 +01003804 if (!(s->flags & SN_ERR_MASK))
3805 s->flags |= SN_ERR_PRXCOND;
3806 if (!(s->flags & SN_FINST_MASK))
3807 s->flags |= SN_FINST_T;
3808 return 0;
3809}
3810
Willy Tarreaud34af782008-11-30 23:36:37 +01003811/* This function is an analyser which processes the HTTP request body. It looks
3812 * for parameters to be used for the load balancing algorithm (url_param). It
3813 * must only be called after the standard HTTP request processing has occurred,
3814 * because it expects the request to be parsed. It returns zero if it needs to
3815 * read more data, or 1 once it has completed its analysis.
3816 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003817int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003818{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003819 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003820 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003821 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003822
3823 /* We have to parse the HTTP request body to find any required data.
3824 * "balance url_param check_post" should have been the only way to get
3825 * into this. We were brought here after HTTP header analysis, so all
3826 * related structures are ready.
3827 */
3828
Willy Tarreau522d6c02009-12-06 18:49:18 +01003829 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3830 goto missing_data;
3831
3832 if (msg->msg_state < HTTP_MSG_100_SENT) {
3833 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3834 * send an HTTP/1.1 100 Continue intermediate response.
3835 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003836 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003837 struct hdr_ctx ctx;
3838 ctx.idx = 0;
3839 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003840 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003841 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003842 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003843 }
3844 }
3845 msg->msg_state = HTTP_MSG_100_SENT;
3846 }
3847
3848 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003849 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02003850 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003851 * is still null. We must save the body in msg->next because it
3852 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003853 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003854 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003855
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003856 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003857 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3858 else
3859 msg->msg_state = HTTP_MSG_DATA;
3860 }
3861
3862 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003863 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003864 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003865 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003866 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003867 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003868
Willy Tarreau115acb92009-12-26 13:56:06 +01003869 if (!ret)
3870 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003871 else if (ret < 0) {
3872 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003873 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003874 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003875 }
3876
Willy Tarreaud98cf932009-12-27 22:54:55 +01003877 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003878 * We have the first data byte is in msg->sov. We're waiting for at
3879 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003880 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003881
Willy Tarreau124d9912011-03-01 20:30:48 +01003882 if (msg->body_len < limit)
3883 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003884
Willy Tarreau9b28e032012-10-12 23:49:43 +02003885 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003886 goto http_end;
3887
3888 missing_data:
3889 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003890 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02003891 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003892 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003893 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003894
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003895 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003896 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02003897 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003898
3899 if (!(s->flags & SN_ERR_MASK))
3900 s->flags |= SN_ERR_CLITO;
3901 if (!(s->flags & SN_FINST_MASK))
3902 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003903 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003904 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003905
3906 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003907 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003908 /* Not enough data. We'll re-use the http-request
3909 * timeout here. Ideally, we should set the timeout
3910 * relative to the accept() date. We just set the
3911 * request timeout once at the beginning of the
3912 * request.
3913 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003914 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003915 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003916 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003917 return 0;
3918 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003919
3920 http_end:
3921 /* The situation will not evolve, so let's give up on the analysis. */
3922 s->logs.tv_request = now; /* update the request timer to reflect full request */
3923 req->analysers &= ~an_bit;
3924 req->analyse_exp = TICK_ETERNITY;
3925 return 1;
3926
3927 return_bad_req: /* let's centralize all bad requests */
3928 txn->req.msg_state = HTTP_MSG_ERROR;
3929 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003930 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003931
Willy Tarreau79ebac62010-06-07 13:47:49 +02003932 if (!(s->flags & SN_ERR_MASK))
3933 s->flags |= SN_ERR_PRXCOND;
3934 if (!(s->flags & SN_FINST_MASK))
3935 s->flags |= SN_FINST_R;
3936
Willy Tarreau522d6c02009-12-06 18:49:18 +01003937 return_err_msg:
3938 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003939 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003940 if (s->listener->counters)
3941 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003942 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003943}
3944
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003945/* send a server's name with an outgoing request over an established connection.
3946 * Note: this function is designed to be called once the request has been scheduled
3947 * for being forwarded. This is the reason why it rewinds the buffer before
3948 * proceeding.
3949 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003950int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003951
3952 struct hdr_ctx ctx;
3953
Mark Lamourinec2247f02012-01-04 13:02:01 -05003954 char *hdr_name = be->server_id_hdr_name;
3955 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003956 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003957 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003958 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003959
William Lallemandd9e90662012-01-30 17:27:17 +01003960 ctx.idx = 0;
3961
Willy Tarreau9b28e032012-10-12 23:49:43 +02003962 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003963 if (old_o) {
3964 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003965 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003966 }
3967
Willy Tarreau9b28e032012-10-12 23:49:43 +02003968 old_i = chn->buf->i;
3969 while (http_find_header2(hdr_name, hdr_name_len, txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003970 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003971 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003972 }
3973
3974 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003975 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003976 memcpy(hdr_val, hdr_name, hdr_name_len);
3977 hdr_val += hdr_name_len;
3978 *hdr_val++ = ':';
3979 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003980 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
3981 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003982
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003983 if (old_o) {
3984 /* If this was a forwarded request, we must readjust the amount of
3985 * data to be forwarded in order to take into account the size
3986 * variations.
3987 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003988 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003989 }
3990
Mark Lamourinec2247f02012-01-04 13:02:01 -05003991 return 0;
3992}
3993
Willy Tarreau610ecce2010-01-04 21:15:02 +01003994/* Terminate current transaction and prepare a new one. This is very tricky
3995 * right now but it works.
3996 */
3997void http_end_txn_clean_session(struct session *s)
3998{
3999 /* FIXME: We need a more portable way of releasing a backend's and a
4000 * server's connections. We need a safer way to reinitialize buffer
4001 * flags. We also need a more accurate method for computing per-request
4002 * data.
4003 */
4004 http_silent_debug(__LINE__, s);
4005
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004006 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004007 si_shutr(s->req->cons);
4008 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009
4010 http_silent_debug(__LINE__, s);
4011
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004012 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004013 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004014 if (unlikely(s->srv_conn))
4015 sess_change_server(s, NULL);
4016 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017
4018 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4019 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004020 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004021
4022 if (s->txn.status) {
4023 int n;
4024
4025 n = s->txn.status / 100;
4026 if (n < 1 || n > 5)
4027 n = 0;
4028
4029 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004030 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004031
Willy Tarreau24657792010-02-26 10:30:28 +01004032 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004033 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004034 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004035 }
4036
4037 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004038 s->logs.bytes_in -= s->req->buf->i;
4039 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004040
4041 /* let's do a final log if we need it */
4042 if (s->logs.logwait &&
4043 !(s->flags & SN_MONITOR) &&
4044 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4045 s->do_log(s);
4046 }
4047
4048 s->logs.accept_date = date; /* user-visible date for logging */
4049 s->logs.tv_accept = now; /* corrected date for internal use */
4050 tv_zero(&s->logs.tv_request);
4051 s->logs.t_queue = -1;
4052 s->logs.t_connect = -1;
4053 s->logs.t_data = -1;
4054 s->logs.t_close = 0;
4055 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4056 s->logs.srv_queue_size = 0; /* we will get this number soon */
4057
Willy Tarreau9b28e032012-10-12 23:49:43 +02004058 s->logs.bytes_in = s->req->total = s->req->buf->i;
4059 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060
4061 if (s->pend_pos)
4062 pendconn_free(s->pend_pos);
4063
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004064 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065 if (s->flags & SN_CURR_SESS) {
4066 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004067 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004068 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004069 if (may_dequeue_tasks(objt_server(s->target), s->be))
4070 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004071 }
4072
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004073 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004074
4075 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004076 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4077 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004079 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004080 s->req->cons->err_loc = NULL;
4081 s->req->cons->exp = TICK_ETERNITY;
4082 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004083 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4084 s->rep->flags &= ~(CF_SHUTR|CF_SHUTR_NOW|CF_READ_ATTACHED|CF_READ_ERROR|CF_READ_NOEXP|CF_STREAMER|CF_STREAMER_FAST|CF_WRITE_PARTIAL|CF_NEVER_WAIT);
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02004085 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 +01004086 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004087
4088 if (s->flags & SN_COMP_READY)
4089 s->comp_algo->end(&s->comp_ctx);
4090 s->comp_algo = NULL;
4091 s->flags &= ~SN_COMP_READY;
4092
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093 s->txn.meth = 0;
4094 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004095 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004096 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004097 s->req->cons->flags |= SI_FL_INDEP_STR;
4098
Willy Tarreau96e31212011-05-30 18:10:30 +02004099 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004100 s->req->flags |= CF_NEVER_WAIT;
4101 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004102 }
4103
Willy Tarreau610ecce2010-01-04 21:15:02 +01004104 /* if the request buffer is not empty, it means we're
4105 * about to process another request, so send pending
4106 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004107 * Just don't do this if the buffer is close to be full,
4108 * because the request will wait for it to flush a little
4109 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004110 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004111 if (s->req->buf->i) {
4112 if (s->rep->buf->o &&
4113 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4114 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004115 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004116 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004117
4118 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004119 channel_auto_read(s->req);
4120 channel_auto_close(s->req);
4121 channel_auto_read(s->rep);
4122 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123
Willy Tarreau342b11c2010-11-24 16:22:09 +01004124 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004125 s->rep->analysers = 0;
4126
4127 http_silent_debug(__LINE__, s);
4128}
4129
4130
4131/* This function updates the request state machine according to the response
4132 * state machine and buffer flags. It returns 1 if it changes anything (flag
4133 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4134 * it is only used to find when a request/response couple is complete. Both
4135 * this function and its equivalent should loop until both return zero. It
4136 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4137 */
4138int http_sync_req_state(struct session *s)
4139{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004140 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004141 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004142 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004143 unsigned int old_state = txn->req.msg_state;
4144
4145 http_silent_debug(__LINE__, s);
4146 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4147 return 0;
4148
4149 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004150 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004151 * We can shut the read side unless we want to abort_on_close,
4152 * or we have a POST request. The issue with POST requests is
4153 * that some browsers still send a CRLF after the request, and
4154 * this CRLF must be read so that it does not remain in the kernel
4155 * buffers, otherwise a close could cause an RST on some systems
4156 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004157 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004158 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004159 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004160
4161 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4162 goto wait_other_side;
4163
4164 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4165 /* The server has not finished to respond, so we
4166 * don't want to move in order not to upset it.
4167 */
4168 goto wait_other_side;
4169 }
4170
4171 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4172 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004173 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004174 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004175 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004176 goto wait_other_side;
4177 }
4178
4179 /* When we get here, it means that both the request and the
4180 * response have finished receiving. Depending on the connection
4181 * mode, we'll have to wait for the last bytes to leave in either
4182 * direction, and sometimes for a close to be effective.
4183 */
4184
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004185 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4186 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004187 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4188 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004189 }
4190 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4191 /* Option forceclose is set, or either side wants to close,
4192 * let's enforce it now that we're not expecting any new
4193 * data to come. The caller knows the session is complete
4194 * once both states are CLOSED.
4195 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004196 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4197 channel_shutr_now(chn);
4198 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004199 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004200 }
4201 else {
4202 /* The last possible modes are keep-alive and tunnel. Since tunnel
4203 * mode does not set the body analyser, we can't reach this place
4204 * in tunnel mode, so we're left with keep-alive only.
4205 * This mode is currently not implemented, we switch to tunnel mode.
4206 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004207 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004208 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004209 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004210 }
4211
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004212 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004213 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004214 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004215
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004216 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217 txn->req.msg_state = HTTP_MSG_CLOSING;
4218 goto http_msg_closing;
4219 }
4220 else {
4221 txn->req.msg_state = HTTP_MSG_CLOSED;
4222 goto http_msg_closed;
4223 }
4224 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004225 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004226 }
4227
4228 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4229 http_msg_closing:
4230 /* nothing else to forward, just waiting for the output buffer
4231 * to be empty and for the shutw_now to take effect.
4232 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004233 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004234 txn->req.msg_state = HTTP_MSG_CLOSED;
4235 goto http_msg_closed;
4236 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004237 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004238 txn->req.msg_state = HTTP_MSG_ERROR;
4239 goto wait_other_side;
4240 }
4241 }
4242
4243 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4244 http_msg_closed:
4245 goto wait_other_side;
4246 }
4247
4248 wait_other_side:
4249 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004250 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004251}
4252
4253
4254/* This function updates the response state machine according to the request
4255 * state machine and buffer flags. It returns 1 if it changes anything (flag
4256 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4257 * it is only used to find when a request/response couple is complete. Both
4258 * this function and its equivalent should loop until both return zero. It
4259 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4260 */
4261int http_sync_res_state(struct session *s)
4262{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004263 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004264 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004265 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004266 unsigned int old_state = txn->rsp.msg_state;
4267
4268 http_silent_debug(__LINE__, s);
4269 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4270 return 0;
4271
4272 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4273 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004274 * still monitor the server connection for a possible close
4275 * while the request is being uploaded, so we don't disable
4276 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004277 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004278 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004279
4280 if (txn->req.msg_state == HTTP_MSG_ERROR)
4281 goto wait_other_side;
4282
4283 if (txn->req.msg_state < HTTP_MSG_DONE) {
4284 /* The client seems to still be sending data, probably
4285 * because we got an error response during an upload.
4286 * We have the choice of either breaking the connection
4287 * or letting it pass through. Let's do the later.
4288 */
4289 goto wait_other_side;
4290 }
4291
4292 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4293 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004294 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004295 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004296 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297 goto wait_other_side;
4298 }
4299
4300 /* When we get here, it means that both the request and the
4301 * response have finished receiving. Depending on the connection
4302 * mode, we'll have to wait for the last bytes to leave in either
4303 * direction, and sometimes for a close to be effective.
4304 */
4305
4306 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4307 /* Server-close mode : shut read and wait for the request
4308 * side to close its output buffer. The caller will detect
4309 * when we're in DONE and the other is in CLOSED and will
4310 * catch that for the final cleanup.
4311 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004312 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4313 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004314 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004315 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4316 /* Option forceclose is set, or either side wants to close,
4317 * let's enforce it now that we're not expecting any new
4318 * data to come. The caller knows the session is complete
4319 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004321 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4322 channel_shutr_now(chn);
4323 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004324 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004325 }
4326 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004327 /* The last possible modes are keep-alive and tunnel. Since tunnel
4328 * mode does not set the body analyser, we can't reach this place
4329 * in tunnel mode, so we're left with keep-alive only.
4330 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004332 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004333 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004334 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004335 }
4336
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004337 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004338 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004339 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004340 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4341 goto http_msg_closing;
4342 }
4343 else {
4344 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4345 goto http_msg_closed;
4346 }
4347 }
4348 goto wait_other_side;
4349 }
4350
4351 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4352 http_msg_closing:
4353 /* nothing else to forward, just waiting for the output buffer
4354 * to be empty and for the shutw_now to take effect.
4355 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004356 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004357 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4358 goto http_msg_closed;
4359 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004360 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004361 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004362 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004363 if (objt_server(s->target))
4364 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004365 goto wait_other_side;
4366 }
4367 }
4368
4369 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4370 http_msg_closed:
4371 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004372 bi_erase(chn);
4373 channel_auto_close(chn);
4374 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004375 goto wait_other_side;
4376 }
4377
4378 wait_other_side:
4379 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004380 /* We force the response to leave immediately if we're waiting for the
4381 * other side, since there is no pending shutdown to push it out.
4382 */
4383 if (!channel_is_empty(chn))
4384 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004385 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004386}
4387
4388
4389/* Resync the request and response state machines. Return 1 if either state
4390 * changes.
4391 */
4392int http_resync_states(struct session *s)
4393{
4394 struct http_txn *txn = &s->txn;
4395 int old_req_state = txn->req.msg_state;
4396 int old_res_state = txn->rsp.msg_state;
4397
4398 http_silent_debug(__LINE__, s);
4399 http_sync_req_state(s);
4400 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004401 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004402 if (!http_sync_res_state(s))
4403 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004404 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004405 if (!http_sync_req_state(s))
4406 break;
4407 }
4408 http_silent_debug(__LINE__, s);
4409 /* OK, both state machines agree on a compatible state.
4410 * There are a few cases we're interested in :
4411 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4412 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4413 * directions, so let's simply disable both analysers.
4414 * - HTTP_MSG_CLOSED on the response only means we must abort the
4415 * request.
4416 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4417 * with server-close mode means we've completed one request and we
4418 * must re-initialize the server connection.
4419 */
4420
4421 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4422 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4423 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4424 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4425 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004426 channel_auto_close(s->req);
4427 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004428 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004429 channel_auto_close(s->rep);
4430 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004431 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004432 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4433 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004434 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004435 (s->rep->flags & CF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004436 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004437 channel_auto_close(s->rep);
4438 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004439 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004440 channel_abort(s->req);
4441 channel_auto_close(s->req);
4442 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004443 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444 }
4445 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4446 txn->rsp.msg_state == HTTP_MSG_DONE &&
4447 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4448 /* server-close: terminate this server connection and
4449 * reinitialize a fresh-new transaction.
4450 */
4451 http_end_txn_clean_session(s);
4452 }
4453
4454 http_silent_debug(__LINE__, s);
4455 return txn->req.msg_state != old_req_state ||
4456 txn->rsp.msg_state != old_res_state;
4457}
4458
Willy Tarreaud98cf932009-12-27 22:54:55 +01004459/* This function is an analyser which forwards request body (including chunk
4460 * sizes if any). It is called as soon as we must forward, even if we forward
4461 * zero byte. The only situation where it must not be called is when we're in
4462 * tunnel mode and we want to forward till the close. It's used both to forward
4463 * remaining data and to resync after end of body. It expects the msg_state to
4464 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4465 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004466 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004467 * bytes of pending data + the headers if not already done (between sol and sov).
4468 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004469 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004470int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004471{
4472 struct http_txn *txn = &s->txn;
4473 struct http_msg *msg = &s->txn.req;
4474
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004475 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4476 return 0;
4477
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004478 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004479 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004480 /* Output closed while we were sending data. We must abort and
4481 * wake the other side up.
4482 */
4483 msg->msg_state = HTTP_MSG_ERROR;
4484 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004485 return 1;
4486 }
4487
Willy Tarreau4fe41902010-06-07 22:27:41 +02004488 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004489 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004490
4491 /* Note that we don't have to send 100-continue back because we don't
4492 * need the data to complete our job, and it's up to the server to
4493 * decide whether to return 100, 417 or anything else in return of
4494 * an "Expect: 100-continue" header.
4495 */
4496
4497 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004498 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004499 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004500 * is still null. We must save the body in msg->next because it
4501 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004502 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004503 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004504
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004505 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004506 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004507 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004508 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004509 }
4510
Willy Tarreaud98cf932009-12-27 22:54:55 +01004511 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004512 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004513
Willy Tarreau610ecce2010-01-04 21:15:02 +01004514 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004515 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004516 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004517 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004518 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004519 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004520 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004521 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004522 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004523
Willy Tarreaucaabe412010-01-03 23:08:28 +01004524 if (msg->msg_state == HTTP_MSG_DATA) {
4525 /* must still forward */
4526 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004527 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004528
4529 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004530 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004531 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004532 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004533 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004534 }
4535 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004536 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004537 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004538 * TRAILERS state.
4539 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004540 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004541
Willy Tarreau54d23df2012-10-25 19:04:45 +02004542 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004543 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004544 else if (ret < 0) {
4545 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004546 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004547 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004548 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004549 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004550 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004551 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004552 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004553 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004554 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004555
4556 if (ret == 0)
4557 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004558 else if (ret < 0) {
4559 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004560 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004561 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004562 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004563 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004564 /* we're in MSG_CHUNK_SIZE now */
4565 }
4566 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004567 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004568
4569 if (ret == 0)
4570 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004571 else if (ret < 0) {
4572 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004573 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004574 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004575 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004576 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004577 /* we're in HTTP_MSG_DONE now */
4578 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004579 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004580 int old_state = msg->msg_state;
4581
Willy Tarreau610ecce2010-01-04 21:15:02 +01004582 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004583 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004584 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4585 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004586 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004587 if (http_resync_states(s)) {
4588 /* some state changes occurred, maybe the analyser
4589 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004590 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004591 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004592 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004593 /* request errors are most likely due to
4594 * the server aborting the transfer.
4595 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004596 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004597 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004598 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004599 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004600 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004601 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004602 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004603 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004604
4605 /* If "option abortonclose" is set on the backend, we
4606 * want to monitor the client's connection and forward
4607 * any shutdown notification to the server, which will
4608 * decide whether to close or to go on processing the
4609 * request.
4610 */
4611 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004612 channel_auto_read(req);
4613 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004614 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004615 else if (s->txn.meth == HTTP_METH_POST) {
4616 /* POST requests may require to read extra CRLF
4617 * sent by broken browsers and which could cause
4618 * an RST to be sent upon close on some systems
4619 * (eg: Linux).
4620 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004621 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004622 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004623
Willy Tarreau610ecce2010-01-04 21:15:02 +01004624 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004625 }
4626 }
4627
Willy Tarreaud98cf932009-12-27 22:54:55 +01004628 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004629 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004630 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004631 if (!(s->flags & SN_ERR_MASK))
4632 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004633 if (!(s->flags & SN_FINST_MASK)) {
4634 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4635 s->flags |= SN_FINST_H;
4636 else
4637 s->flags |= SN_FINST_D;
4638 }
4639
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004640 s->fe->fe_counters.cli_aborts++;
4641 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004642 if (objt_server(s->target))
4643 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004644
4645 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004646 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004647
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004648 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004649 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004650 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004651
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004652 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004653 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004654 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004655 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004656 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004657
Willy Tarreau5c620922011-05-11 19:56:11 +02004658 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004659 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004660 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004661 * modes are already handled by the stream sock layer. We must not do
4662 * this in content-length mode because it could present the MSG_MORE
4663 * flag with the last block of forwarded data, which would cause an
4664 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004665 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004666 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004667 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004668
Willy Tarreau610ecce2010-01-04 21:15:02 +01004669 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004670 return 0;
4671
Willy Tarreaud98cf932009-12-27 22:54:55 +01004672 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004673 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004674 if (s->listener->counters)
4675 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004676 return_bad_req_stats_ok:
4677 txn->req.msg_state = HTTP_MSG_ERROR;
4678 if (txn->status) {
4679 /* Note: we don't send any error if some data were already sent */
4680 stream_int_retnclose(req->prod, NULL);
4681 } else {
4682 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004683 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004684 }
4685 req->analysers = 0;
4686 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004687
4688 if (!(s->flags & SN_ERR_MASK))
4689 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004690 if (!(s->flags & SN_FINST_MASK)) {
4691 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4692 s->flags |= SN_FINST_H;
4693 else
4694 s->flags |= SN_FINST_D;
4695 }
4696 return 0;
4697
4698 aborted_xfer:
4699 txn->req.msg_state = HTTP_MSG_ERROR;
4700 if (txn->status) {
4701 /* Note: we don't send any error if some data were already sent */
4702 stream_int_retnclose(req->prod, NULL);
4703 } else {
4704 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004705 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004706 }
4707 req->analysers = 0;
4708 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4709
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004710 s->fe->fe_counters.srv_aborts++;
4711 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004712 if (objt_server(s->target))
4713 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004714
4715 if (!(s->flags & SN_ERR_MASK))
4716 s->flags |= SN_ERR_SRVCL;
4717 if (!(s->flags & SN_FINST_MASK)) {
4718 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4719 s->flags |= SN_FINST_H;
4720 else
4721 s->flags |= SN_FINST_D;
4722 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004723 return 0;
4724}
4725
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004726/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4727 * processing can continue on next analysers, or zero if it either needs more
4728 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4729 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4730 * when it has nothing left to do, and may remove any analyser when it wants to
4731 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004732 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004733int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004734{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004735 struct http_txn *txn = &s->txn;
4736 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004737 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004738 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004739 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004740 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004741
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004742 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 +02004743 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004744 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004745 rep,
4746 rep->rex, rep->wex,
4747 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004748 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004749 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004750
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004751 /*
4752 * Now parse the partial (or complete) lines.
4753 * We will check the response syntax, and also join multi-line
4754 * headers. An index of all the lines will be elaborated while
4755 * parsing.
4756 *
4757 * For the parsing, we use a 28 states FSM.
4758 *
4759 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004760 * rep->buf->p = beginning of response
4761 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4762 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004763 * msg->eol = end of current header or line (LF or CRLF)
4764 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004765 */
4766
Willy Tarreau83e3af02009-12-28 17:39:57 +01004767 /* There's a protected area at the end of the buffer for rewriting
4768 * purposes. We don't want to start to parse the request if the
4769 * protected area is affected, because we may have to move processed
4770 * data later, which is much more complicated.
4771 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004772 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004773 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004774 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4775 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4776 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004777 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004778 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004779 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004780 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004781 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004782 return 0;
4783 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004784 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4785 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004786 }
4787
Willy Tarreau9b28e032012-10-12 23:49:43 +02004788 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004789 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004790 }
4791
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004792 /* 1: we might have to print this header in debug mode */
4793 if (unlikely((global.mode & MODE_DEBUG) &&
4794 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004795 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004796 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004797
Willy Tarreau9b28e032012-10-12 23:49:43 +02004798 sol = rep->buf->p;
4799 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004800 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004801
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004802 sol += hdr_idx_first_pos(&txn->hdr_idx);
4803 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004804
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004805 while (cur_idx) {
4806 eol = sol + txn->hdr_idx.v[cur_idx].len;
4807 debug_hdr("srvhdr", s, sol, eol);
4808 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4809 cur_idx = txn->hdr_idx.v[cur_idx].next;
4810 }
4811 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004812
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004813 /*
4814 * Now we quickly check if we have found a full valid response.
4815 * If not so, we check the FD and buffer states before leaving.
4816 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004817 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004818 * responses are checked first.
4819 *
4820 * Depending on whether the client is still there or not, we
4821 * may send an error response back or not. Note that normally
4822 * we should only check for HTTP status there, and check I/O
4823 * errors somewhere else.
4824 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004825
Willy Tarreau655dce92009-11-08 13:10:58 +01004826 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004827 /* Invalid response */
4828 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4829 /* we detected a parsing error. We want to archive this response
4830 * in the dedicated proxy area for later troubleshooting.
4831 */
4832 hdr_response_bad:
4833 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004834 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004835
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004836 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004837 if (objt_server(s->target)) {
4838 objt_server(s->target)->counters.failed_resp++;
4839 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004840 }
Willy Tarreau64648412010-03-05 10:41:54 +01004841 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004842 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004843 rep->analysers = 0;
4844 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004845 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004846 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004847 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004848
4849 if (!(s->flags & SN_ERR_MASK))
4850 s->flags |= SN_ERR_PRXCOND;
4851 if (!(s->flags & SN_FINST_MASK))
4852 s->flags |= SN_FINST_H;
4853
4854 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004855 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004856
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004857 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004858 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004859 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02004860 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004861 goto hdr_response_bad;
4862 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004863
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004864 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004865 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004866 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004867 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004868
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004869 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004870 if (objt_server(s->target)) {
4871 objt_server(s->target)->counters.failed_resp++;
4872 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004873 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004874
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004875 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004876 rep->analysers = 0;
4877 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004878 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004879 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004880 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004881
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004882 if (!(s->flags & SN_ERR_MASK))
4883 s->flags |= SN_ERR_SRVCL;
4884 if (!(s->flags & SN_FINST_MASK))
4885 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004886 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004887 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004888
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004889 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004890 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004891 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004892 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004893
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004894 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004895 if (objt_server(s->target)) {
4896 objt_server(s->target)->counters.failed_resp++;
4897 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004898 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004899
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004900 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004901 rep->analysers = 0;
4902 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004903 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004904 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004905 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004906
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004907 if (!(s->flags & SN_ERR_MASK))
4908 s->flags |= SN_ERR_SRVTO;
4909 if (!(s->flags & SN_FINST_MASK))
4910 s->flags |= SN_FINST_H;
4911 return 0;
4912 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004913
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004914 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004915 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004916 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004917 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004918
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004919 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004920 if (objt_server(s->target)) {
4921 objt_server(s->target)->counters.failed_resp++;
4922 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004923 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004924
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004925 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004926 rep->analysers = 0;
4927 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004928 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004929 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004930 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004931
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004932 if (!(s->flags & SN_ERR_MASK))
4933 s->flags |= SN_ERR_SRVCL;
4934 if (!(s->flags & SN_FINST_MASK))
4935 s->flags |= SN_FINST_H;
4936 return 0;
4937 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004938
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004939 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004940 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004941 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004942 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004943
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004944 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004945 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004946 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004947
4948 if (!(s->flags & SN_ERR_MASK))
4949 s->flags |= SN_ERR_CLICL;
4950 if (!(s->flags & SN_FINST_MASK))
4951 s->flags |= SN_FINST_H;
4952
4953 /* process_session() will take care of the error */
4954 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004955 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004956
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004957 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004958 return 0;
4959 }
4960
4961 /* More interesting part now : we know that we have a complete
4962 * response which at least looks like HTTP. We have an indicator
4963 * of each header's length, so we can parse them quickly.
4964 */
4965
4966 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004967 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004968
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004969 /*
4970 * 1: get the status code
4971 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004972 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004973 if (n < 1 || n > 5)
4974 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004975 /* when the client triggers a 4xx from the server, it's most often due
4976 * to a missing object or permission. These events should be tracked
4977 * because if they happen often, it may indicate a brute force or a
4978 * vulnerability scan.
4979 */
4980 if (n == 4)
4981 session_inc_http_err_ctr(s);
4982
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004983 if (objt_server(s->target))
4984 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004985
Willy Tarreau5b154472009-12-21 20:11:07 +01004986 /* check if the response is HTTP/1.1 or above */
4987 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02004988 ((rep->buf->p[5] > '1') ||
4989 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004990 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004991
4992 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01004993 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01004994
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004995 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004996 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004997
Willy Tarreau9b28e032012-10-12 23:49:43 +02004998 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004999
Willy Tarreau39650402010-03-15 19:44:39 +01005000 /* Adjust server's health based on status code. Note: status codes 501
5001 * and 505 are triggered on demand by client request, so we must not
5002 * count them as server failures.
5003 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005004 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005005 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005006 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005007 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005008 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005009 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005010
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005011 /*
5012 * 2: check for cacheability.
5013 */
5014
5015 switch (txn->status) {
5016 case 200:
5017 case 203:
5018 case 206:
5019 case 300:
5020 case 301:
5021 case 410:
5022 /* RFC2616 @13.4:
5023 * "A response received with a status code of
5024 * 200, 203, 206, 300, 301 or 410 MAY be stored
5025 * by a cache (...) unless a cache-control
5026 * directive prohibits caching."
5027 *
5028 * RFC2616 @9.5: POST method :
5029 * "Responses to this method are not cacheable,
5030 * unless the response includes appropriate
5031 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005032 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005033 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005034 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005035 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5036 break;
5037 default:
5038 break;
5039 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005040
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005041 /*
5042 * 3: we may need to capture headers
5043 */
5044 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005045 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005046 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005047 txn->rsp.cap, s->fe->rsp_cap);
5048
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005049 /* 4: determine the transfer-length.
5050 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5051 * the presence of a message-body in a RESPONSE and its transfer length
5052 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005053 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005054 * All responses to the HEAD request method MUST NOT include a
5055 * message-body, even though the presence of entity-header fields
5056 * might lead one to believe they do. All 1xx (informational), 204
5057 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5058 * message-body. All other responses do include a message-body,
5059 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005060 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005061 * 1. Any response which "MUST NOT" include a message-body (such as the
5062 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5063 * always terminated by the first empty line after the header fields,
5064 * regardless of the entity-header fields present in the message.
5065 *
5066 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5067 * the "chunked" transfer-coding (Section 6.2) is used, the
5068 * transfer-length is defined by the use of this transfer-coding.
5069 * If a Transfer-Encoding header field is present and the "chunked"
5070 * transfer-coding is not present, the transfer-length is defined by
5071 * the sender closing the connection.
5072 *
5073 * 3. If a Content-Length header field is present, its decimal value in
5074 * OCTETs represents both the entity-length and the transfer-length.
5075 * If a message is received with both a Transfer-Encoding header
5076 * field and a Content-Length header field, the latter MUST be ignored.
5077 *
5078 * 4. If the message uses the media type "multipart/byteranges", and
5079 * the transfer-length is not otherwise specified, then this self-
5080 * delimiting media type defines the transfer-length. This media
5081 * type MUST NOT be used unless the sender knows that the recipient
5082 * can parse it; the presence in a request of a Range header with
5083 * multiple byte-range specifiers from a 1.1 client implies that the
5084 * client can parse multipart/byteranges responses.
5085 *
5086 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005087 */
5088
5089 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005090 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005091 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005092 * FIXME: should we parse anyway and return an error on chunked encoding ?
5093 */
5094 if (txn->meth == HTTP_METH_HEAD ||
5095 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005096 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005097 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005098 goto skip_content_length;
5099 }
5100
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005101 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005102 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005103 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005104 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005105 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005106 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5107 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005108 /* bad transfer-encoding (chunked followed by something else) */
5109 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005110 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005111 break;
5112 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005113 }
5114
5115 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5116 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005117 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005118 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005119 signed long long cl;
5120
Willy Tarreauad14f752011-09-02 20:33:27 +02005121 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005122 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005123 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005124 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005125
Willy Tarreauad14f752011-09-02 20:33:27 +02005126 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005127 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005128 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005129 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005130
Willy Tarreauad14f752011-09-02 20:33:27 +02005131 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005132 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005133 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005134 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005135
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005136 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005137 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005138 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005139 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005140
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005141 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005142 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005143 }
5144
William Lallemand82fe75c2012-10-23 10:25:10 +02005145 if (s->fe->comp || s->be->comp)
5146 select_compression_response_header(s, rep->buf);
5147
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005148 /* FIXME: we should also implement the multipart/byterange method.
5149 * For now on, we resort to close mode in this case (unknown length).
5150 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005151skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005152
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005153 /* end of job, return OK */
5154 rep->analysers &= ~an_bit;
5155 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005156 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005157 return 1;
5158}
5159
5160/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005161 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5162 * and updates t->rep->analysers. It might make sense to explode it into several
5163 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005164 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005165int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005166{
5167 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005168 struct http_msg *msg = &txn->rsp;
5169 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005170 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005171
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005172 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 +02005173 now_ms, __FUNCTION__,
5174 t,
5175 rep,
5176 rep->rex, rep->wex,
5177 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005178 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005179 rep->analysers);
5180
Willy Tarreau655dce92009-11-08 13:10:58 +01005181 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005182 return 0;
5183
5184 rep->analysers &= ~an_bit;
5185 rep->analyse_exp = TICK_ETERNITY;
5186
Willy Tarreau5b154472009-12-21 20:11:07 +01005187 /* Now we have to check if we need to modify the Connection header.
5188 * This is more difficult on the response than it is on the request,
5189 * because we can have two different HTTP versions and we don't know
5190 * how the client will interprete a response. For instance, let's say
5191 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5192 * HTTP/1.1 response without any header. Maybe it will bound itself to
5193 * HTTP/1.0 because it only knows about it, and will consider the lack
5194 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5195 * the lack of header as a keep-alive. Thus we will use two flags
5196 * indicating how a request MAY be understood by the client. In case
5197 * of multiple possibilities, we'll fix the header to be explicit. If
5198 * ambiguous cases such as both close and keepalive are seen, then we
5199 * will fall back to explicit close. Note that we won't take risks with
5200 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005201 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005202 */
5203
Willy Tarreaudc008c52010-02-01 16:20:08 +01005204 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5205 txn->status == 101)) {
5206 /* Either we've established an explicit tunnel, or we're
5207 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005208 * to understand the next protocols. We have to switch to tunnel
5209 * mode, so that we transfer the request and responses then let
5210 * this protocol pass unmodified. When we later implement specific
5211 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005212 * header which contains information about that protocol for
5213 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005214 */
5215 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5216 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005217 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5218 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5219 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005220 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005221
Willy Tarreau60466522010-01-18 19:08:45 +01005222 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005223 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005224 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5225 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005226
Willy Tarreau60466522010-01-18 19:08:45 +01005227 /* now adjust header transformations depending on current state */
5228 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5229 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5230 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005231 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005232 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005233 }
Willy Tarreau60466522010-01-18 19:08:45 +01005234 else { /* SCL / KAL */
5235 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005236 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005237 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005238 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005239
Willy Tarreau60466522010-01-18 19:08:45 +01005240 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005241 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005242
Willy Tarreau60466522010-01-18 19:08:45 +01005243 /* Some keep-alive responses are converted to Server-close if
5244 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005245 */
Willy Tarreau60466522010-01-18 19:08:45 +01005246 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5247 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005248 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005249 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005250 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005251 }
5252
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005253 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005254 /*
5255 * 3: we will have to evaluate the filters.
5256 * As opposed to version 1.2, now they will be evaluated in the
5257 * filters order and not in the header order. This means that
5258 * each filter has to be validated among all headers.
5259 *
5260 * Filters are tried with ->be first, then with ->fe if it is
5261 * different from ->be.
5262 */
5263
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005264 cur_proxy = t->be;
5265 while (1) {
5266 struct proxy *rule_set = cur_proxy;
5267
5268 /* try headers filters */
5269 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005270 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005271 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005272 if (objt_server(t->target)) {
5273 objt_server(t->target)->counters.failed_resp++;
5274 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005275 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005276 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005277 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005278 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005279 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005280 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005281 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005282 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005283 if (!(t->flags & SN_ERR_MASK))
5284 t->flags |= SN_ERR_PRXCOND;
5285 if (!(t->flags & SN_FINST_MASK))
5286 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005287 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005288 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005289 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005290
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005291 /* has the response been denied ? */
5292 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005293 if (objt_server(t->target))
5294 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005295
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005296 t->be->be_counters.denied_resp++;
5297 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005298 if (t->listener->counters)
5299 t->listener->counters->denied_resp++;
5300
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005301 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005302 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005303
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005304 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005305 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005306 if (txn->status < 200)
5307 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005308 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005309 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005310 ret = acl_pass(ret);
5311 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5312 ret = !ret;
5313 if (!ret)
5314 continue;
5315 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005316 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005317 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005318 }
5319
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005320 /* check whether we're already working on the frontend */
5321 if (cur_proxy == t->fe)
5322 break;
5323 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005324 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005325
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005326 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005327 * We may be facing a 100-continue response, in which case this
5328 * is not the right response, and we're waiting for the next one.
5329 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005330 * next one.
5331 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005332 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005333 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005334 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005335 msg->msg_state = HTTP_MSG_RPBEFORE;
5336 txn->status = 0;
5337 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5338 return 1;
5339 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005340 else if (unlikely(txn->status < 200))
5341 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005342
5343 /* we don't have any 1xx status code now */
5344
5345 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005346 * 4: check for server cookie.
5347 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005348 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5349 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005350 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005351
Willy Tarreaubaaee002006-06-26 02:48:02 +02005352
Willy Tarreaua15645d2007-03-18 16:22:39 +01005353 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005354 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005355 */
Willy Tarreau67402132012-05-31 20:40:20 +02005356 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005357 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005358
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005359 /*
5360 * 6: add server cookie in the response if needed
5361 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005362 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005363 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005364 (!(t->flags & SN_DIRECT) ||
5365 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5366 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5367 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5368 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005369 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005370 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005371 /* the server is known, it's not the one the client requested, or the
5372 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005373 * insert a set-cookie here, except if we want to insert only on POST
5374 * requests and this one isn't. Note that servers which don't have cookies
5375 * (eg: some backup servers) will return a full cookie removal request.
5376 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005377 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005378 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005379 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5380 t->be->cookie_name);
5381 }
5382 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005383 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005384
5385 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5386 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005387 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5388 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5389 trash.len += 5;
5390
Willy Tarreauef4f3912010-10-07 21:00:29 +02005391 if (t->be->cookie_maxlife) {
5392 /* emit first_date, which is either the original one or
5393 * the current date.
5394 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005395 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005396 s30tob64(txn->cookie_first_date ?
5397 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005398 (date.tv_sec+3) >> 2, trash.str + trash.len);
5399 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005400 }
5401 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005402 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005403 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005404
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005405 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005406 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005407
Willy Tarreau4992dd22012-05-31 21:02:17 +02005408 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005409 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005410
5411 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005412 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005413
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005414 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005415 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005416
Willy Tarreauf1348312010-10-07 15:54:11 +02005417 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005418 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005419 /* the server did not change, only the date was updated */
5420 txn->flags |= TX_SCK_UPDATED;
5421 else
5422 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005423
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005424 /* Here, we will tell an eventual cache on the client side that we don't
5425 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5426 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5427 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5428 */
Willy Tarreau67402132012-05-31 20:40:20 +02005429 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005430
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005431 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5432
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005433 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005434 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005435 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005436 }
5437 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005438
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005439 /*
5440 * 7: check if result will be cacheable with a cookie.
5441 * We'll block the response if security checks have caught
5442 * nasty things such as a cacheable cookie.
5443 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005444 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5445 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005446 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005447
5448 /* we're in presence of a cacheable response containing
5449 * a set-cookie header. We'll block it as requested by
5450 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005451 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005452 if (objt_server(t->target))
5453 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005454
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005455 t->be->be_counters.denied_resp++;
5456 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005457 if (t->listener->counters)
5458 t->listener->counters->denied_resp++;
5459
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005460 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005461 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005462 send_log(t->be, LOG_ALERT,
5463 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005464 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005465 goto return_srv_prx_502;
5466 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005467
5468 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005469 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005470 * If an "Upgrade" token is found, the header is left untouched in order
5471 * not to have to deal with some client bugs : some of them fail an upgrade
5472 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005473 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005474 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5475 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5476 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005477 unsigned int want_flags = 0;
5478
5479 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5480 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5481 /* we want a keep-alive response here. Keep-alive header
5482 * required if either side is not 1.1.
5483 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005484 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005485 want_flags |= TX_CON_KAL_SET;
5486 }
5487 else {
5488 /* we want a close response here. Close header required if
5489 * the server is 1.1, regardless of the client.
5490 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005491 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005492 want_flags |= TX_CON_CLO_SET;
5493 }
5494
5495 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005496 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005497 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005498
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005499 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005500 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005501 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005502 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005503
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005504 /*************************************************************
5505 * OK, that's finished for the headers. We have done what we *
5506 * could. Let's switch to the DATA state. *
5507 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005508
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005509 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005510
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005511 /* if the user wants to log as soon as possible, without counting
5512 * bytes from the server, then this is the right moment. We have
5513 * to temporarily assign bytes_out to log what we currently have.
5514 */
5515 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5516 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5517 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005518 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005519 t->logs.bytes_out = 0;
5520 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005521
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005522 /* Note: we must not try to cheat by jumping directly to DATA,
5523 * otherwise we would not let the client side wake up.
5524 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005525
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005526 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005527 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005528 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005529}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005530
Willy Tarreaud98cf932009-12-27 22:54:55 +01005531/* This function is an analyser which forwards response body (including chunk
5532 * sizes if any). It is called as soon as we must forward, even if we forward
5533 * zero byte. The only situation where it must not be called is when we're in
5534 * tunnel mode and we want to forward till the close. It's used both to forward
5535 * remaining data and to resync after end of body. It expects the msg_state to
5536 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5537 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005538 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005539 * bytes of pending data + the headers if not already done (between sol and sov).
5540 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005541 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005542int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005543{
5544 struct http_txn *txn = &s->txn;
5545 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005546 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005547 static struct buffer *tmpbuf = NULL;
5548 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005549 int consumed_data = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005550
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005551 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5552 return 0;
5553
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005554 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005555 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005556 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005557 /* Output closed while we were sending data. We must abort and
5558 * wake the other side up.
5559 */
5560 msg->msg_state = HTTP_MSG_ERROR;
5561 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005562 return 1;
5563 }
5564
Willy Tarreau4fe41902010-06-07 22:27:41 +02005565 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005566 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005567
William Lallemand82fe75c2012-10-23 10:25:10 +02005568 /* this is the first time we need the compression buffer */
5569 if (s->comp_algo != NULL && tmpbuf == NULL) {
5570 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5571 goto aborted_xfer; /* no memory */
5572 }
5573
Willy Tarreaud98cf932009-12-27 22:54:55 +01005574 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005575 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005576 * rep->buf.p still points to the beginning of the message and msg->sol
5577 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005578 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005579 channel_forward(res, msg->sov);
5580 msg->next = 0;
5581 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005582
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005583 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005584 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005585 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005586 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005587 }
5588
William Lallemand82fe75c2012-10-23 10:25:10 +02005589 if (s->comp_algo != NULL) {
5590 int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
5591 if (ret < 0)
5592 goto missing_data; /* not enough spaces in buffers */
5593 compressing = 1;
5594 }
5595
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005597 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005598 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005599 if (s->comp_algo == NULL) {
5600 bytes = msg->sov - msg->sol;
5601 if (msg->chunk_len || bytes) {
5602 msg->sol = msg->sov;
5603 msg->next -= bytes; /* will be forwarded */
5604 msg->chunk_len += bytes;
5605 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5606 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005607 }
5608
Willy Tarreaucaabe412010-01-03 23:08:28 +01005609 if (msg->msg_state == HTTP_MSG_DATA) {
William Lallemandbf3ae612012-11-19 12:35:37 +01005610 int ret;
5611
Willy Tarreaucaabe412010-01-03 23:08:28 +01005612 /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005613 if (compressing) {
5614 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5615 if (ret < 0)
5616 goto aborted_xfer;
5617 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005618
5619 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005620 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005621
5622 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005623 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005624 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005625 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005626 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005627 if (compressing && consumed_data) {
5628 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5629 compressing = 0;
5630 }
5631 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01005632 }
5633 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005634 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005635 * set ->sov and ->next to point to the body and switch to DATA or
5636 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005637 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005638 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005639
Willy Tarreau54d23df2012-10-25 19:04:45 +02005640 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005641 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005642 else if (ret < 0) {
5643 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005644 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005645 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005646 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005647 if (compressing) {
5648 if (likely(msg->chunk_len > 0)) {
5649 /* skipping data if we are in compression mode */
5650 b_adv(res->buf, msg->next);
5651 msg->next = 0;
5652 msg->sov = 0;
5653 msg->sol = 0;
5654 } else {
5655 if (consumed_data) {
5656 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5657 compressing = 0;
5658 }
5659 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005660 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005661 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005662 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005663 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005664 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005665 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005666
Willy Tarreau54d23df2012-10-25 19:04:45 +02005667 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005668 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005669 else if (ret < 0) {
5670 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005671 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005672 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005673 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005674 /* skipping data in buffer for compression */
5675 if (compressing) {
5676 b_adv(res->buf, msg->next);
5677 msg->next = 0;
5678 msg->sov = 0;
5679 msg->sol = 0;
5680 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005681 /* we're in MSG_CHUNK_SIZE now */
5682 }
5683 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005684 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005685
Willy Tarreaud98cf932009-12-27 22:54:55 +01005686 if (ret == 0)
5687 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005688 else if (ret < 0) {
5689 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005690 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005691 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005692 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005693 /* we're in HTTP_MSG_DONE now */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005694 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005695 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005696 int old_state = msg->msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005697 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005698 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005699 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5700 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005701 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005702 if (http_resync_states(s)) {
5703 http_silent_debug(__LINE__, s);
5704 /* some state changes occurred, maybe the analyser
5705 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005706 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005707 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005708 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005709 /* response errors are most likely due to
5710 * the client aborting the transfer.
5711 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005712 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005713 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005714 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005715 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005716 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005717 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005718 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005719 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005720 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005721 }
5722 }
5723
Willy Tarreaud98cf932009-12-27 22:54:55 +01005724 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005725 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005726 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5727 compressing = 0;
5728 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005729 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005730 if (res->flags & CF_SHUTR) {
Willy Tarreau40dba092010-03-04 18:14:51 +01005731 if (!(s->flags & SN_ERR_MASK))
5732 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005733 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005734 if (objt_server(s->target))
5735 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005736 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005737 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005738
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005739 if (res->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005740 goto aborted_xfer;
5741
Willy Tarreau40dba092010-03-04 18:14:51 +01005742 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005743 if (!s->req->analysers)
5744 goto return_bad_res;
5745
Willy Tarreauea953162012-05-18 23:41:28 +02005746 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005747 if (s->comp_algo == NULL) {
5748 bytes = msg->sov - msg->sol;
5749 if (msg->chunk_len || bytes) {
5750 msg->sol = msg->sov;
5751 msg->next -= bytes; /* will be forwarded */
5752 msg->chunk_len += bytes;
5753 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5754 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005755 }
5756
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005757 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005758 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005759 * Similarly, with keep-alive on the client side, we don't want to forward a
5760 * close.
5761 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005762 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005763 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5764 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005765 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005766
Willy Tarreau5c620922011-05-11 19:56:11 +02005767 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005768 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005769 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005770 * modes are already handled by the stream sock layer. We must not do
5771 * this in content-length mode because it could present the MSG_MORE
5772 * flag with the last block of forwarded data, which would cause an
5773 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005774 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005775 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005776 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005777
Willy Tarreaud98cf932009-12-27 22:54:55 +01005778 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005779 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005780 return 0;
5781
Willy Tarreau40dba092010-03-04 18:14:51 +01005782 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005783 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005784 if (objt_server(s->target))
5785 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005786
5787 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005788 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005789 /* don't send any error message as we're in the body */
5790 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005791 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005792 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005793 if (objt_server(s->target))
5794 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005795
5796 if (!(s->flags & SN_ERR_MASK))
5797 s->flags |= SN_ERR_PRXCOND;
5798 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005799 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005800 return 0;
5801
5802 aborted_xfer:
5803 txn->rsp.msg_state = HTTP_MSG_ERROR;
5804 /* don't send any error message as we're in the body */
5805 stream_int_retnclose(res->cons, NULL);
5806 res->analysers = 0;
5807 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5808
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005809 s->fe->fe_counters.cli_aborts++;
5810 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005811 if (objt_server(s->target))
5812 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005813
5814 if (!(s->flags & SN_ERR_MASK))
5815 s->flags |= SN_ERR_CLICL;
5816 if (!(s->flags & SN_FINST_MASK))
5817 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005818 return 0;
5819}
5820
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005821/* Iterate the same filter through all request headers.
5822 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005823 * Since it can manage the switch to another backend, it updates the per-proxy
5824 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005825 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005826int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005827{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005828 char term;
5829 char *cur_ptr, *cur_end, *cur_next;
5830 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005831 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005832 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005833 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005834
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005835 last_hdr = 0;
5836
Willy Tarreau9b28e032012-10-12 23:49:43 +02005837 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005838 old_idx = 0;
5839
5840 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005841 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005842 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005843 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005844 (exp->action == ACT_ALLOW ||
5845 exp->action == ACT_DENY ||
5846 exp->action == ACT_TARPIT))
5847 return 0;
5848
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005849 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005850 if (!cur_idx)
5851 break;
5852
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005853 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005854 cur_ptr = cur_next;
5855 cur_end = cur_ptr + cur_hdr->len;
5856 cur_next = cur_end + cur_hdr->cr + 1;
5857
5858 /* Now we have one header between cur_ptr and cur_end,
5859 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005860 */
5861
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005862 /* The annoying part is that pattern matching needs
5863 * that we modify the contents to null-terminate all
5864 * strings before testing them.
5865 */
5866
5867 term = *cur_end;
5868 *cur_end = '\0';
5869
5870 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5871 switch (exp->action) {
5872 case ACT_SETBE:
5873 /* It is not possible to jump a second time.
5874 * FIXME: should we return an HTTP/500 here so that
5875 * the admin knows there's a problem ?
5876 */
5877 if (t->be != t->fe)
5878 break;
5879
5880 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005881 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005882 last_hdr = 1;
5883 break;
5884
5885 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005886 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005887 last_hdr = 1;
5888 break;
5889
5890 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005891 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005892 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005893
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005894 t->fe->fe_counters.denied_req++;
5895 if (t->fe != t->be)
5896 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005897 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005898 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005899
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005900 break;
5901
5902 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005903 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005904 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005905
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005906 t->fe->fe_counters.denied_req++;
5907 if (t->fe != t->be)
5908 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005909 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005910 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005911
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005912 break;
5913
5914 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005915 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
5916 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005917 /* FIXME: if the user adds a newline in the replacement, the
5918 * index will not be recalculated for now, and the new line
5919 * will not be counted as a new header.
5920 */
5921
5922 cur_end += delta;
5923 cur_next += delta;
5924 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005925 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005926 break;
5927
5928 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02005929 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005930 cur_next += delta;
5931
Willy Tarreaufa355d42009-11-29 18:12:29 +01005932 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005933 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5934 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005935 cur_hdr->len = 0;
5936 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005937 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005938 break;
5939
5940 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005941 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005942 if (cur_end)
5943 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005944
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005945 /* keep the link from this header to next one in case of later
5946 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005947 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005948 old_idx = cur_idx;
5949 }
5950 return 0;
5951}
5952
5953
5954/* Apply the filter to the request line.
5955 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5956 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005957 * Since it can manage the switch to another backend, it updates the per-proxy
5958 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005959 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005960int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005961{
5962 char term;
5963 char *cur_ptr, *cur_end;
5964 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005965 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005966 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005967
Willy Tarreau3d300592007-03-18 18:34:41 +01005968 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005969 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005970 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005971 (exp->action == ACT_ALLOW ||
5972 exp->action == ACT_DENY ||
5973 exp->action == ACT_TARPIT))
5974 return 0;
5975 else if (exp->action == ACT_REMOVE)
5976 return 0;
5977
5978 done = 0;
5979
Willy Tarreau9b28e032012-10-12 23:49:43 +02005980 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005981 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005982
5983 /* Now we have the request line between cur_ptr and cur_end */
5984
5985 /* The annoying part is that pattern matching needs
5986 * that we modify the contents to null-terminate all
5987 * strings before testing them.
5988 */
5989
5990 term = *cur_end;
5991 *cur_end = '\0';
5992
5993 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5994 switch (exp->action) {
5995 case ACT_SETBE:
5996 /* It is not possible to jump a second time.
5997 * FIXME: should we return an HTTP/500 here so that
5998 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005999 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006000 if (t->be != t->fe)
6001 break;
6002
6003 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006004 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006005 done = 1;
6006 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006007
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006008 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006009 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006010 done = 1;
6011 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006012
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006013 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006014 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006015
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006016 t->fe->fe_counters.denied_req++;
6017 if (t->fe != t->be)
6018 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006019 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006020 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006021
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006022 done = 1;
6023 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006024
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006025 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006026 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006027
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006028 t->fe->fe_counters.denied_req++;
6029 if (t->fe != t->be)
6030 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006031 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006032 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006033
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006034 done = 1;
6035 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006036
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006037 case ACT_REPLACE:
6038 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006039 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6040 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006041 /* FIXME: if the user adds a newline in the replacement, the
6042 * index will not be recalculated for now, and the new line
6043 * will not be counted as a new header.
6044 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006045
Willy Tarreaufa355d42009-11-29 18:12:29 +01006046 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006047 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006048 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006049 HTTP_MSG_RQMETH,
6050 cur_ptr, cur_end + 1,
6051 NULL, NULL);
6052 if (unlikely(!cur_end))
6053 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006054
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006055 /* we have a full request and we know that we have either a CR
6056 * or an LF at <ptr>.
6057 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006058 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6059 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006060 /* there is no point trying this regex on headers */
6061 return 1;
6062 }
6063 }
6064 *cur_end = term; /* restore the string terminator */
6065 return done;
6066}
Willy Tarreau97de6242006-12-27 17:18:38 +01006067
Willy Tarreau58f10d72006-12-04 02:26:12 +01006068
Willy Tarreau58f10d72006-12-04 02:26:12 +01006069
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006070/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006071 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006072 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006073 * unparsable request. Since it can manage the switch to another backend, it
6074 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006075 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006076int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006077{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006078 struct http_txn *txn = &s->txn;
6079 struct hdr_exp *exp;
6080
6081 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006082 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006084 /*
6085 * The interleaving of transformations and verdicts
6086 * makes it difficult to decide to continue or stop
6087 * the evaluation.
6088 */
6089
Willy Tarreau6c123b12010-01-28 20:22:06 +01006090 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6091 break;
6092
Willy Tarreau3d300592007-03-18 18:34:41 +01006093 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006094 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006095 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006096 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006097
6098 /* if this filter had a condition, evaluate it now and skip to
6099 * next filter if the condition does not match.
6100 */
6101 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006102 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006103 ret = acl_pass(ret);
6104 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6105 ret = !ret;
6106
6107 if (!ret)
6108 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006109 }
6110
6111 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006112 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006113 if (unlikely(ret < 0))
6114 return -1;
6115
6116 if (likely(ret == 0)) {
6117 /* The filter did not match the request, it can be
6118 * iterated through all headers.
6119 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006120 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006121 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006122 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006123 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006124}
6125
6126
Willy Tarreaua15645d2007-03-18 16:22:39 +01006127
Willy Tarreau58f10d72006-12-04 02:26:12 +01006128/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006129 * Try to retrieve the server associated to the appsession.
6130 * If the server is found, it's assigned to the session.
6131 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006132void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006133 struct http_txn *txn = &t->txn;
6134 appsess *asession = NULL;
6135 char *sessid_temp = NULL;
6136
Cyril Bontéb21570a2009-11-29 20:04:48 +01006137 if (len > t->be->appsession_len) {
6138 len = t->be->appsession_len;
6139 }
6140
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006141 if (t->be->options2 & PR_O2_AS_REQL) {
6142 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006143 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006144 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006145 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006146 }
6147
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006148 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006149 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6150 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6151 return;
6152 }
6153
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006154 memcpy(txn->sessid, buf, len);
6155 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006156 }
6157
6158 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6159 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6160 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6161 return;
6162 }
6163
Cyril Bontéb21570a2009-11-29 20:04:48 +01006164 memcpy(sessid_temp, buf, len);
6165 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006166
6167 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6168 /* free previously allocated memory */
6169 pool_free2(apools.sessid, sessid_temp);
6170
6171 if (asession != NULL) {
6172 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6173 if (!(t->be->options2 & PR_O2_AS_REQL))
6174 asession->request_count++;
6175
6176 if (asession->serverid != NULL) {
6177 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006178
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006179 while (srv) {
6180 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006181 if ((srv->state & SRV_RUNNING) ||
6182 (t->be->options & PR_O_PERSIST) ||
6183 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006184 /* we found the server and it's usable */
6185 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006186 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006187 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006188 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006189
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006190 break;
6191 } else {
6192 txn->flags &= ~TX_CK_MASK;
6193 txn->flags |= TX_CK_DOWN;
6194 }
6195 }
6196 srv = srv->next;
6197 }
6198 }
6199 }
6200}
6201
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202/* Find the end of a cookie value contained between <s> and <e>. It works the
6203 * same way as with headers above except that the semi-colon also ends a token.
6204 * See RFC2965 for more information. Note that it requires a valid header to
6205 * return a valid result.
6206 */
6207char *find_cookie_value_end(char *s, const char *e)
6208{
6209 int quoted, qdpair;
6210
6211 quoted = qdpair = 0;
6212 for (; s < e; s++) {
6213 if (qdpair) qdpair = 0;
6214 else if (quoted) {
6215 if (*s == '\\') qdpair = 1;
6216 else if (*s == '"') quoted = 0;
6217 }
6218 else if (*s == '"') quoted = 1;
6219 else if (*s == ',' || *s == ';') return s;
6220 }
6221 return s;
6222}
6223
6224/* Delete a value in a header between delimiters <from> and <next> in buffer
6225 * <buf>. The number of characters displaced is returned, and the pointer to
6226 * the first delimiter is updated if required. The function tries as much as
6227 * possible to respect the following principles :
6228 * - replace <from> delimiter by the <next> one unless <from> points to a
6229 * colon, in which case <next> is simply removed
6230 * - set exactly one space character after the new first delimiter, unless
6231 * there are not enough characters in the block being moved to do so.
6232 * - remove unneeded spaces before the previous delimiter and after the new
6233 * one.
6234 *
6235 * It is the caller's responsibility to ensure that :
6236 * - <from> points to a valid delimiter or the colon ;
6237 * - <next> points to a valid delimiter or the final CR/LF ;
6238 * - there are non-space chars before <from> ;
6239 * - there is a CR/LF at or after <next>.
6240 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006241int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006242{
6243 char *prev = *from;
6244
6245 if (*prev == ':') {
6246 /* We're removing the first value, preserve the colon and add a
6247 * space if possible.
6248 */
6249 if (!http_is_crlf[(unsigned char)*next])
6250 next++;
6251 prev++;
6252 if (prev < next)
6253 *prev++ = ' ';
6254
6255 while (http_is_spht[(unsigned char)*next])
6256 next++;
6257 } else {
6258 /* Remove useless spaces before the old delimiter. */
6259 while (http_is_spht[(unsigned char)*(prev-1)])
6260 prev--;
6261 *from = prev;
6262
6263 /* copy the delimiter and if possible a space if we're
6264 * not at the end of the line.
6265 */
6266 if (!http_is_crlf[(unsigned char)*next]) {
6267 *prev++ = *next++;
6268 if (prev + 1 < next)
6269 *prev++ = ' ';
6270 while (http_is_spht[(unsigned char)*next])
6271 next++;
6272 }
6273 }
6274 return buffer_replace2(buf, prev, next, NULL, 0);
6275}
6276
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006277/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006278 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006279 * desirable to call it only when needed. This code is quite complex because
6280 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6281 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006282 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006283void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006284{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006285 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006286 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006287 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006288 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6289 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006290
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006291 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006292 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006293 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006294
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006295 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006297 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006298
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006299 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006300 hdr_beg = hdr_next;
6301 hdr_end = hdr_beg + cur_hdr->len;
6302 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006303
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006304 /* We have one full header between hdr_beg and hdr_end, and the
6305 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006306 * "Cookie:" headers.
6307 */
6308
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006309 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006310 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006311 old_idx = cur_idx;
6312 continue;
6313 }
6314
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006315 del_from = NULL; /* nothing to be deleted */
6316 preserve_hdr = 0; /* assume we may kill the whole header */
6317
Willy Tarreau58f10d72006-12-04 02:26:12 +01006318 /* Now look for cookies. Conforming to RFC2109, we have to support
6319 * attributes whose name begin with a '$', and associate them with
6320 * the right cookie, if we want to delete this cookie.
6321 * So there are 3 cases for each cookie read :
6322 * 1) it's a special attribute, beginning with a '$' : ignore it.
6323 * 2) it's a server id cookie that we *MAY* want to delete : save
6324 * some pointers on it (last semi-colon, beginning of cookie...)
6325 * 3) it's an application cookie : we *MAY* have to delete a previous
6326 * "special" cookie.
6327 * At the end of loop, if a "special" cookie remains, we may have to
6328 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006329 * *MUST* delete it.
6330 *
6331 * Note: RFC2965 is unclear about the processing of spaces around
6332 * the equal sign in the ATTR=VALUE form. A careful inspection of
6333 * the RFC explicitly allows spaces before it, and not within the
6334 * tokens (attrs or values). An inspection of RFC2109 allows that
6335 * too but section 10.1.3 lets one think that spaces may be allowed
6336 * after the equal sign too, resulting in some (rare) buggy
6337 * implementations trying to do that. So let's do what servers do.
6338 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6339 * allowed quoted strings in values, with any possible character
6340 * after a backslash, including control chars and delimitors, which
6341 * causes parsing to become ambiguous. Browsers also allow spaces
6342 * within values even without quotes.
6343 *
6344 * We have to keep multiple pointers in order to support cookie
6345 * removal at the beginning, middle or end of header without
6346 * corrupting the header. All of these headers are valid :
6347 *
6348 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6349 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6350 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6351 * | | | | | | | | |
6352 * | | | | | | | | hdr_end <--+
6353 * | | | | | | | +--> next
6354 * | | | | | | +----> val_end
6355 * | | | | | +-----------> val_beg
6356 * | | | | +--------------> equal
6357 * | | | +----------------> att_end
6358 * | | +---------------------> att_beg
6359 * | +--------------------------> prev
6360 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006361 */
6362
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006363 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6364 /* Iterate through all cookies on this line */
6365
6366 /* find att_beg */
6367 att_beg = prev + 1;
6368 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6369 att_beg++;
6370
6371 /* find att_end : this is the first character after the last non
6372 * space before the equal. It may be equal to hdr_end.
6373 */
6374 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006375
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006376 while (equal < hdr_end) {
6377 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006378 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006379 if (http_is_spht[(unsigned char)*equal++])
6380 continue;
6381 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006382 }
6383
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006384 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6385 * is between <att_beg> and <equal>, both may be identical.
6386 */
6387
6388 /* look for end of cookie if there is an equal sign */
6389 if (equal < hdr_end && *equal == '=') {
6390 /* look for the beginning of the value */
6391 val_beg = equal + 1;
6392 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6393 val_beg++;
6394
6395 /* find the end of the value, respecting quotes */
6396 next = find_cookie_value_end(val_beg, hdr_end);
6397
6398 /* make val_end point to the first white space or delimitor after the value */
6399 val_end = next;
6400 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6401 val_end--;
6402 } else {
6403 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006404 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006405
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006406 /* We have nothing to do with attributes beginning with '$'. However,
6407 * they will automatically be removed if a header before them is removed,
6408 * since they're supposed to be linked together.
6409 */
6410 if (*att_beg == '$')
6411 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006412
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006413 /* Ignore cookies with no equal sign */
6414 if (equal == next) {
6415 /* This is not our cookie, so we must preserve it. But if we already
6416 * scheduled another cookie for removal, we cannot remove the
6417 * complete header, but we can remove the previous block itself.
6418 */
6419 preserve_hdr = 1;
6420 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006421 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006422 val_end += delta;
6423 next += delta;
6424 hdr_end += delta;
6425 hdr_next += delta;
6426 cur_hdr->len += delta;
6427 http_msg_move_end(&txn->req, delta);
6428 prev = del_from;
6429 del_from = NULL;
6430 }
6431 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006432 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006433
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006434 /* if there are spaces around the equal sign, we need to
6435 * strip them otherwise we'll get trouble for cookie captures,
6436 * or even for rewrites. Since this happens extremely rarely,
6437 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006438 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006439 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6440 int stripped_before = 0;
6441 int stripped_after = 0;
6442
6443 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006444 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006445 equal += stripped_before;
6446 val_beg += stripped_before;
6447 }
6448
6449 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006450 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006451 val_beg += stripped_after;
6452 stripped_before += stripped_after;
6453 }
6454
6455 val_end += stripped_before;
6456 next += stripped_before;
6457 hdr_end += stripped_before;
6458 hdr_next += stripped_before;
6459 cur_hdr->len += stripped_before;
6460 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006461 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006462 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006463
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006464 /* First, let's see if we want to capture this cookie. We check
6465 * that we don't already have a client side cookie, because we
6466 * can only capture one. Also as an optimisation, we ignore
6467 * cookies shorter than the declared name.
6468 */
6469 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6470 (val_end - att_beg >= t->fe->capture_namelen) &&
6471 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6472 int log_len = val_end - att_beg;
6473
6474 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6475 Alert("HTTP logging : out of memory.\n");
6476 } else {
6477 if (log_len > t->fe->capture_len)
6478 log_len = t->fe->capture_len;
6479 memcpy(txn->cli_cookie, att_beg, log_len);
6480 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006481 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006482 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006483
Willy Tarreaubca99692010-10-06 19:25:55 +02006484 /* Persistence cookies in passive, rewrite or insert mode have the
6485 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006486 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006487 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006488 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006489 * For cookies in prefix mode, the form is :
6490 *
6491 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006492 */
6493 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6494 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6495 struct server *srv = t->be->srv;
6496 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006497
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006498 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6499 * have the server ID between val_beg and delim, and the original cookie between
6500 * delim+1 and val_end. Otherwise, delim==val_end :
6501 *
6502 * Cookie: NAME=SRV; # in all but prefix modes
6503 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6504 * | || || | |+-> next
6505 * | || || | +--> val_end
6506 * | || || +---------> delim
6507 * | || |+------------> val_beg
6508 * | || +-------------> att_end = equal
6509 * | |+-----------------> att_beg
6510 * | +------------------> prev
6511 * +-------------------------> hdr_beg
6512 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006513
Willy Tarreau67402132012-05-31 20:40:20 +02006514 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006515 for (delim = val_beg; delim < val_end; delim++)
6516 if (*delim == COOKIE_DELIM)
6517 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006518 } else {
6519 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006520 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006521 /* Now check if the cookie contains a date field, which would
6522 * appear after a vertical bar ('|') just after the server name
6523 * and before the delimiter.
6524 */
6525 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6526 if (vbar1) {
6527 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006528 * right is the last seen date. It is a base64 encoded
6529 * 30-bit value representing the UNIX date since the
6530 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006531 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006532 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006533 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006534 if (val_end - vbar1 >= 5) {
6535 val = b64tos30(vbar1);
6536 if (val > 0)
6537 txn->cookie_last_date = val << 2;
6538 }
6539 /* look for a second vertical bar */
6540 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6541 if (vbar1 && (val_end - vbar1 > 5)) {
6542 val = b64tos30(vbar1 + 1);
6543 if (val > 0)
6544 txn->cookie_first_date = val << 2;
6545 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006546 }
6547 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006548
Willy Tarreauf64d1412010-10-07 20:06:11 +02006549 /* if the cookie has an expiration date and the proxy wants to check
6550 * it, then we do that now. We first check if the cookie is too old,
6551 * then only if it has expired. We detect strict overflow because the
6552 * time resolution here is not great (4 seconds). Cookies with dates
6553 * in the future are ignored if their offset is beyond one day. This
6554 * allows an admin to fix timezone issues without expiring everyone
6555 * and at the same time avoids keeping unwanted side effects for too
6556 * long.
6557 */
6558 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006559 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6560 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006561 txn->flags &= ~TX_CK_MASK;
6562 txn->flags |= TX_CK_OLD;
6563 delim = val_beg; // let's pretend we have not found the cookie
6564 txn->cookie_first_date = 0;
6565 txn->cookie_last_date = 0;
6566 }
6567 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006568 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6569 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006570 txn->flags &= ~TX_CK_MASK;
6571 txn->flags |= TX_CK_EXPIRED;
6572 delim = val_beg; // let's pretend we have not found the cookie
6573 txn->cookie_first_date = 0;
6574 txn->cookie_last_date = 0;
6575 }
6576
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006577 /* Here, we'll look for the first running server which supports the cookie.
6578 * This allows to share a same cookie between several servers, for example
6579 * to dedicate backup servers to specific servers only.
6580 * However, to prevent clients from sticking to cookie-less backup server
6581 * when they have incidentely learned an empty cookie, we simply ignore
6582 * empty cookies and mark them as invalid.
6583 * The same behaviour is applied when persistence must be ignored.
6584 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006585 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006586 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006587
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006588 while (srv) {
6589 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6590 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6591 if ((srv->state & SRV_RUNNING) ||
6592 (t->be->options & PR_O_PERSIST) ||
6593 (t->flags & SN_FORCE_PRST)) {
6594 /* we found the server and we can use it */
6595 txn->flags &= ~TX_CK_MASK;
6596 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6597 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006598 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006599 break;
6600 } else {
6601 /* we found a server, but it's down,
6602 * mark it as such and go on in case
6603 * another one is available.
6604 */
6605 txn->flags &= ~TX_CK_MASK;
6606 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006607 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006608 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006609 srv = srv->next;
6610 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006611
Willy Tarreauf64d1412010-10-07 20:06:11 +02006612 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006613 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006614 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006615 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6616 txn->flags |= TX_CK_UNUSED;
6617 else
6618 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006619 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006620
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006621 /* depending on the cookie mode, we may have to either :
6622 * - delete the complete cookie if we're in insert+indirect mode, so that
6623 * the server never sees it ;
6624 * - remove the server id from the cookie value, and tag the cookie as an
6625 * application cookie so that it does not get accidentely removed later,
6626 * if we're in cookie prefix mode
6627 */
Willy Tarreau67402132012-05-31 20:40:20 +02006628 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006629 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006630
Willy Tarreau9b28e032012-10-12 23:49:43 +02006631 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006632 val_end += delta;
6633 next += delta;
6634 hdr_end += delta;
6635 hdr_next += delta;
6636 cur_hdr->len += delta;
6637 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006638
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006639 del_from = NULL;
6640 preserve_hdr = 1; /* we want to keep this cookie */
6641 }
6642 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006643 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006644 del_from = prev;
6645 }
6646 } else {
6647 /* This is not our cookie, so we must preserve it. But if we already
6648 * scheduled another cookie for removal, we cannot remove the
6649 * complete header, but we can remove the previous block itself.
6650 */
6651 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006652
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006653 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006654 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006655 if (att_beg >= del_from)
6656 att_beg += delta;
6657 if (att_end >= del_from)
6658 att_end += delta;
6659 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006660 val_end += delta;
6661 next += delta;
6662 hdr_end += delta;
6663 hdr_next += delta;
6664 cur_hdr->len += delta;
6665 http_msg_move_end(&txn->req, delta);
6666 prev = del_from;
6667 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006668 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006669 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006670
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006671 /* Look for the appsession cookie unless persistence must be ignored */
6672 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6673 int cmp_len, value_len;
6674 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006675
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006676 if (t->be->options2 & PR_O2_AS_PFX) {
6677 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6678 value_begin = att_beg + t->be->appsession_name_len;
6679 value_len = val_end - att_beg - t->be->appsession_name_len;
6680 } else {
6681 cmp_len = att_end - att_beg;
6682 value_begin = val_beg;
6683 value_len = val_end - val_beg;
6684 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006685
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006686 /* let's see if the cookie is our appcookie */
6687 if (cmp_len == t->be->appsession_name_len &&
6688 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6689 manage_client_side_appsession(t, value_begin, value_len);
6690 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006691 }
6692
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006693 /* continue with next cookie on this header line */
6694 att_beg = next;
6695 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006696
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006697 /* There are no more cookies on this line.
6698 * We may still have one (or several) marked for deletion at the
6699 * end of the line. We must do this now in two ways :
6700 * - if some cookies must be preserved, we only delete from the
6701 * mark to the end of line ;
6702 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006703 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006704 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006705 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006706 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006707 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006708 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006709 cur_hdr->len += delta;
6710 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006711 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006712
6713 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006714 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6715 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006716 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006717 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006718 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006719 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006720 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006721 }
6722
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006723 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006724 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006725 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006726}
6727
6728
Willy Tarreaua15645d2007-03-18 16:22:39 +01006729/* Iterate the same filter through all response headers contained in <rtr>.
6730 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6731 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006732int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006733{
6734 char term;
6735 char *cur_ptr, *cur_end, *cur_next;
6736 int cur_idx, old_idx, last_hdr;
6737 struct http_txn *txn = &t->txn;
6738 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006739 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006740
6741 last_hdr = 0;
6742
Willy Tarreau9b28e032012-10-12 23:49:43 +02006743 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744 old_idx = 0;
6745
6746 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006747 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006748 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006749 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750 (exp->action == ACT_ALLOW ||
6751 exp->action == ACT_DENY))
6752 return 0;
6753
6754 cur_idx = txn->hdr_idx.v[old_idx].next;
6755 if (!cur_idx)
6756 break;
6757
6758 cur_hdr = &txn->hdr_idx.v[cur_idx];
6759 cur_ptr = cur_next;
6760 cur_end = cur_ptr + cur_hdr->len;
6761 cur_next = cur_end + cur_hdr->cr + 1;
6762
6763 /* Now we have one header between cur_ptr and cur_end,
6764 * and the next header starts at cur_next.
6765 */
6766
6767 /* The annoying part is that pattern matching needs
6768 * that we modify the contents to null-terminate all
6769 * strings before testing them.
6770 */
6771
6772 term = *cur_end;
6773 *cur_end = '\0';
6774
6775 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6776 switch (exp->action) {
6777 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006778 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006779 last_hdr = 1;
6780 break;
6781
6782 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006783 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006784 last_hdr = 1;
6785 break;
6786
6787 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006788 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6789 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790 /* FIXME: if the user adds a newline in the replacement, the
6791 * index will not be recalculated for now, and the new line
6792 * will not be counted as a new header.
6793 */
6794
6795 cur_end += delta;
6796 cur_next += delta;
6797 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006798 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799 break;
6800
6801 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006802 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 cur_next += delta;
6804
Willy Tarreaufa355d42009-11-29 18:12:29 +01006805 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6807 txn->hdr_idx.used--;
6808 cur_hdr->len = 0;
6809 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006810 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811 break;
6812
6813 }
6814 }
6815 if (cur_end)
6816 *cur_end = term; /* restore the string terminator */
6817
6818 /* keep the link from this header to next one in case of later
6819 * removal of next header.
6820 */
6821 old_idx = cur_idx;
6822 }
6823 return 0;
6824}
6825
6826
6827/* Apply the filter to the status line in the response buffer <rtr>.
6828 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6829 * or -1 if a replacement resulted in an invalid status line.
6830 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006831int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006832{
6833 char term;
6834 char *cur_ptr, *cur_end;
6835 int done;
6836 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006837 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838
6839
Willy Tarreau3d300592007-03-18 18:34:41 +01006840 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006842 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006843 (exp->action == ACT_ALLOW ||
6844 exp->action == ACT_DENY))
6845 return 0;
6846 else if (exp->action == ACT_REMOVE)
6847 return 0;
6848
6849 done = 0;
6850
Willy Tarreau9b28e032012-10-12 23:49:43 +02006851 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006852 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006853
6854 /* Now we have the status line between cur_ptr and cur_end */
6855
6856 /* The annoying part is that pattern matching needs
6857 * that we modify the contents to null-terminate all
6858 * strings before testing them.
6859 */
6860
6861 term = *cur_end;
6862 *cur_end = '\0';
6863
6864 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6865 switch (exp->action) {
6866 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006867 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006868 done = 1;
6869 break;
6870
6871 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006872 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873 done = 1;
6874 break;
6875
6876 case ACT_REPLACE:
6877 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006878 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6879 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880 /* FIXME: if the user adds a newline in the replacement, the
6881 * index will not be recalculated for now, and the new line
6882 * will not be counted as a new header.
6883 */
6884
Willy Tarreaufa355d42009-11-29 18:12:29 +01006885 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006886 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006887 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006888 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006889 cur_ptr, cur_end + 1,
6890 NULL, NULL);
6891 if (unlikely(!cur_end))
6892 return -1;
6893
6894 /* we have a full respnse and we know that we have either a CR
6895 * or an LF at <ptr>.
6896 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006897 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006898 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006899 /* there is no point trying this regex on headers */
6900 return 1;
6901 }
6902 }
6903 *cur_end = term; /* restore the string terminator */
6904 return done;
6905}
6906
6907
6908
6909/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006910 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006911 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6912 * unparsable response.
6913 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006914int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006915{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006916 struct http_txn *txn = &s->txn;
6917 struct hdr_exp *exp;
6918
6919 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006920 int ret;
6921
6922 /*
6923 * The interleaving of transformations and verdicts
6924 * makes it difficult to decide to continue or stop
6925 * the evaluation.
6926 */
6927
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006928 if (txn->flags & TX_SVDENY)
6929 break;
6930
Willy Tarreau3d300592007-03-18 18:34:41 +01006931 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006932 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6933 exp->action == ACT_PASS)) {
6934 exp = exp->next;
6935 continue;
6936 }
6937
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006938 /* if this filter had a condition, evaluate it now and skip to
6939 * next filter if the condition does not match.
6940 */
6941 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006942 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006943 ret = acl_pass(ret);
6944 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6945 ret = !ret;
6946 if (!ret)
6947 continue;
6948 }
6949
Willy Tarreaua15645d2007-03-18 16:22:39 +01006950 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006951 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006952 if (unlikely(ret < 0))
6953 return -1;
6954
6955 if (likely(ret == 0)) {
6956 /* The filter did not match the response, it can be
6957 * iterated through all headers.
6958 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006959 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006960 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006961 }
6962 return 0;
6963}
6964
6965
Willy Tarreaua15645d2007-03-18 16:22:39 +01006966/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006967 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006968 * desirable to call it only when needed. This function is also used when we
6969 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006970 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006971void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006972{
6973 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006974 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006975 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006976 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006977 char *hdr_beg, *hdr_end, *hdr_next;
6978 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979
Willy Tarreaua15645d2007-03-18 16:22:39 +01006980 /* Iterate through the headers.
6981 * we start with the start line.
6982 */
6983 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006984 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985
6986 while ((cur_idx = txn->hdr_idx.v[old_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];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006991 hdr_beg = hdr_next;
6992 hdr_end = hdr_beg + cur_hdr->len;
6993 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006994
Willy Tarreau24581ba2010-08-31 22:39:35 +02006995 /* We have one full header between hdr_beg and hdr_end, and the
6996 * next header starts at hdr_next. We're only interested in
6997 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006998 */
6999
Willy Tarreau24581ba2010-08-31 22:39:35 +02007000 is_cookie2 = 0;
7001 prev = hdr_beg + 10;
7002 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007003 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007004 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7005 if (!val) {
7006 old_idx = cur_idx;
7007 continue;
7008 }
7009 is_cookie2 = 1;
7010 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011 }
7012
Willy Tarreau24581ba2010-08-31 22:39:35 +02007013 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7014 * <prev> points to the colon.
7015 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007016 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007017
Willy Tarreau24581ba2010-08-31 22:39:35 +02007018 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7019 * check-cache is enabled) and we are not interested in checking
7020 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007021 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007022 if (t->be->cookie_name == NULL &&
7023 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007024 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007025 return;
7026
Willy Tarreau24581ba2010-08-31 22:39:35 +02007027 /* OK so now we know we have to process this response cookie.
7028 * The format of the Set-Cookie header is slightly different
7029 * from the format of the Cookie header in that it does not
7030 * support the comma as a cookie delimiter (thus the header
7031 * cannot be folded) because the Expires attribute described in
7032 * the original Netscape's spec may contain an unquoted date
7033 * with a comma inside. We have to live with this because
7034 * many browsers don't support Max-Age and some browsers don't
7035 * support quoted strings. However the Set-Cookie2 header is
7036 * clean.
7037 *
7038 * We have to keep multiple pointers in order to support cookie
7039 * removal at the beginning, middle or end of header without
7040 * corrupting the header (in case of set-cookie2). A special
7041 * pointer, <scav> points to the beginning of the set-cookie-av
7042 * fields after the first semi-colon. The <next> pointer points
7043 * either to the end of line (set-cookie) or next unquoted comma
7044 * (set-cookie2). All of these headers are valid :
7045 *
7046 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7047 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7048 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7049 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7050 * | | | | | | | | | |
7051 * | | | | | | | | +-> next hdr_end <--+
7052 * | | | | | | | +------------> scav
7053 * | | | | | | +--------------> val_end
7054 * | | | | | +--------------------> val_beg
7055 * | | | | +----------------------> equal
7056 * | | | +------------------------> att_end
7057 * | | +----------------------------> att_beg
7058 * | +------------------------------> prev
7059 * +-----------------------------------------> hdr_beg
7060 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007061
Willy Tarreau24581ba2010-08-31 22:39:35 +02007062 for (; prev < hdr_end; prev = next) {
7063 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007064
Willy Tarreau24581ba2010-08-31 22:39:35 +02007065 /* find att_beg */
7066 att_beg = prev + 1;
7067 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7068 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069
Willy Tarreau24581ba2010-08-31 22:39:35 +02007070 /* find att_end : this is the first character after the last non
7071 * space before the equal. It may be equal to hdr_end.
7072 */
7073 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007074
Willy Tarreau24581ba2010-08-31 22:39:35 +02007075 while (equal < hdr_end) {
7076 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7077 break;
7078 if (http_is_spht[(unsigned char)*equal++])
7079 continue;
7080 att_end = equal;
7081 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007082
Willy Tarreau24581ba2010-08-31 22:39:35 +02007083 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7084 * is between <att_beg> and <equal>, both may be identical.
7085 */
7086
7087 /* look for end of cookie if there is an equal sign */
7088 if (equal < hdr_end && *equal == '=') {
7089 /* look for the beginning of the value */
7090 val_beg = equal + 1;
7091 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7092 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007093
Willy Tarreau24581ba2010-08-31 22:39:35 +02007094 /* find the end of the value, respecting quotes */
7095 next = find_cookie_value_end(val_beg, hdr_end);
7096
7097 /* make val_end point to the first white space or delimitor after the value */
7098 val_end = next;
7099 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7100 val_end--;
7101 } else {
7102 /* <equal> points to next comma, semi-colon or EOL */
7103 val_beg = val_end = next = equal;
7104 }
7105
7106 if (next < hdr_end) {
7107 /* Set-Cookie2 supports multiple cookies, and <next> points to
7108 * a colon or semi-colon before the end. So skip all attr-value
7109 * pairs and look for the next comma. For Set-Cookie, since
7110 * commas are permitted in values, skip to the end.
7111 */
7112 if (is_cookie2)
7113 next = find_hdr_value_end(next, hdr_end);
7114 else
7115 next = hdr_end;
7116 }
7117
7118 /* Now everything is as on the diagram above */
7119
7120 /* Ignore cookies with no equal sign */
7121 if (equal == val_end)
7122 continue;
7123
7124 /* If there are spaces around the equal sign, we need to
7125 * strip them otherwise we'll get trouble for cookie captures,
7126 * or even for rewrites. Since this happens extremely rarely,
7127 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007129 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7130 int stripped_before = 0;
7131 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132
Willy Tarreau24581ba2010-08-31 22:39:35 +02007133 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007134 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007135 equal += stripped_before;
7136 val_beg += stripped_before;
7137 }
7138
7139 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007140 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007141 val_beg += stripped_after;
7142 stripped_before += stripped_after;
7143 }
7144
7145 val_end += stripped_before;
7146 next += stripped_before;
7147 hdr_end += stripped_before;
7148 hdr_next += stripped_before;
7149 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007150 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007151 }
7152
7153 /* First, let's see if we want to capture this cookie. We check
7154 * that we don't already have a server side cookie, because we
7155 * can only capture one. Also as an optimisation, we ignore
7156 * cookies shorter than the declared name.
7157 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007158 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007159 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007160 (val_end - att_beg >= t->fe->capture_namelen) &&
7161 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7162 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007163 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007164 Alert("HTTP logging : out of memory.\n");
7165 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007166 else {
7167 if (log_len > t->fe->capture_len)
7168 log_len = t->fe->capture_len;
7169 memcpy(txn->srv_cookie, att_beg, log_len);
7170 txn->srv_cookie[log_len] = 0;
7171 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007172 }
7173
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007174 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007175 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007176 if (!(t->flags & SN_IGNORE_PRST) &&
7177 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7178 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007179 /* assume passive cookie by default */
7180 txn->flags &= ~TX_SCK_MASK;
7181 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007182
7183 /* If the cookie is in insert mode on a known server, we'll delete
7184 * this occurrence because we'll insert another one later.
7185 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007186 * a direct access.
7187 */
Willy Tarreau67402132012-05-31 20:40:20 +02007188 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007189 /* The "preserve" flag was set, we don't want to touch the
7190 * server's cookie.
7191 */
7192 }
Willy Tarreau67402132012-05-31 20:40:20 +02007193 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7194 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007195 /* this cookie must be deleted */
7196 if (*prev == ':' && next == hdr_end) {
7197 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007198 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007199 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7200 txn->hdr_idx.used--;
7201 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007202 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007203 hdr_next += delta;
7204 http_msg_move_end(&txn->rsp, delta);
7205 /* note: while both invalid now, <next> and <hdr_end>
7206 * are still equal, so the for() will stop as expected.
7207 */
7208 } else {
7209 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007210 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007211 next = prev;
7212 hdr_end += delta;
7213 hdr_next += delta;
7214 cur_hdr->len += delta;
7215 http_msg_move_end(&txn->rsp, delta);
7216 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007217 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007218 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007219 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007220 }
Willy Tarreau67402132012-05-31 20:40:20 +02007221 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007222 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223 * with this server since we know it.
7224 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007225 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007226 next += delta;
7227 hdr_end += delta;
7228 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007230 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231
Willy Tarreauf1348312010-10-07 15:54:11 +02007232 txn->flags &= ~TX_SCK_MASK;
7233 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007234 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007235 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007237 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007238 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007239 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007240 next += delta;
7241 hdr_end += delta;
7242 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007243 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007244 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007245
Willy Tarreau827aee92011-03-10 16:55:02 +01007246 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007247 txn->flags &= ~TX_SCK_MASK;
7248 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007249 }
7250 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007251 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7252 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007253 int cmp_len, value_len;
7254 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255
Cyril Bontéb21570a2009-11-29 20:04:48 +01007256 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007257 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7258 value_begin = att_beg + t->be->appsession_name_len;
7259 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007260 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007261 cmp_len = att_end - att_beg;
7262 value_begin = val_beg;
7263 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007264 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007265
Cyril Bonté17530c32010-04-06 21:11:10 +02007266 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007267 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7268 /* free a possibly previously allocated memory */
7269 pool_free2(apools.sessid, txn->sessid);
7270
Cyril Bontéb21570a2009-11-29 20:04:48 +01007271 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007272 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007273 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7274 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7275 return;
7276 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007277 memcpy(txn->sessid, value_begin, value_len);
7278 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007279 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007280 }
7281 /* that's done for this cookie, check the next one on the same
7282 * line when next != hdr_end (only if is_cookie2).
7283 */
7284 }
7285 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007286 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007287 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007288
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007289 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007290 appsess *asession = NULL;
7291 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007292 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007293 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007294 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007295 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7296 Alert("Not enough Memory process_srv():asession:calloc().\n");
7297 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7298 return;
7299 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007300 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7301
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007302 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7303 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7304 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007305 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007306 return;
7307 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007308 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007309 asession->sessid[t->be->appsession_len] = 0;
7310
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007311 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007312 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007313 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007314 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007315 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007316 return;
7317 }
7318 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007319 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007320
7321 asession->request_count = 0;
7322 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7323 }
7324
7325 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7326 asession->request_count++;
7327 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007328}
7329
7330
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331/*
7332 * Check if response is cacheable or not. Updates t->flags.
7333 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007334void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007335{
7336 struct http_txn *txn = &t->txn;
7337 char *p1, *p2;
7338
7339 char *cur_ptr, *cur_end, *cur_next;
7340 int cur_idx;
7341
Willy Tarreau5df51872007-11-25 16:20:08 +01007342 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 return;
7344
7345 /* Iterate through the headers.
7346 * we start with the start line.
7347 */
7348 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007349 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350
7351 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7352 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007353 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007354
7355 cur_hdr = &txn->hdr_idx.v[cur_idx];
7356 cur_ptr = cur_next;
7357 cur_end = cur_ptr + cur_hdr->len;
7358 cur_next = cur_end + cur_hdr->cr + 1;
7359
7360 /* We have one full header between cur_ptr and cur_end, and the
7361 * next header starts at cur_next. We're only interested in
7362 * "Cookie:" headers.
7363 */
7364
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007365 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7366 if (val) {
7367 if ((cur_end - (cur_ptr + val) >= 8) &&
7368 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7369 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7370 return;
7371 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007372 }
7373
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007374 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7375 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007376 continue;
7377
7378 /* OK, right now we know we have a cache-control header at cur_ptr */
7379
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007380 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007381
7382 if (p1 >= cur_end) /* no more info */
7383 continue;
7384
7385 /* p1 is at the beginning of the value */
7386 p2 = p1;
7387
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007388 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007389 p2++;
7390
7391 /* we have a complete value between p1 and p2 */
7392 if (p2 < cur_end && *p2 == '=') {
7393 /* we have something of the form no-cache="set-cookie" */
7394 if ((cur_end - p1 >= 21) &&
7395 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7396 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007397 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007398 continue;
7399 }
7400
7401 /* OK, so we know that either p2 points to the end of string or to a comma */
7402 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7403 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7404 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7405 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007406 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007407 return;
7408 }
7409
7410 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007411 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007412 continue;
7413 }
7414 }
7415}
7416
7417
Willy Tarreau58f10d72006-12-04 02:26:12 +01007418/*
7419 * Try to retrieve a known appsession in the URI, then the associated server.
7420 * If the server is found, it's assigned to the session.
7421 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007422void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007423{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007424 char *end_params, *first_param, *cur_param, *next_param;
7425 char separator;
7426 int value_len;
7427
7428 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007429
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007430 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007431 (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 +01007432 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007434
Cyril Bontéb21570a2009-11-29 20:04:48 +01007435 first_param = NULL;
7436 switch (mode) {
7437 case PR_O2_AS_M_PP:
7438 first_param = memchr(begin, ';', len);
7439 break;
7440 case PR_O2_AS_M_QS:
7441 first_param = memchr(begin, '?', len);
7442 break;
7443 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007444
Cyril Bontéb21570a2009-11-29 20:04:48 +01007445 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007446 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007447 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007448
Cyril Bontéb21570a2009-11-29 20:04:48 +01007449 switch (mode) {
7450 case PR_O2_AS_M_PP:
7451 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7452 end_params = (char *) begin + len;
7453 }
7454 separator = ';';
7455 break;
7456 case PR_O2_AS_M_QS:
7457 end_params = (char *) begin + len;
7458 separator = '&';
7459 break;
7460 default:
7461 /* unknown mode, shouldn't happen */
7462 return;
7463 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007464
Cyril Bontéb21570a2009-11-29 20:04:48 +01007465 cur_param = next_param = end_params;
7466 while (cur_param > first_param) {
7467 cur_param--;
7468 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7469 /* let's see if this is the appsession parameter */
7470 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7471 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7472 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7473 /* Cool... it's the right one */
7474 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7475 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7476 if (value_len > 0) {
7477 manage_client_side_appsession(t, cur_param, value_len);
7478 }
7479 break;
7480 }
7481 next_param = cur_param;
7482 }
7483 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007484#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007485 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007486 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007487#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007488}
7489
Willy Tarreaub2513902006-12-17 14:52:38 +01007490/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007491 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007492 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007493 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007494 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007495 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007496 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007497 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007498 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007499int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007500{
7501 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007502 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007503 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007504 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007505
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007506 if (!uri_auth)
7507 return 0;
7508
Cyril Bonté70be45d2010-10-12 00:14:35 +02007509 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007510 return 0;
7511
Willy Tarreau295a8372011-03-10 11:25:07 +01007512 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007513 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007514
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007515 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007516 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007517 return 0;
7518
Willy Tarreau3a215be2012-03-09 21:39:51 +01007519 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007520 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007521 return 0;
7522
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007523 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007524 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007525 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007526 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007527 break;
7528 }
7529 h++;
7530 }
7531
7532 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007533 h = uri + uri_auth->uri_len;
7534 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007535 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007536 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007537 break;
7538 }
7539 h++;
7540 }
7541 }
7542
Willy Tarreau3a215be2012-03-09 21:39:51 +01007543 h = uri + uri_auth->uri_len;
7544 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007545 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007546 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007547 break;
7548 }
7549 h++;
7550 }
7551
Willy Tarreau3a215be2012-03-09 21:39:51 +01007552 h = uri + uri_auth->uri_len;
7553 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007554 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007555 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007556 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007557 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007558 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7559 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7560 si->applet.ctx.stats.st_code = i;
7561 break;
7562 }
7563 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007564 break;
7565 }
7566 h++;
7567 }
7568
Willy Tarreau295a8372011-03-10 11:25:07 +01007569 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007570
Willy Tarreaub2513902006-12-17 14:52:38 +01007571 return 1;
7572}
7573
Willy Tarreau4076a152009-04-02 15:18:36 +02007574/*
7575 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007576 * By default it tries to report the error position as msg->err_pos. However if
7577 * this one is not set, it will then report msg->next, which is the last known
7578 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007579 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007580 */
7581void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007582 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007583 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007584{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007585 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007586 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007587
Willy Tarreau9b28e032012-10-12 23:49:43 +02007588 es->len = MIN(chn->buf->i, sizeof(es->buf));
7589 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007590 len1 = MIN(len1, es->len);
7591 len2 = es->len - len1; /* remaining data if buffer wraps */
7592
Willy Tarreau9b28e032012-10-12 23:49:43 +02007593 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007594 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007595 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007596
Willy Tarreau4076a152009-04-02 15:18:36 +02007597 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007598 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007599 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007600 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007601
Willy Tarreau4076a152009-04-02 15:18:36 +02007602 es->when = date; // user-visible date
7603 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007604 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007605 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007606 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007607 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007608 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007609 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007610 es->s_flags = s->flags;
7611 es->t_flags = s->txn.flags;
7612 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007613 es->b_out = chn->buf->o;
7614 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007615 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007616 es->m_clen = msg->chunk_len;
7617 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007618}
Willy Tarreaub2513902006-12-17 14:52:38 +01007619
Willy Tarreau294c4732011-12-16 21:35:50 +01007620/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7621 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7622 * performed over the whole headers. Otherwise it must contain a valid header
7623 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7624 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7625 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7626 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7627 * -1.
7628 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007629 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007630unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007631 struct hdr_idx *idx, int occ,
7632 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007633{
Willy Tarreau294c4732011-12-16 21:35:50 +01007634 struct hdr_ctx local_ctx;
7635 char *ptr_hist[MAX_HDR_HISTORY];
7636 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007637 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007638 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007639
Willy Tarreau294c4732011-12-16 21:35:50 +01007640 if (!ctx) {
7641 local_ctx.idx = 0;
7642 ctx = &local_ctx;
7643 }
7644
Willy Tarreaubce70882009-09-07 11:51:47 +02007645 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007646 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007647 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007648 occ--;
7649 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007650 *vptr = ctx->line + ctx->val;
7651 *vlen = ctx->vlen;
7652 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007653 }
7654 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007655 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007656 }
7657
7658 /* negative occurrence, we scan all the list then walk back */
7659 if (-occ > MAX_HDR_HISTORY)
7660 return 0;
7661
Willy Tarreau294c4732011-12-16 21:35:50 +01007662 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007663 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007664 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7665 len_hist[hist_ptr] = ctx->vlen;
7666 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007667 hist_ptr = 0;
7668 found++;
7669 }
7670 if (-occ > found)
7671 return 0;
7672 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7673 * find occurrence -occ, so we have to check [hist_ptr+occ].
7674 */
7675 hist_ptr += occ;
7676 if (hist_ptr >= MAX_HDR_HISTORY)
7677 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007678 *vptr = ptr_hist[hist_ptr];
7679 *vlen = len_hist[hist_ptr];
7680 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007681}
7682
Willy Tarreaubaaee002006-06-26 02:48:02 +02007683/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007684 * Print a debug line with a header. Always stop at the first CR or LF char,
7685 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7686 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007687 */
7688void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7689{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007690 int max;
7691 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01007692 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
7693 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007694
7695 for (max = 0; start + max < end; max++)
7696 if (start[max] == '\r' || start[max] == '\n')
7697 break;
7698
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007699 UBOUND(max, trash.size - trash.len - 3);
7700 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7701 trash.str[trash.len++] = '\n';
7702 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007703}
7704
Willy Tarreau0937bc42009-12-22 15:03:09 +01007705/*
7706 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7707 * the required fields are properly allocated and that we only need to (re)init
7708 * them. This should be used before processing any new request.
7709 */
7710void http_init_txn(struct session *s)
7711{
7712 struct http_txn *txn = &s->txn;
7713 struct proxy *fe = s->fe;
7714
7715 txn->flags = 0;
7716 txn->status = -1;
7717
William Lallemand5f232402012-04-05 18:02:55 +02007718 global.req_count++;
7719
Willy Tarreauf64d1412010-10-07 20:06:11 +02007720 txn->cookie_first_date = 0;
7721 txn->cookie_last_date = 0;
7722
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007723 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007724 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007725 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007726 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007727 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007728 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007729 txn->req.chunk_len = 0LL;
7730 txn->req.body_len = 0LL;
7731 txn->rsp.chunk_len = 0LL;
7732 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007733 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7734 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007735 txn->req.chn = s->req;
7736 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007737
7738 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007739
7740 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7741 if (fe->options2 & PR_O2_REQBUG_OK)
7742 txn->req.err_pos = -1; /* let buggy requests pass */
7743
Willy Tarreau46023632010-01-07 22:51:47 +01007744 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007745 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7746
Willy Tarreau46023632010-01-07 22:51:47 +01007747 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007748 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7749
7750 if (txn->hdr_idx.v)
7751 hdr_idx_init(&txn->hdr_idx);
7752}
7753
7754/* to be used at the end of a transaction */
7755void http_end_txn(struct session *s)
7756{
7757 struct http_txn *txn = &s->txn;
7758
7759 /* these ones will have been dynamically allocated */
7760 pool_free2(pool2_requri, txn->uri);
7761 pool_free2(pool2_capture, txn->cli_cookie);
7762 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007763 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007764 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007765
William Lallemanda73203e2012-03-12 12:48:57 +01007766 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007767 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007768 txn->uri = NULL;
7769 txn->srv_cookie = NULL;
7770 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007771
7772 if (txn->req.cap) {
7773 struct cap_hdr *h;
7774 for (h = s->fe->req_cap; h; h = h->next)
7775 pool_free2(h->pool, txn->req.cap[h->index]);
7776 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7777 }
7778
7779 if (txn->rsp.cap) {
7780 struct cap_hdr *h;
7781 for (h = s->fe->rsp_cap; h; h = h->next)
7782 pool_free2(h->pool, txn->rsp.cap[h->index]);
7783 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7784 }
7785
Willy Tarreau0937bc42009-12-22 15:03:09 +01007786}
7787
7788/* to be used at the end of a transaction to prepare a new one */
7789void http_reset_txn(struct session *s)
7790{
7791 http_end_txn(s);
7792 http_init_txn(s);
7793
7794 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007795 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007796 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007797 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007798 /* re-init store persistence */
7799 s->store_count = 0;
7800
Willy Tarreau0937bc42009-12-22 15:03:09 +01007801 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007802
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007803 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007804
Willy Tarreau739cfba2010-01-25 23:11:14 +01007805 /* We must trim any excess data from the response buffer, because we
7806 * may have blocked an invalid response from a server that we don't
7807 * want to accidentely forward once we disable the analysers, nor do
7808 * we want those data to come along with next response. A typical
7809 * example of such data would be from a buggy server responding to
7810 * a HEAD with some data, or sending more than the advertised
7811 * content-length.
7812 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007813 if (unlikely(s->rep->buf->i))
7814 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007815
Willy Tarreau0937bc42009-12-22 15:03:09 +01007816 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007817 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007818
Willy Tarreaud04e8582010-05-31 12:31:35 +02007819 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007820 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007821
7822 s->req->rex = TICK_ETERNITY;
7823 s->req->wex = TICK_ETERNITY;
7824 s->req->analyse_exp = TICK_ETERNITY;
7825 s->rep->rex = TICK_ETERNITY;
7826 s->rep->wex = TICK_ETERNITY;
7827 s->rep->analyse_exp = TICK_ETERNITY;
7828}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007829
Willy Tarreauff011f22011-01-06 17:51:27 +01007830void free_http_req_rules(struct list *r) {
7831 struct http_req_rule *tr, *pr;
7832
7833 list_for_each_entry_safe(pr, tr, r, list) {
7834 LIST_DEL(&pr->list);
7835 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7836 free(pr->http_auth.realm);
7837
7838 free(pr);
7839 }
7840}
7841
7842struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7843{
7844 struct http_req_rule *rule;
7845 int cur_arg;
7846
7847 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7848 if (!rule) {
7849 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7850 return NULL;
7851 }
7852
7853 if (!*args[0]) {
7854 goto req_error_parsing;
7855 } else if (!strcmp(args[0], "allow")) {
7856 rule->action = HTTP_REQ_ACT_ALLOW;
7857 cur_arg = 1;
7858 } else if (!strcmp(args[0], "deny")) {
7859 rule->action = HTTP_REQ_ACT_DENY;
7860 cur_arg = 1;
7861 } else if (!strcmp(args[0], "auth")) {
7862 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7863 cur_arg = 1;
7864
7865 while(*args[cur_arg]) {
7866 if (!strcmp(args[cur_arg], "realm")) {
7867 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7868 cur_arg+=2;
7869 continue;
7870 } else
7871 break;
7872 }
7873 } else {
7874req_error_parsing:
7875 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7876 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7877 return NULL;
7878 }
7879
7880 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7881 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007882 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007883
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007884 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7885 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7886 file, linenum, args[0], errmsg);
7887 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007888 return NULL;
7889 }
7890 rule->cond = cond;
7891 }
7892 else if (*args[cur_arg]) {
7893 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7894 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7895 file, linenum, args[0], args[cur_arg]);
7896 return NULL;
7897 }
7898
7899 return rule;
7900}
7901
Willy Tarreau8797c062007-05-07 00:55:35 +02007902/************************************************************************/
7903/* The code below is dedicated to ACL parsing and matching */
7904/************************************************************************/
7905
7906
Willy Tarreau14174bc2012-04-16 14:34:04 +02007907/* This function ensures that the prerequisites for an L7 fetch are ready,
7908 * which means that a request or response is ready. If some data is missing,
7909 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007910 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7911 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007912 *
7913 * The function returns :
7914 * 0 if some data is missing or if the requested data cannot be fetched
7915 * -1 if it is certain that we'll never have any HTTP message there
7916 * 1 if an HTTP message is ready
7917 */
7918static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007919acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007920 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007921{
7922 struct http_txn *txn = l7;
7923 struct http_msg *msg = &txn->req;
7924
7925 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7926 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7927 */
7928
7929 if (unlikely(!s || !txn))
7930 return 0;
7931
7932 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007933 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007934
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007935 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007936 if (unlikely(!s->req))
7937 return 0;
7938
7939 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007940 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007941 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007942 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007943 return -1;
7944 }
7945
7946 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007947 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02007948 http_msg_analyzer(msg, &txn->hdr_idx);
7949
7950 /* Still no valid request ? */
7951 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007952 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007953 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007954 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007955 return -1;
7956 }
7957 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007958 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007959 return 0;
7960 }
7961
7962 /* OK we just got a valid HTTP request. We have some minor
7963 * preparation to perform so that further checks can rely
7964 * on HTTP tests.
7965 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007966 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007967 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7968 s->flags |= SN_REDIRECTABLE;
7969
7970 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007971 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007972 return -1;
7973 }
7974 }
7975
Willy Tarreau24e32d82012-04-23 23:55:44 +02007976 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007977 return 0; /* data might have moved and indexes changed */
7978
7979 /* otherwise everything's ready for the request */
7980 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007981 else {
7982 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007983 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7984 return 0;
7985 }
7986
7987 /* everything's OK */
7988 return 1;
7989}
Willy Tarreau8797c062007-05-07 00:55:35 +02007990
Willy Tarreauc0239e02012-04-16 14:42:55 +02007991#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007992 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 1); if (r <= 0) return r; } while (0)
Willy Tarreauc0239e02012-04-16 14:42:55 +02007993
Willy Tarreau24e32d82012-04-23 23:55:44 +02007994#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007995 do { int r = acl_prefetch_http(px, l4, l7, opt, args, smp, 0); if (r <= 0) return r; } while (0)
Willy Tarreau24e32d82012-04-23 23:55:44 +02007996
Willy Tarreau8797c062007-05-07 00:55:35 +02007997
7998/* 1. Check on METHOD
7999 * We use the pre-parsed method if it is known, and store its number as an
8000 * integer. If it is unknown, we use the pointer and the length.
8001 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008002static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008003{
8004 int len, meth;
8005
Willy Tarreauae8b7962007-06-09 23:10:04 +02008006 len = strlen(*text);
8007 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008008
8009 pattern->val.i = meth;
8010 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008011 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008012 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008013 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008014 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008015 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008016 pattern->len = len;
8017 }
8018 return 1;
8019}
8020
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008021/* This function fetches the method of current HTTP request and stores
8022 * it in the global pattern struct as a chunk. There are two possibilities :
8023 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8024 * in <len> and <ptr> is NULL ;
8025 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8026 * <len> to its length.
8027 * This is intended to be used with acl_match_meth() only.
8028 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008029static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008030acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008031 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008032{
8033 int meth;
8034 struct http_txn *txn = l7;
8035
Willy Tarreau24e32d82012-04-23 23:55:44 +02008036 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008037
Willy Tarreau8797c062007-05-07 00:55:35 +02008038 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008039 smp->type = SMP_T_UINT;
8040 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008041 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008042 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8043 /* ensure the indexes are not affected */
8044 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008045 smp->type = SMP_T_CSTR;
8046 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008047 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008048 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008049 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008050 return 1;
8051}
8052
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008053/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008054static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008055{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008056 int icase;
8057
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008058
Willy Tarreauf853c462012-04-23 18:53:56 +02008059 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008060 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008061 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008062 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008063 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008064 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008065
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008066 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8067 if (pattern->val.i != HTTP_METH_OTHER)
8068 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008069
8070 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008071 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008072 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008073
8074 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008075 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8076 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008077 return ACL_PAT_FAIL;
8078 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008079}
8080
8081/* 2. Check on Request/Status Version
8082 * We simply compare strings here.
8083 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008084static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008085{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008086 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008087 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008088 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008089 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008090 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008091 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008092 return 1;
8093}
8094
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008095static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008096acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008097 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008098{
8099 struct http_txn *txn = l7;
8100 char *ptr;
8101 int len;
8102
Willy Tarreauc0239e02012-04-16 14:42:55 +02008103 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008104
Willy Tarreau8797c062007-05-07 00:55:35 +02008105 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008106 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008107
8108 while ((len-- > 0) && (*ptr++ != '/'));
8109 if (len <= 0)
8110 return 0;
8111
Willy Tarreauf853c462012-04-23 18:53:56 +02008112 smp->type = SMP_T_CSTR;
8113 smp->data.str.str = ptr;
8114 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008115
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008116 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008117 return 1;
8118}
8119
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008120static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008121acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008122 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008123{
8124 struct http_txn *txn = l7;
8125 char *ptr;
8126 int len;
8127
Willy Tarreauc0239e02012-04-16 14:42:55 +02008128 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008129
Willy Tarreau8797c062007-05-07 00:55:35 +02008130 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008131 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008132
8133 while ((len-- > 0) && (*ptr++ != '/'));
8134 if (len <= 0)
8135 return 0;
8136
Willy Tarreauf853c462012-04-23 18:53:56 +02008137 smp->type = SMP_T_CSTR;
8138 smp->data.str.str = ptr;
8139 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008140
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008141 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008142 return 1;
8143}
8144
8145/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008146static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008147acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008148 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008149{
8150 struct http_txn *txn = l7;
8151 char *ptr;
8152 int len;
8153
Willy Tarreauc0239e02012-04-16 14:42:55 +02008154 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008155
Willy Tarreau8797c062007-05-07 00:55:35 +02008156 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008157 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008158
Willy Tarreauf853c462012-04-23 18:53:56 +02008159 smp->type = SMP_T_UINT;
8160 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008161 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008162 return 1;
8163}
8164
8165/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008166static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008167smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008168 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008169{
8170 struct http_txn *txn = l7;
8171
Willy Tarreauc0239e02012-04-16 14:42:55 +02008172 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008173
Willy Tarreauf853c462012-04-23 18:53:56 +02008174 smp->type = SMP_T_CSTR;
8175 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008176 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008177 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008178 return 1;
8179}
8180
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008181static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008182smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008183 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008184{
8185 struct http_txn *txn = l7;
8186
Willy Tarreauc0239e02012-04-16 14:42:55 +02008187 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008188
8189 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008190 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8191 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008192 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008193 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008194 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008195
8196 /*
8197 * If we are parsing url in frontend space, we prepare backend stage
8198 * to not parse again the same url ! optimization lazyness...
8199 */
8200 if (px->options & PR_O_HTTP_PROXY)
8201 l4->flags |= SN_ADDR_SET;
8202
Willy Tarreau37406352012-04-23 16:16:37 +02008203 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008204 return 1;
8205}
8206
8207static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008208smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008209 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008210{
8211 struct http_txn *txn = l7;
8212
Willy Tarreauc0239e02012-04-16 14:42:55 +02008213 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008214
8215 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008216 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
Willy Tarreauf853c462012-04-23 18:53:56 +02008217 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008218 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008219
8220 if (px->options & PR_O_HTTP_PROXY)
8221 l4->flags |= SN_ADDR_SET;
8222
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008223 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008224 return 1;
8225}
8226
Willy Tarreau185b5c42012-04-26 15:11:51 +02008227/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8228 * Accepts an optional argument of type string containing the header field name,
8229 * and an optional argument of type signed or unsigned integer to request an
8230 * explicit occurrence of the header. Note that in the event of a missing name,
8231 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008232 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008233static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008234smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008235 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008236{
8237 struct http_txn *txn = l7;
8238 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008239 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008240 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008241 int occ = 0;
8242 const char *name_str = NULL;
8243 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008244
Willy Tarreau185b5c42012-04-26 15:11:51 +02008245 if (args) {
8246 if (args[0].type != ARGT_STR)
8247 return 0;
8248 name_str = args[0].data.str.str;
8249 name_len = args[0].data.str.len;
8250
8251 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8252 occ = args[1].data.uint;
8253 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008254
Willy Tarreaue333ec92012-04-16 16:26:40 +02008255 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008256
Willy Tarreau185b5c42012-04-26 15:11:51 +02008257 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008258 /* search for header from the beginning */
8259 ctx->idx = 0;
8260
Willy Tarreau185b5c42012-04-26 15:11:51 +02008261 if (!occ && !(opt & SMP_OPT_ITERATE))
8262 /* no explicit occurrence and single fetch => last header by default */
8263 occ = -1;
8264
8265 if (!occ)
8266 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008267 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008268
Willy Tarreau185b5c42012-04-26 15:11:51 +02008269 smp->type = SMP_T_CSTR;
8270 smp->flags |= SMP_F_VOL_HDR;
8271 if (http_get_hdr(msg, name_str, name_len, idx, occ, ctx, &smp->data.str.str, &smp->data.str.len))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008272 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008273
Willy Tarreau37406352012-04-23 16:16:37 +02008274 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008275 return 0;
8276}
8277
Willy Tarreauc11416f2007-06-17 16:58:38 +02008278/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008279 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008280 */
8281static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008282smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008283 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008284{
8285 struct http_txn *txn = l7;
8286 struct hdr_idx *idx = &txn->hdr_idx;
8287 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008288 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008289 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008290
Willy Tarreau24e32d82012-04-23 23:55:44 +02008291 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008292 return 0;
8293
Willy Tarreaue333ec92012-04-16 16:26:40 +02008294 CHECK_HTTP_MESSAGE_FIRST();
8295
Willy Tarreau33a7e692007-06-10 19:45:56 +02008296 ctx.idx = 0;
8297 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008298 while (http_find_header2(args->data.str.str, args->data.str.len, msg->chn->buf->p, idx, &ctx))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008299 cnt++;
8300
Willy Tarreauf853c462012-04-23 18:53:56 +02008301 smp->type = SMP_T_UINT;
8302 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008303 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008304 return 1;
8305}
8306
Willy Tarreau185b5c42012-04-26 15:11:51 +02008307/* Fetch an HTTP header's integer value. The integer value is returned. It
8308 * takes a mandatory argument of type string and an optional one of type int
8309 * to designate a specific occurrence. It returns an unsigned integer, which
8310 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008311 */
8312static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008313smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008314 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008315{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008316 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008317
Willy Tarreauf853c462012-04-23 18:53:56 +02008318 if (ret > 0) {
8319 smp->type = SMP_T_UINT;
8320 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8321 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008322
Willy Tarreaud53e2422012-04-16 17:21:11 +02008323 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008324}
8325
Cyril Bonté69fa9922012-10-25 00:01:06 +02008326/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8327 * and an optional one of type int to designate a specific occurrence.
8328 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008329 */
8330static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008331smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008332 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008333{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008334 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008335
Willy Tarreau185b5c42012-04-26 15:11:51 +02008336 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008337 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8338 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008339 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008340 } else {
8341 struct chunk *temp = sample_get_trash_chunk();
8342 if (smp->data.str.len < temp->size - 1) {
8343 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8344 temp->str[smp->data.str.len] = '\0';
8345 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8346 smp->type = SMP_T_IPV6;
8347 break;
8348 }
8349 }
8350 }
8351
Willy Tarreaud53e2422012-04-16 17:21:11 +02008352 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008353 if (!(smp->flags & SMP_F_NOT_LAST))
8354 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008355 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008356 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008357}
8358
Willy Tarreau737b0c12007-06-10 21:28:46 +02008359/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8360 * the first '/' after the possible hostname, and ends before the possible '?'.
8361 */
8362static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008363smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008364 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008365{
8366 struct http_txn *txn = l7;
8367 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008368
Willy Tarreauc0239e02012-04-16 14:42:55 +02008369 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008370
Willy Tarreau9b28e032012-10-12 23:49:43 +02008371 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008372 ptr = http_get_path(txn);
8373 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008374 return 0;
8375
8376 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008377 smp->type = SMP_T_CSTR;
8378 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008379
8380 while (ptr < end && *ptr != '?')
8381 ptr++;
8382
Willy Tarreauf853c462012-04-23 18:53:56 +02008383 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008384 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008385 return 1;
8386}
8387
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008388/* This produces a concatenation of the first occurrence of the Host header
8389 * followed by the path component if it begins with a slash ('/'). This means
8390 * that '*' will not be added, resulting in exactly the first Host entry.
8391 * If no Host header is found, then the path is returned as-is. The returned
8392 * value is stored in the trash so it does not need to be marked constant.
8393 */
8394static int
8395smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8396 const struct arg *args, struct sample *smp)
8397{
8398 struct http_txn *txn = l7;
8399 char *ptr, *end, *beg;
8400 struct hdr_ctx ctx;
8401
8402 CHECK_HTTP_MESSAGE_FIRST();
8403
8404 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008405 if (!http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx) ||
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008406 !ctx.vlen)
8407 return smp_fetch_path(px, l4, l7, opt, args, smp);
8408
8409 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008410 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008411 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008412 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008413 smp->data.str.len = ctx.vlen;
8414
8415 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008416 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008417 beg = http_get_path(txn);
8418 if (!beg)
8419 beg = end;
8420
8421 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8422
8423 if (beg < ptr && *beg == '/') {
8424 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8425 smp->data.str.len += ptr - beg;
8426 }
8427
8428 smp->flags = SMP_F_VOL_1ST;
8429 return 1;
8430}
8431
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008432static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008433acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008434 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008435{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008436 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8437 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8438 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008439
Willy Tarreau24e32d82012-04-23 23:55:44 +02008440 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008441
Willy Tarreauf853c462012-04-23 18:53:56 +02008442 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008443 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008444 return 1;
8445}
8446
Willy Tarreau7f18e522010-10-22 20:04:13 +02008447/* return a valid test if the current request is the first one on the connection */
8448static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008449acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008450 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008451{
8452 if (!s)
8453 return 0;
8454
Willy Tarreauf853c462012-04-23 18:53:56 +02008455 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008456 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008457 return 1;
8458}
8459
Willy Tarreau34db1082012-04-19 17:16:54 +02008460/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008461static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008462acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008463 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008464{
8465
Willy Tarreau24e32d82012-04-23 23:55:44 +02008466 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008467 return 0;
8468
Willy Tarreauc0239e02012-04-16 14:42:55 +02008469 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008470
Willy Tarreauc0239e02012-04-16 14:42:55 +02008471 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008472 return 0;
8473
Willy Tarreauf853c462012-04-23 18:53:56 +02008474 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008475 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008476 return 1;
8477}
Willy Tarreau8797c062007-05-07 00:55:35 +02008478
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008479/* Accepts exactly 1 argument of type userlist */
8480static int
8481acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8482 const struct arg *args, struct sample *smp)
8483{
8484
8485 if (!args || args->type != ARGT_USR)
8486 return 0;
8487
8488 CHECK_HTTP_MESSAGE_FIRST();
8489
8490 if (!get_http_auth(l4))
8491 return 0;
8492
8493 /* acl_match_auth() will need several information at once */
8494 smp->ctx.a[0] = args->data.usr; /* user list */
8495 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8496 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8497
8498 /* if the user does not belong to the userlist or has a wrong password,
8499 * report that it unconditionally does not match. Otherwise we return
8500 * a non-zero integer which will be ignored anyway since all the params
8501 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8502 */
8503 smp->type = SMP_T_BOOL;
8504 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8505 if (smp->data.uint)
8506 smp->type = SMP_T_UINT;
8507
8508 return 1;
8509}
8510
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008511/* Try to find the next occurrence of a cookie name in a cookie header value.
8512 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8513 * the cookie value is returned into *value and *value_l, and the function
8514 * returns a pointer to the next pointer to search from if the value was found.
8515 * Otherwise if the cookie was not found, NULL is returned and neither value
8516 * nor value_l are touched. The input <hdr> string should first point to the
8517 * header's value, and the <hdr_end> pointer must point to the first character
8518 * not part of the value. <list> must be non-zero if value may represent a list
8519 * of values (cookie headers). This makes it faster to abort parsing when no
8520 * list is expected.
8521 */
8522static char *
8523extract_cookie_value(char *hdr, const char *hdr_end,
8524 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008525 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008526{
8527 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8528 char *next;
8529
8530 /* we search at least a cookie name followed by an equal, and more
8531 * generally something like this :
8532 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8533 */
8534 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8535 /* Iterate through all cookies on this line */
8536
8537 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8538 att_beg++;
8539
8540 /* find att_end : this is the first character after the last non
8541 * space before the equal. It may be equal to hdr_end.
8542 */
8543 equal = att_end = att_beg;
8544
8545 while (equal < hdr_end) {
8546 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8547 break;
8548 if (http_is_spht[(unsigned char)*equal++])
8549 continue;
8550 att_end = equal;
8551 }
8552
8553 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8554 * is between <att_beg> and <equal>, both may be identical.
8555 */
8556
8557 /* look for end of cookie if there is an equal sign */
8558 if (equal < hdr_end && *equal == '=') {
8559 /* look for the beginning of the value */
8560 val_beg = equal + 1;
8561 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8562 val_beg++;
8563
8564 /* find the end of the value, respecting quotes */
8565 next = find_cookie_value_end(val_beg, hdr_end);
8566
8567 /* make val_end point to the first white space or delimitor after the value */
8568 val_end = next;
8569 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8570 val_end--;
8571 } else {
8572 val_beg = val_end = next = equal;
8573 }
8574
8575 /* We have nothing to do with attributes beginning with '$'. However,
8576 * they will automatically be removed if a header before them is removed,
8577 * since they're supposed to be linked together.
8578 */
8579 if (*att_beg == '$')
8580 continue;
8581
8582 /* Ignore cookies with no equal sign */
8583 if (equal == next)
8584 continue;
8585
8586 /* Now we have the cookie name between att_beg and att_end, and
8587 * its value between val_beg and val_end.
8588 */
8589
8590 if (att_end - att_beg == cookie_name_l &&
8591 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8592 /* let's return this value and indicate where to go on from */
8593 *value = val_beg;
8594 *value_l = val_end - val_beg;
8595 return next + 1;
8596 }
8597
8598 /* Set-Cookie headers only have the name in the first attr=value part */
8599 if (!list)
8600 break;
8601 }
8602
8603 return NULL;
8604}
8605
Willy Tarreaue333ec92012-04-16 16:26:40 +02008606/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008607 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8608 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008609 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008610 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008611 * Accepts exactly 1 argument of type string. If the input options indicate
8612 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008613 */
8614static int
Willy Tarreau51539362012-05-08 12:46:28 +02008615smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8616 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008617{
8618 struct http_txn *txn = l7;
8619 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008620 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008621 const struct http_msg *msg;
8622 const char *hdr_name;
8623 int hdr_name_len;
8624 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008625 int occ = 0;
8626 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008627
Willy Tarreau24e32d82012-04-23 23:55:44 +02008628 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008629 return 0;
8630
Willy Tarreaue333ec92012-04-16 16:26:40 +02008631 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008632
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008633 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008634 msg = &txn->req;
8635 hdr_name = "Cookie";
8636 hdr_name_len = 6;
8637 } else {
8638 msg = &txn->rsp;
8639 hdr_name = "Set-Cookie";
8640 hdr_name_len = 10;
8641 }
8642
Willy Tarreau28376d62012-04-26 21:26:10 +02008643 if (!occ && !(opt & SMP_OPT_ITERATE))
8644 /* no explicit occurrence and single fetch => last cookie by default */
8645 occ = -1;
8646
8647 /* OK so basically here, either we want only one value and it's the
8648 * last one, or we want to iterate over all of them and we fetch the
8649 * next one.
8650 */
8651
Willy Tarreau9b28e032012-10-12 23:49:43 +02008652 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008653 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008654 /* search for the header from the beginning, we must first initialize
8655 * the search parameters.
8656 */
Willy Tarreau37406352012-04-23 16:16:37 +02008657 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008658 ctx->idx = 0;
8659 }
8660
Willy Tarreau28376d62012-04-26 21:26:10 +02008661 smp->flags |= SMP_F_VOL_HDR;
8662
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008663 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008664 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8665 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008666 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8667 goto out;
8668
Willy Tarreau24e32d82012-04-23 23:55:44 +02008669 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008670 continue;
8671
Willy Tarreau37406352012-04-23 16:16:37 +02008672 smp->ctx.a[0] = ctx->line + ctx->val;
8673 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008674 }
8675
Willy Tarreauf853c462012-04-23 18:53:56 +02008676 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008677 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008678 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008679 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008680 &smp->data.str.str,
8681 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008682 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008683 found = 1;
8684 if (occ >= 0) {
8685 /* one value was returned into smp->data.str.{str,len} */
8686 smp->flags |= SMP_F_NOT_LAST;
8687 return 1;
8688 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008689 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008690 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008691 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008692 /* all cookie headers and values were scanned. If we're looking for the
8693 * last occurrence, we may return it now.
8694 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008695 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008696 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008697 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008698}
8699
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008700/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008701 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008702 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008703 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008704 */
8705static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008706acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008707 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008708{
8709 struct http_txn *txn = l7;
8710 struct hdr_idx *idx = &txn->hdr_idx;
8711 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008712 const struct http_msg *msg;
8713 const char *hdr_name;
8714 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008715 int cnt;
8716 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008717 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008718
Willy Tarreau24e32d82012-04-23 23:55:44 +02008719 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008720 return 0;
8721
Willy Tarreaue333ec92012-04-16 16:26:40 +02008722 CHECK_HTTP_MESSAGE_FIRST();
8723
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008724 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008725 msg = &txn->req;
8726 hdr_name = "Cookie";
8727 hdr_name_len = 6;
8728 } else {
8729 msg = &txn->rsp;
8730 hdr_name = "Set-Cookie";
8731 hdr_name_len = 10;
8732 }
8733
Willy Tarreau9b28e032012-10-12 23:49:43 +02008734 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008735 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008736 ctx.idx = 0;
8737 cnt = 0;
8738
8739 while (1) {
8740 /* Note: val_beg == NULL every time we need to fetch a new header */
8741 if (!val_beg) {
8742 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8743 break;
8744
Willy Tarreau24e32d82012-04-23 23:55:44 +02008745 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008746 continue;
8747
8748 val_beg = ctx.line + ctx.val;
8749 val_end = val_beg + ctx.vlen;
8750 }
8751
Willy Tarreauf853c462012-04-23 18:53:56 +02008752 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008753 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008754 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008755 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008756 &smp->data.str.str,
8757 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008758 cnt++;
8759 }
8760 }
8761
Willy Tarreauf853c462012-04-23 18:53:56 +02008762 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008763 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008764 return 1;
8765}
8766
Willy Tarreau51539362012-05-08 12:46:28 +02008767/* Fetch an cookie's integer value. The integer value is returned. It
8768 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8769 */
8770static int
8771smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8772 const struct arg *args, struct sample *smp)
8773{
8774 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8775
8776 if (ret > 0) {
8777 smp->type = SMP_T_UINT;
8778 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8779 }
8780
8781 return ret;
8782}
8783
Willy Tarreau8797c062007-05-07 00:55:35 +02008784/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008785/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008786/************************************************************************/
8787
David Cournapeau16023ee2010-12-23 20:55:41 +09008788/*
8789 * Given a path string and its length, find the position of beginning of the
8790 * query string. Returns NULL if no query string is found in the path.
8791 *
8792 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8793 *
8794 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8795 */
bedis4c75cca2012-10-05 08:38:24 +02008796static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008797{
8798 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008799
bedis4c75cca2012-10-05 08:38:24 +02008800 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09008801 return p ? p + 1 : NULL;
8802}
8803
bedis4c75cca2012-10-05 08:38:24 +02008804static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008805{
bedis4c75cca2012-10-05 08:38:24 +02008806 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09008807}
8808
8809/*
8810 * Given a url parameter, find the starting position of the first occurence,
8811 * or NULL if the parameter is not found.
8812 *
8813 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8814 * the function will return query_string+8.
8815 */
8816static char*
8817find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02008818 char* url_param_name, size_t url_param_name_l,
8819 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008820{
8821 char *pos, *last;
8822
8823 pos = query_string;
8824 last = query_string + query_string_l - url_param_name_l - 1;
8825
8826 while (pos <= last) {
8827 if (pos[url_param_name_l] == '=') {
8828 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8829 return pos;
8830 pos += url_param_name_l + 1;
8831 }
bedis4c75cca2012-10-05 08:38:24 +02008832 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008833 pos++;
8834 pos++;
8835 }
8836 return NULL;
8837}
8838
8839/*
8840 * Given a url parameter name, returns its value and size into *value and
8841 * *value_l respectively, and returns non-zero. If the parameter is not found,
8842 * zero is returned and value/value_l are not touched.
8843 */
8844static int
8845find_url_param_value(char* path, size_t path_l,
8846 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02008847 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008848{
8849 char *query_string, *qs_end;
8850 char *arg_start;
8851 char *value_start, *value_end;
8852
bedis4c75cca2012-10-05 08:38:24 +02008853 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008854 if (!query_string)
8855 return 0;
8856
8857 qs_end = path + path_l;
8858 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02008859 url_param_name, url_param_name_l,
8860 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008861 if (!arg_start)
8862 return 0;
8863
8864 value_start = arg_start + url_param_name_l + 1;
8865 value_end = value_start;
8866
bedis4c75cca2012-10-05 08:38:24 +02008867 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008868 value_end++;
8869
8870 *value = value_start;
8871 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008872 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008873}
8874
8875static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008876smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8877 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008878{
bedis4c75cca2012-10-05 08:38:24 +02008879 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09008880 struct http_txn *txn = l7;
8881 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008882
bedis4c75cca2012-10-05 08:38:24 +02008883 if (!args || args[0].type != ARGT_STR ||
8884 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008885 return 0;
8886
8887 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008888
bedis4c75cca2012-10-05 08:38:24 +02008889 if (args[1].type)
8890 delim = *args[1].data.str.str;
8891
Willy Tarreau9b28e032012-10-12 23:49:43 +02008892 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02008893 args->data.str.str, args->data.str.len,
8894 &smp->data.str.str, &smp->data.str.len,
8895 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008896 return 0;
8897
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008898 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008899 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008900 return 1;
8901}
8902
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008903/* Return the signed integer value for the specified url parameter (see url_param
8904 * above).
8905 */
8906static int
8907smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8908 const struct arg *args, struct sample *smp)
8909{
8910 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
8911
8912 if (ret > 0) {
8913 smp->type = SMP_T_UINT;
8914 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8915 }
8916
8917 return ret;
8918}
8919
Willy Tarreau185b5c42012-04-26 15:11:51 +02008920/* This function is used to validate the arguments passed to any "hdr" fetch
8921 * keyword. These keywords support an optional positive or negative occurrence
8922 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8923 * is assumed that the types are already the correct ones. Returns 0 on error,
8924 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8925 * error message in case of error, that the caller is responsible for freeing.
8926 * The initial location must either be freeable or NULL.
8927 */
8928static int val_hdr(struct arg *arg, char **err_msg)
8929{
8930 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008931 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02008932 return 0;
8933 }
8934 return 1;
8935}
8936
Willy Tarreau4a568972010-05-12 08:08:50 +02008937/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008938/* All supported ACL keywords must be declared here. */
8939/************************************************************************/
8940
8941/* Note: must not be declared <const> as its list will be overwritten.
8942 * Please take care of keeping this list alphabetically sorted.
8943 */
8944static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008945 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8946 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8947 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8948 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8949 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8950 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8951 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8952 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8953
Willy Tarreau51539362012-05-08 12:46:28 +02008954 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8955 { "cook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008956 { "cook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02008957 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8958 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8959 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8960 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8961 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8962 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8963 { "cook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008964
Willy Tarreau185b5c42012-04-26 15:11:51 +02008965 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8966 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8967 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8968 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8969 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8970 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8971 { "hdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8972 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8973 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8974 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8975 { "hdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008976
8977 { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_nothing, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008978 { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth_grp, acl_match_auth, ACL_USE_L7REQ_VOLATILE, ARG1(0,USR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008979 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8980
8981 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8982
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008983 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8984 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8985 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8986 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8987 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8988 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8989 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8990 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008991
8992 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8993 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8994 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8995
Willy Tarreau51539362012-05-08 12:46:28 +02008996 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8997 { "scook_beg", acl_parse_str, smp_fetch_cookie, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008998 { "scook_cnt", acl_parse_int, acl_fetch_cookie_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau51539362012-05-08 12:46:28 +02008999 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9000 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9001 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9002 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9003 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9004 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9005 { "scook_val", acl_parse_int, smp_fetch_cookie_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009006
Willy Tarreau185b5c42012-04-26 15:11:51 +02009007 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9008 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9009 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9010 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9011 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9012 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9013 { "shdr_ip", acl_parse_ip, smp_fetch_hdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9014 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9015 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9016 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9017 { "shdr_val", acl_parse_int, smp_fetch_hdr_val, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009018
9019 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
9020
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009021 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9022 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9023 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9024 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9025 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9026 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9027 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9028 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
9029 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9030 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009031
9032 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9033 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9034 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9035 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9036 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9037 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9038 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9039 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9040 { "urlp_sub", acl_parse_str, smp_fetch_url_param, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009041 { "urlp_val", acl_parse_int, smp_fetch_url_param_val, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009042
9043 { NULL, NULL, NULL, NULL },
9044}};
9045
9046/************************************************************************/
9047/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009048/************************************************************************/
9049/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009050static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009051 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9052 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9053 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9054 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9055 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
9056 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
9057 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9058 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
9059 { "set-cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_RES }, /* deprecated */
Willy Tarreau9fcb9842012-04-20 14:45:49 +02009060 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009061}};
9062
Willy Tarreau8797c062007-05-07 00:55:35 +02009063
9064__attribute__((constructor))
9065static void __http_protocol_init(void)
9066{
9067 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009068 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009069}
9070
9071
Willy Tarreau58f10d72006-12-04 02:26:12 +01009072/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009073 * Local variables:
9074 * c-indent-level: 8
9075 * c-basic-offset: 8
9076 * End:
9077 */