blob: 9b094d732cd7e32cefadd32ae7476c0ff6375364 [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{
393 int size = 0;
David du Colombier7af46052012-05-16 14:16:48 +0200394 size += snprintf(trash + size, trashlen - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100395 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100396 line,
397 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200398 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);
Willy Tarreaue988a792010-01-04 21:13:14 +0100399 write(-1, trash, size);
400 size = 0;
David du Colombier7af46052012-05-16 14:16:48 +0200401 size += snprintf(trash + size, trashlen - size,
Willy Tarreaua458b672012-03-05 11:17:50 +0100402 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
Willy Tarreaue988a792010-01-04 21:13:14 +0100403 line,
404 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200405 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);
Willy Tarreaue988a792010-01-04 21:13:14 +0100406
407 write(-1, trash, size);
408}
409#else
410#define http_silent_debug(l,s) do { } while (0)
411#endif
412
413/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100414 * Adds a header and its CRLF at the tail of the message's buffer, just before
415 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100416 * The header is also automatically added to the index <hdr_idx>, and the end
417 * of headers is automatically adjusted. The number of bytes added is returned
418 * on success, otherwise <0 is returned indicating an error.
419 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100420int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100421{
422 int bytes, len;
423
424 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200425 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 if (!bytes)
427 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100428 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100429 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
430}
431
432/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100433 * Adds a header and its CRLF at the tail of the message's buffer, just before
434 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100435 * the buffer is only opened and the space reserved, but nothing is copied.
436 * The header is also automatically added to the index <hdr_idx>, and the end
437 * of headers is automatically adjusted. The number of bytes added is returned
438 * on success, otherwise <0 is returned indicating an error.
439 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100440int http_header_add_tail2(struct http_msg *msg,
441 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100442{
443 int bytes;
444
Willy Tarreau9b28e032012-10-12 23:49:43 +0200445 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100446 if (!bytes)
447 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100448 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100449 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
450}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451
452/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100453 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
454 * If so, returns the position of the first non-space character relative to
455 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
456 * to return a pointer to the place after the first space. Returns 0 if the
457 * header name does not match. Checks are case-insensitive.
458 */
459int http_header_match2(const char *hdr, const char *end,
460 const char *name, int len)
461{
462 const char *val;
463
464 if (hdr + len >= end)
465 return 0;
466 if (hdr[len] != ':')
467 return 0;
468 if (strncasecmp(hdr, name, len) != 0)
469 return 0;
470 val = hdr + len + 1;
471 while (val < end && HTTP_IS_SPHT(*val))
472 val++;
473 if ((val >= end) && (len + 2 <= end - hdr))
474 return len + 2; /* we may replace starting from second space */
475 return val - hdr;
476}
477
Willy Tarreau68085d82010-01-18 14:54:04 +0100478/* Find the end of the header value contained between <s> and <e>. See RFC2616,
479 * par 2.2 for more information. Note that it requires a valid header to return
480 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200481 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100482char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200483{
484 int quoted, qdpair;
485
486 quoted = qdpair = 0;
487 for (; s < e; s++) {
488 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200489 else if (quoted) {
490 if (*s == '\\') qdpair = 1;
491 else if (*s == '"') quoted = 0;
492 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200493 else if (*s == '"') quoted = 1;
494 else if (*s == ',') return s;
495 }
496 return s;
497}
498
499/* Find the first or next occurrence of header <name> in message buffer <sol>
500 * using headers index <idx>, and return it in the <ctx> structure. This
501 * structure holds everything necessary to use the header and find next
502 * occurrence. If its <idx> member is 0, the header is searched from the
503 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100504 * 1 when it finds a value, and 0 when there is no more. It is designed to work
505 * with headers defined as comma-separated lists. As a special case, if ctx->val
506 * is NULL when searching for a new values of a header, the current header is
507 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 */
509int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100510 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200511 struct hdr_ctx *ctx)
512{
Willy Tarreau68085d82010-01-18 14:54:04 +0100513 char *eol, *sov;
514 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200515
Willy Tarreau68085d82010-01-18 14:54:04 +0100516 cur_idx = ctx->idx;
517 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200518 /* We have previously returned a value, let's search
519 * another one on the same line.
520 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200521 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200522 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100523 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200524 eol = sol + idx->v[cur_idx].len;
525
526 if (sov >= eol)
527 /* no more values in this header */
528 goto next_hdr;
529
Willy Tarreau68085d82010-01-18 14:54:04 +0100530 /* values remaining for this header, skip the comma but save it
531 * for later use (eg: for header deletion).
532 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200533 sov++;
534 while (sov < eol && http_is_lws[(unsigned char)*sov])
535 sov++;
536
537 goto return_hdr;
538 }
539
540 /* first request for this header */
541 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100542 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200543 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200544 while (cur_idx) {
545 eol = sol + idx->v[cur_idx].len;
546
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200547 if (len == 0) {
548 /* No argument was passed, we want any header.
549 * To achieve this, we simply build a fake request. */
550 while (sol + len < eol && sol[len] != ':')
551 len++;
552 name = sol;
553 }
554
Willy Tarreau33a7e692007-06-10 19:45:56 +0200555 if ((len < eol - sol) &&
556 (sol[len] == ':') &&
557 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100558 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200559 sov = sol + len + 1;
560 while (sov < eol && http_is_lws[(unsigned char)*sov])
561 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100562
Willy Tarreau33a7e692007-06-10 19:45:56 +0200563 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100564 ctx->prev = old_idx;
565 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200566 ctx->idx = cur_idx;
567 ctx->val = sov - sol;
568
569 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200570 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200571 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200572 eol--;
573 ctx->tws++;
574 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200575 ctx->vlen = eol - sov;
576 return 1;
577 }
578 next_hdr:
579 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100580 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200581 cur_idx = idx->v[cur_idx].next;
582 }
583 return 0;
584}
585
586int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100587 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200588 struct hdr_ctx *ctx)
589{
590 return http_find_header2(name, strlen(name), sol, idx, ctx);
591}
592
Willy Tarreau68085d82010-01-18 14:54:04 +0100593/* Remove one value of a header. This only works on a <ctx> returned by one of
594 * the http_find_header functions. The value is removed, as well as surrounding
595 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100596 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100597 * message <msg>. The new index is returned. If it is zero, it means there is
598 * no more header, so any processing may stop. The ctx is always left in a form
599 * that can be handled by http_find_header2() to find next occurrence.
600 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100601int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100602{
603 int cur_idx = ctx->idx;
604 char *sol = ctx->line;
605 struct hdr_idx_elem *hdr;
606 int delta, skip_comma;
607
608 if (!cur_idx)
609 return 0;
610
611 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200612 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200614 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100615 http_msg_move_end(msg, delta);
616 idx->used--;
617 hdr->len = 0; /* unused entry */
618 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100619 if (idx->tail == ctx->idx)
620 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100621 ctx->idx = ctx->prev; /* walk back to the end of previous header */
622 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
623 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200624 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100625 return ctx->idx;
626 }
627
628 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200629 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
630 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100631 */
632
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200634 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200635 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100636 NULL, 0);
637 hdr->len += delta;
638 http_msg_move_end(msg, delta);
639 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200640 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100641 return ctx->idx;
642}
643
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100644/* This function handles a server error at the stream interface level. The
645 * stream interface is assumed to be already in a closed state. An optional
646 * message is copied into the input buffer, and an HTTP status code stored.
647 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100648 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100650static void http_server_error(struct session *t, struct stream_interface *si,
651 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200652{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200653 channel_auto_read(si->ob);
654 channel_abort(si->ob);
655 channel_auto_close(si->ob);
656 channel_erase(si->ob);
657 channel_auto_close(si->ib);
658 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100659 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100660 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200661 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 }
663 if (!(t->flags & SN_ERR_MASK))
664 t->flags |= err;
665 if (!(t->flags & SN_FINST_MASK))
666 t->flags |= finst;
667}
668
Willy Tarreau80587432006-12-24 17:47:20 +0100669/* This function returns the appropriate error location for the given session
670 * and message.
671 */
672
Willy Tarreau783f2582012-09-04 12:19:04 +0200673struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100674{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200675 if (s->be->errmsg[msgnum].str)
676 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100677 else if (s->fe->errmsg[msgnum].str)
678 return &s->fe->errmsg[msgnum];
679 else
680 return &http_err_chunks[msgnum];
681}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200682
Willy Tarreau53b6c742006-12-17 13:37:46 +0100683/*
684 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
685 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
686 */
687static http_meth_t find_http_meth(const char *str, const int len)
688{
689 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100690 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100691
692 m = ((unsigned)*str - 'A');
693
694 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100695 for (h = http_methods[m]; h->len > 0; h++) {
696 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100698 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100699 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100700 };
701 return HTTP_METH_OTHER;
702 }
703 return HTTP_METH_NONE;
704
705}
706
Willy Tarreau21d2af32008-02-14 20:25:24 +0100707/* Parse the URI from the given transaction (which is assumed to be in request
708 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
709 * It is returned otherwise.
710 */
711static char *
712http_get_path(struct http_txn *txn)
713{
714 char *ptr, *end;
715
Willy Tarreau9b28e032012-10-12 23:49:43 +0200716 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100717 end = ptr + txn->req.sl.rq.u_l;
718
719 if (ptr >= end)
720 return NULL;
721
722 /* RFC2616, par. 5.1.2 :
723 * Request-URI = "*" | absuri | abspath | authority
724 */
725
726 if (*ptr == '*')
727 return NULL;
728
729 if (isalpha((unsigned char)*ptr)) {
730 /* this is a scheme as described by RFC3986, par. 3.1 */
731 ptr++;
732 while (ptr < end &&
733 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
734 ptr++;
735 /* skip '://' */
736 if (ptr == end || *ptr++ != ':')
737 return NULL;
738 if (ptr == end || *ptr++ != '/')
739 return NULL;
740 if (ptr == end || *ptr++ != '/')
741 return NULL;
742 }
743 /* skip [user[:passwd]@]host[:[port]] */
744
745 while (ptr < end && *ptr != '/')
746 ptr++;
747
748 if (ptr == end)
749 return NULL;
750
751 /* OK, we got the '/' ! */
752 return ptr;
753}
754
Willy Tarreauefb453c2008-10-26 20:49:47 +0100755/* Returns a 302 for a redirectable request. This may only be called just after
756 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
757 * left unchanged and will follow normal proxy processing.
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200758 * NOTE: this function is designed to support being called once data are scheduled
759 * for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100761void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762{
763 struct http_txn *txn;
764 struct chunk rdr;
Willy Tarreau827aee92011-03-10 16:55:02 +0100765 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100766 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200767 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100768
769 /* 1: create the response header */
770 rdr.len = strlen(HTTP_302);
771 rdr.str = trash;
David du Colombier7af46052012-05-16 14:16:48 +0200772 rdr.size = trashlen;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 memcpy(rdr.str, HTTP_302, rdr.len);
774
Willy Tarreau827aee92011-03-10 16:55:02 +0100775 srv = target_srv(&s->target);
776
Willy Tarreauefb453c2008-10-26 20:49:47 +0100777 /* 2: add the server's prefix */
Willy Tarreau827aee92011-03-10 16:55:02 +0100778 if (rdr.len + srv->rdr_len > rdr.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100779 return;
780
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100781 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100782 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
783 memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len);
784 rdr.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100785 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100786
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200787 /* 3: add the request URI. Since it was already forwarded, we need
788 * to temporarily rewind the buffer.
789 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100790 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200791 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200792
Willy Tarreauefb453c2008-10-26 20:49:47 +0100793 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200794 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 +0200795
Willy Tarreau9b28e032012-10-12 23:49:43 +0200796 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200797
Willy Tarreauefb453c2008-10-26 20:49:47 +0100798 if (!path)
799 return;
800
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +0200801 if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100802 return;
803
804 memcpy(rdr.str + rdr.len, path, len);
805 rdr.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100806
807 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
808 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
809 rdr.len += 29;
810 } else {
811 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
812 rdr.len += 23;
813 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100814
815 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200816 si_shutr(si);
817 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818 si->err_type = SI_ET_NONE;
819 si->err_loc = NULL;
820 si->state = SI_ST_CLO;
821
822 /* send the message */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100823 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100824
825 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100826 if (srv)
827 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100828}
829
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100830/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100831 * that the server side is closed. Note that err_type is actually a
832 * bitmask, where almost only aborts may be cumulated with other
833 * values. We consider that aborted operations are more important
834 * than timeouts or errors due to the fact that nobody else in the
835 * logs might explain incomplete retries. All others should avoid
836 * being cumulated. It should normally not be possible to have multiple
837 * aborts at once, but just in case, the first one in sequence is reported.
838 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100839void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100841 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100842
843 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100844 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200845 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100846 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100847 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200848 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100850 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200851 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100852 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200854 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100855 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100856 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200857 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100858 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100859 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200860 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100861 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100862 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200863 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100864}
865
Willy Tarreau42250582007-04-01 01:30:43 +0200866extern const char sess_term_cond[8];
867extern const char sess_fin_state[8];
868extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200869struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200870struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100871struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100872
Willy Tarreau117f59e2007-03-04 18:17:17 +0100873/*
874 * Capture headers from message starting at <som> according to header list
875 * <cap_hdr>, and fill the <idx> structure appropriately.
876 */
877void capture_headers(char *som, struct hdr_idx *idx,
878 char **cap, struct cap_hdr *cap_hdr)
879{
880 char *eol, *sol, *col, *sov;
881 int cur_idx;
882 struct cap_hdr *h;
883 int len;
884
885 sol = som + hdr_idx_first_pos(idx);
886 cur_idx = hdr_idx_first_idx(idx);
887
888 while (cur_idx) {
889 eol = sol + idx->v[cur_idx].len;
890
891 col = sol;
892 while (col < eol && *col != ':')
893 col++;
894
895 sov = col + 1;
896 while (sov < eol && http_is_lws[(unsigned char)*sov])
897 sov++;
898
899 for (h = cap_hdr; h; h = h->next) {
900 if ((h->namelen == col - sol) &&
901 (strncasecmp(sol, h->name, h->namelen) == 0)) {
902 if (cap[h->index] == NULL)
903 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200904 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100905
906 if (cap[h->index] == NULL) {
907 Alert("HTTP capture : out of memory.\n");
908 continue;
909 }
910
911 len = eol - sov;
912 if (len > h->len)
913 len = h->len;
914
915 memcpy(cap[h->index], sov, len);
916 cap[h->index][len]=0;
917 }
918 }
919 sol = eol + idx->v[cur_idx].cr + 1;
920 cur_idx = idx->v[cur_idx].next;
921 }
922}
923
924
Willy Tarreau42250582007-04-01 01:30:43 +0200925/* either we find an LF at <ptr> or we jump to <bad>.
926 */
927#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
928
929/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
930 * otherwise to <http_msg_ood> with <state> set to <st>.
931 */
932#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
933 ptr++; \
934 if (likely(ptr < end)) \
935 goto good; \
936 else { \
937 state = (st); \
938 goto http_msg_ood; \
939 } \
940 } while (0)
941
942
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100944 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100945 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
946 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
947 * will give undefined results.
948 * Note that it is upon the caller's responsibility to ensure that ptr < end,
949 * and that msg->sol points to the beginning of the response.
950 * If a complete line is found (which implies that at least one CR or LF is
951 * found before <end>, the updated <ptr> is returned, otherwise NULL is
952 * returned indicating an incomplete line (which does not mean that parts have
953 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
954 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
955 * upon next call.
956 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200957 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100958 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
959 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200960 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100961 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200962const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100963 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100964 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100965{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200966 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100967
Willy Tarreau8973c702007-01-21 23:58:29 +0100968 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100969 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200970 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100971 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100972 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
973
974 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100975 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100976 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
977 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100978 state = HTTP_MSG_ERROR;
979 break;
980
Willy Tarreau8973c702007-01-21 23:58:29 +0100981 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200982 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100983 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100984 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100985 goto http_msg_rpcode;
986 }
987 if (likely(HTTP_IS_SPHT(*ptr)))
988 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
989 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100990 state = HTTP_MSG_ERROR;
991 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100992
Willy Tarreau8973c702007-01-21 23:58:29 +0100993 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200994 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100995 if (likely(!HTTP_IS_LWS(*ptr)))
996 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
997
998 if (likely(HTTP_IS_SPHT(*ptr))) {
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 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1001 }
1002
1003 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001004 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001005 http_msg_rsp_reason:
1006 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001007 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001008 msg->sl.st.r_l = 0;
1009 goto http_msg_rpline_eol;
1010
Willy Tarreau8973c702007-01-21 23:58:29 +01001011 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001012 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001013 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001014 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001015 goto http_msg_rpreason;
1016 }
1017 if (likely(HTTP_IS_SPHT(*ptr)))
1018 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1019 /* so it's a CR/LF, so there is no reason phrase */
1020 goto http_msg_rsp_reason;
1021
Willy Tarreau8973c702007-01-21 23:58:29 +01001022 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001023 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 if (likely(!HTTP_IS_CRLF(*ptr)))
1025 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001026 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001027 http_msg_rpline_eol:
1028 /* We have seen the end of line. Note that we do not
1029 * necessarily have the \n yet, but at least we know that we
1030 * have EITHER \r OR \n, otherwise the response would not be
1031 * complete. We can then record the response length and return
1032 * to the caller which will be able to register it.
1033 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001034 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001035 return ptr;
1036
1037#ifdef DEBUG_FULL
1038 default:
1039 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1040 exit(1);
1041#endif
1042 }
1043
1044 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001045 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001046 if (ret_state)
1047 *ret_state = state;
1048 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001049 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001050 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001051}
1052
Willy Tarreau8973c702007-01-21 23:58:29 +01001053/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001054 * This function parses a request line between <ptr> and <end>, starting with
1055 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1056 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1057 * will give undefined results.
1058 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1059 * and that msg->sol points to the beginning of the request.
1060 * If a complete line is found (which implies that at least one CR or LF is
1061 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1062 * returned indicating an incomplete line (which does not mean that parts have
1063 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1064 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1065 * upon next call.
1066 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001067 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001068 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1069 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001070 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001071 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001072const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001073 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001074 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001075{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001076 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001080 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 if (likely(HTTP_IS_TOKEN(*ptr)))
1082 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1087 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001088
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001089 if (likely(HTTP_IS_CRLF(*ptr))) {
1090 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001091 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001093 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001094 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001095 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001096 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001097 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 msg->sl.rq.v_l = 0;
1099 goto http_msg_rqline_eol;
1100 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001101 state = HTTP_MSG_ERROR;
1102 break;
1103
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001104 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001105 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001106 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001107 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001108 goto http_msg_rquri;
1109 }
1110 if (likely(HTTP_IS_SPHT(*ptr)))
1111 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1112 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1113 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001116 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001117 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001118 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001119
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001120 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001121 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001122 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1123 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001124
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001125 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001126 /* non-ASCII chars are forbidden unless option
1127 * accept-invalid-http-request is enabled in the frontend.
1128 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001129 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001130 if (msg->err_pos < -1)
1131 goto invalid_char;
1132 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001133 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001134 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1135 }
1136
1137 if (likely(HTTP_IS_CRLF(*ptr))) {
1138 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1139 goto http_msg_req09_uri_e;
1140 }
1141
1142 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001143 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001144 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001145 state = HTTP_MSG_ERROR;
1146 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001147
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001148 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001149 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001150 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001151 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001152 goto http_msg_rqver;
1153 }
1154 if (likely(HTTP_IS_SPHT(*ptr)))
1155 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1156 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1157 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001158
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001160 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001161 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001162 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001163
1164 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001165 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001166 http_msg_rqline_eol:
1167 /* We have seen the end of line. Note that we do not
1168 * necessarily have the \n yet, but at least we know that we
1169 * have EITHER \r OR \n, otherwise the request would not be
1170 * complete. We can then record the request length and return
1171 * to the caller which will be able to register it.
1172 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001173 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001174 return ptr;
1175 }
1176
1177 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001178 state = HTTP_MSG_ERROR;
1179 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001180
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001181#ifdef DEBUG_FULL
1182 default:
1183 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1184 exit(1);
1185#endif
1186 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001187
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001189 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190 if (ret_state)
1191 *ret_state = state;
1192 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001193 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001194 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001195}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001196
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001197/*
1198 * Returns the data from Authorization header. Function may be called more
1199 * than once so data is stored in txn->auth_data. When no header is found
1200 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1201 * searching again for something we are unable to find anyway.
1202 */
1203
1204char get_http_auth_buff[BUFSIZE];
1205
1206int
1207get_http_auth(struct session *s)
1208{
1209
1210 struct http_txn *txn = &s->txn;
1211 struct chunk auth_method;
1212 struct hdr_ctx ctx;
1213 char *h, *p;
1214 int len;
1215
1216#ifdef DEBUG_AUTH
1217 printf("Auth for session %p: %d\n", s, txn->auth.method);
1218#endif
1219
1220 if (txn->auth.method == HTTP_AUTH_WRONG)
1221 return 0;
1222
1223 if (txn->auth.method)
1224 return 1;
1225
1226 txn->auth.method = HTTP_AUTH_WRONG;
1227
1228 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001229
1230 if (txn->flags & TX_USE_PX_CONN) {
1231 h = "Proxy-Authorization";
1232 len = strlen(h);
1233 } else {
1234 h = "Authorization";
1235 len = strlen(h);
1236 }
1237
Willy Tarreau9b28e032012-10-12 23:49:43 +02001238 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001239 return 0;
1240
1241 h = ctx.line + ctx.val;
1242
1243 p = memchr(h, ' ', ctx.vlen);
1244 if (!p || p == h)
1245 return 0;
1246
1247 chunk_initlen(&auth_method, h, 0, p-h);
1248 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1249
1250 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1251
1252 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
1253 get_http_auth_buff, BUFSIZE - 1);
1254
1255 if (len < 0)
1256 return 0;
1257
1258
1259 get_http_auth_buff[len] = '\0';
1260
1261 p = strchr(get_http_auth_buff, ':');
1262
1263 if (!p)
1264 return 0;
1265
1266 txn->auth.user = get_http_auth_buff;
1267 *p = '\0';
1268 txn->auth.pass = p+1;
1269
1270 txn->auth.method = HTTP_AUTH_BASIC;
1271 return 1;
1272 }
1273
1274 return 0;
1275}
1276
Willy Tarreau58f10d72006-12-04 02:26:12 +01001277
Willy Tarreau8973c702007-01-21 23:58:29 +01001278/*
1279 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001280 * depending on the initial msg->msg_state. The caller is responsible for
1281 * ensuring that the message does not wrap. The function can be preempted
1282 * everywhere when data are missing and recalled at the exact same location
1283 * with no information loss. The message may even be realigned between two
1284 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001285 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001286 * fields. Note that msg->sol will be initialized after completing the first
1287 * state, so that none of the msg pointers has to be initialized prior to the
1288 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001289 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001290void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001291{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001292 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001293 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001294 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001295
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001296 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001297 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001298 ptr = buf->p + msg->next;
1299 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001300
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001301 if (unlikely(ptr >= end))
1302 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001304 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 /*
1306 * First, states that are specific to the response only.
1307 * We check them first so that request and headers are
1308 * closer to each other (accessed more often).
1309 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001310 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001311 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001312 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001313 /* we have a start of message, but we have to check
1314 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001315 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001316 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001317 if (unlikely(ptr != buf->p)) {
1318 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001319 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001320 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001321 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001322 }
Willy Tarreau26927362012-05-18 23:22:52 +02001323 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001324 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001325 hdr_idx_init(idx);
1326 state = HTTP_MSG_RPVER;
1327 goto http_msg_rpver;
1328 }
1329
1330 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1331 goto http_msg_invalid;
1332
1333 if (unlikely(*ptr == '\n'))
1334 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1335 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1336 /* stop here */
1337
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001339 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 EXPECT_LF_HERE(ptr, http_msg_invalid);
1341 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1342 /* stop here */
1343
Willy Tarreau8973c702007-01-21 23:58:29 +01001344 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001345 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001346 case HTTP_MSG_RPVER_SP:
1347 case HTTP_MSG_RPCODE:
1348 case HTTP_MSG_RPCODE_SP:
1349 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001350 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001351 state, ptr, end,
1352 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 if (unlikely(!ptr))
1354 return;
1355
1356 /* we have a full response and we know that we have either a CR
1357 * or an LF at <ptr>.
1358 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001359 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1360
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001361 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 if (likely(*ptr == '\r'))
1363 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1364 goto http_msg_rpline_end;
1365
Willy Tarreau8973c702007-01-21 23:58:29 +01001366 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001367 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 /* msg->sol must point to the first of CR or LF. */
1369 EXPECT_LF_HERE(ptr, http_msg_invalid);
1370 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1371 /* stop here */
1372
1373 /*
1374 * Second, states that are specific to the request only
1375 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001376 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001377 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001378 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001379 /* we have a start of message, but we have to check
1380 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001381 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001382 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001383 if (likely(ptr != buf->p)) {
1384 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001385 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001386 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001387 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 }
Willy Tarreau26927362012-05-18 23:22:52 +02001389 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001390 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001391 state = HTTP_MSG_RQMETH;
1392 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001394
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001395 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1396 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 if (unlikely(*ptr == '\n'))
1399 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001404 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 EXPECT_LF_HERE(ptr, http_msg_invalid);
1406 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001407 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001408
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001409 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001410 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001411 case HTTP_MSG_RQMETH_SP:
1412 case HTTP_MSG_RQURI:
1413 case HTTP_MSG_RQURI_SP:
1414 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001415 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001416 state, ptr, end,
1417 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 if (unlikely(!ptr))
1419 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 /* we have a full request and we know that we have either a CR
1422 * or an LF at <ptr>.
1423 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001425
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001426 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 if (likely(*ptr == '\r'))
1428 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001429 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001430
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001431 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001432 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 /* check for HTTP/0.9 request : no version information available.
1434 * msg->sol must point to the first of CR or LF.
1435 */
1436 if (unlikely(msg->sl.rq.v_l == 0))
1437 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001438
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001439 EXPECT_LF_HERE(ptr, http_msg_invalid);
1440 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001441 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001442
Willy Tarreau8973c702007-01-21 23:58:29 +01001443 /*
1444 * Common states below
1445 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001446 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001447 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001448 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001449 if (likely(!HTTP_IS_CRLF(*ptr))) {
1450 goto http_msg_hdr_name;
1451 }
1452
1453 if (likely(*ptr == '\r'))
1454 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1455 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001456
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001457 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001458 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 /* assumes msg->sol points to the first char */
1460 if (likely(HTTP_IS_TOKEN(*ptr)))
1461 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001462
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001463 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001464 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001465
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001466 if (likely(msg->err_pos < -1) || *ptr == '\n')
1467 goto http_msg_invalid;
1468
1469 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001470 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001471
1472 /* and we still accept this non-token character */
1473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001476 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001477 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(HTTP_IS_SPHT(*ptr)))
1479 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001480
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001481 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001482 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 if (likely(!HTTP_IS_CRLF(*ptr))) {
1485 goto http_msg_hdr_val;
1486 }
1487
1488 if (likely(*ptr == '\r'))
1489 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1490 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 EXPECT_LF_HERE(ptr, http_msg_invalid);
1495 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001496
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001498 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001499 if (likely(HTTP_IS_SPHT(*ptr))) {
1500 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001501 for (; buf->p + msg->sov < ptr; msg->sov++)
1502 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 goto http_msg_hdr_l1_sp;
1504 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001505 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001506 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 goto http_msg_complete_header;
1508
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001509 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001510 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001511 /* assumes msg->sol points to the first char, and msg->sov
1512 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 */
1514 if (likely(!HTTP_IS_CRLF(*ptr)))
1515 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001516
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001517 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 /* Note: we could also copy eol into ->eoh so that we have the
1519 * real header end in case it ends with lots of LWS, but is this
1520 * really needed ?
1521 */
1522 if (likely(*ptr == '\r'))
1523 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1524 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001527 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 EXPECT_LF_HERE(ptr, http_msg_invalid);
1529 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001530
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001532 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001533 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1534 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001535 for (; buf->p + msg->eol < ptr; msg->eol++)
1536 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 goto http_msg_hdr_val;
1538 }
1539 http_msg_complete_header:
1540 /*
1541 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001542 * Assumes msg->sol points to the first char, msg->sov points
1543 * to the first character of the value and msg->eol to the
1544 * first CR or LF so we know how the line ends. We insert last
1545 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001547 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001548 idx, idx->tail) < 0))
1549 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001550
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001551 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001552 if (likely(!HTTP_IS_CRLF(*ptr))) {
1553 goto http_msg_hdr_name;
1554 }
1555
1556 if (likely(*ptr == '\r'))
1557 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1558 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001559
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001560 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001561 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001562 /* Assumes msg->sol points to the first of either CR or LF */
1563 EXPECT_LF_HERE(ptr, http_msg_invalid);
1564 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001565 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001566 msg->eoh = msg->sol;
1567 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001568 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001570
1571 case HTTP_MSG_ERROR:
1572 /* this may only happen if we call http_msg_analyser() twice with an error */
1573 break;
1574
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575#ifdef DEBUG_FULL
1576 default:
1577 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1578 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001579#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 }
1581 http_msg_ood:
1582 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001583 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001584 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001586
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001587 http_msg_invalid:
1588 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001589 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001590 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001591 return;
1592}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001593
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001594/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1595 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1596 * nothing is done and 1 is returned.
1597 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001598static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001599{
1600 int delta;
1601 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001602 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001603
1604 if (msg->sl.rq.v_l != 0)
1605 return 1;
1606
Willy Tarreau9b28e032012-10-12 23:49:43 +02001607 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608 delta = 0;
1609
1610 if (msg->sl.rq.u_l == 0) {
1611 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001612 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001614 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001615 }
1616 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001617 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001618 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001619 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001620 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001621 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001622 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001623 NULL, NULL);
1624 if (unlikely(!cur_end))
1625 return 0;
1626
1627 /* we have a full HTTP/1.0 request now and we know that
1628 * we have either a CR or an LF at <ptr>.
1629 */
1630 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1631 return 1;
1632}
1633
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001634/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001635 * and "keep-alive" values. If we already know that some headers may safely
1636 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001637 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1638 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1639 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1640 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1641 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001642 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001643 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001644void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001645{
Willy Tarreau5b154472009-12-21 20:11:07 +01001646 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001647 const char *hdr_val = "Connection";
1648 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001649
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001650 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001651 return;
1652
Willy Tarreau88d349d2010-01-25 12:15:43 +01001653 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1654 hdr_val = "Proxy-Connection";
1655 hdr_len = 16;
1656 }
1657
Willy Tarreau5b154472009-12-21 20:11:07 +01001658 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001659 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001660 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001661 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1662 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001663 if (to_del & 2)
1664 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001665 else
1666 txn->flags |= TX_CON_KAL_SET;
1667 }
1668 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1669 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001670 if (to_del & 1)
1671 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001672 else
1673 txn->flags |= TX_CON_CLO_SET;
1674 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001675 }
1676
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001677 txn->flags |= TX_HDR_CONN_PRS;
1678 return;
1679}
Willy Tarreau5b154472009-12-21 20:11:07 +01001680
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001681/* Apply desired changes on the Connection: header. Values may be removed and/or
1682 * added depending on the <wanted> flags, which are exclusively composed of
1683 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1684 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1685 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001686void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001687{
1688 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001689 const char *hdr_val = "Connection";
1690 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001691
1692 ctx.idx = 0;
1693
Willy Tarreau88d349d2010-01-25 12:15:43 +01001694
1695 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1696 hdr_val = "Proxy-Connection";
1697 hdr_len = 16;
1698 }
1699
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001700 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001701 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001702 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1703 if (wanted & TX_CON_KAL_SET)
1704 txn->flags |= TX_CON_KAL_SET;
1705 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001706 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001707 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001708 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1709 if (wanted & TX_CON_CLO_SET)
1710 txn->flags |= TX_CON_CLO_SET;
1711 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001712 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001713 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001714 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001715
1716 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1717 return;
1718
1719 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1720 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001721 hdr_val = "Connection: close";
1722 hdr_len = 17;
1723 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1724 hdr_val = "Proxy-Connection: close";
1725 hdr_len = 23;
1726 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001727 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001728 }
1729
1730 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1731 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001732 hdr_val = "Connection: keep-alive";
1733 hdr_len = 22;
1734 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1735 hdr_val = "Proxy-Connection: keep-alive";
1736 hdr_len = 28;
1737 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001738 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001739 }
1740 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001741}
1742
Willy Tarreaua458b672012-03-05 11:17:50 +01001743/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001744 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001745 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001746 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001747 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001748 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001749static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001750{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001751 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001752 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001753 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001754 const char *end = buf->data + buf->size;
1755 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001756 unsigned int chunk = 0;
1757
1758 /* The chunk size is in the following form, though we are only
1759 * interested in the size and CRLF :
1760 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1761 */
1762 while (1) {
1763 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001764 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001765 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001766 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001767 if (c < 0) /* not a hex digit anymore */
1768 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001769 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001770 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001771 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001772 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001773 chunk = (chunk << 4) + c;
1774 }
1775
Willy Tarreaud98cf932009-12-27 22:54:55 +01001776 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001777 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001778 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001779
1780 while (http_is_spht[(unsigned char)*ptr]) {
1781 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001782 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001783 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001784 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001785 }
1786
Willy Tarreaud98cf932009-12-27 22:54:55 +01001787 /* Up to there, we know that at least one byte is present at *ptr. Check
1788 * for the end of chunk size.
1789 */
1790 while (1) {
1791 if (likely(HTTP_IS_CRLF(*ptr))) {
1792 /* we now have a CR or an LF at ptr */
1793 if (likely(*ptr == '\r')) {
1794 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001795 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001796 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001797 return 0;
1798 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001799
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001801 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001802 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001803 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001804 /* done */
1805 break;
1806 }
1807 else if (*ptr == ';') {
1808 /* chunk extension, ends at next CRLF */
1809 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001810 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001811 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001812 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001813
1814 while (!HTTP_IS_CRLF(*ptr)) {
1815 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001816 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001817 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001818 return 0;
1819 }
1820 /* we have a CRLF now, loop above */
1821 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001822 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001823 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001824 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001825 }
1826
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001828 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001829 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001830 */
Willy Tarreaub8ffd372012-09-27 15:08:56 +02001831 if (ptr < ptr_old)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001832 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001833 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001834 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001835 msg->chunk_len = chunk;
1836 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001837 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001839 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001840 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001841 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001842}
1843
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001844/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001845 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001846 * the trailers is found, it is automatically scheduled to be forwarded,
1847 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1848 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001849 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001850 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001851 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001852 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1853 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001854 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001855 * matches the length of trailers already parsed and not forwarded. It is also
1856 * important to note that this function is designed to be able to parse wrapped
1857 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001859static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001860{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001861 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001862
Willy Tarreaua458b672012-03-05 11:17:50 +01001863 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001864 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001865 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001866 const char *ptr = b_ptr(buf, msg->next);
1867 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001868 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001869
1870 /* scan current line and stop at LF or CRLF */
1871 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001872 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873 return 0;
1874
1875 if (*ptr == '\n') {
1876 if (!p1)
1877 p1 = ptr;
1878 p2 = ptr;
1879 break;
1880 }
1881
1882 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001883 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001884 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001886 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001887 p1 = ptr;
1888 }
1889
1890 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001891 if (ptr >= buf->data + buf->size)
1892 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001893 }
1894
1895 /* after LF; point to beginning of next line */
1896 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001897 if (p2 >= buf->data + buf->size)
1898 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001899
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001900 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001901 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001902 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001903
1904 /* schedule this line for forwarding */
1905 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001906 if (msg->sov >= buf->size)
1907 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001908
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001909 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001910 /* LF/CRLF at beginning of line => end of trailers at p2.
1911 * Everything was scheduled for forwarding, there's nothing
1912 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001913 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001914 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 msg->msg_state = HTTP_MSG_DONE;
1916 return 1;
1917 }
1918 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001919 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001920 }
1921}
1922
Willy Tarreau54d23df2012-10-25 19:04:45 +02001923/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001924 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001925 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001926 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001927 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1928 * not enough data are available, the function does not change anything and
1929 * returns zero. If a parse error is encountered, the function returns < 0 and
1930 * does not change anything. Note: this function is designed to parse wrapped
1931 * CRLF at the end of the buffer.
1932 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001933static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001934{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001935 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001936 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001937 int bytes;
1938
1939 /* NB: we'll check data availabilty at the end. It's not a
1940 * problem because whatever we match first will be checked
1941 * against the correct length.
1942 */
1943 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001944 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001945 if (*ptr == '\r') {
1946 bytes++;
1947 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001948 if (ptr >= buf->data + buf->size)
1949 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 }
1951
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001952 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001953 return 0;
1954
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001955 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001958 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001959
1960 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001961 if (ptr >= buf->data + buf->size)
1962 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001963 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1964 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001965 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001966 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1967 return 1;
1968}
Willy Tarreau5b154472009-12-21 20:11:07 +01001969
William Lallemand82fe75c2012-10-23 10:25:10 +02001970
1971/*
1972 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001973 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001974int select_compression_request_header(struct session *s, struct buffer *req)
1975{
1976 struct http_txn *txn = &s->txn;
1977 struct hdr_ctx ctx;
1978 struct comp_algo *comp_algo = NULL;
1979
Willy Tarreau05d84602012-10-26 02:11:25 +02001980 /* Disable compression for older user agents announcing themselves as "Mozilla/4".
1981 * Note all of them are broken but they are very few and the broken ones are there.
1982 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1983 */
1984 ctx.idx = 0;
1985 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
1986 ctx.vlen >= 9 &&
1987 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0) {
1988 s->comp_algo = NULL;
1989 return 0;
1990 }
1991
William Lallemand82fe75c2012-10-23 10:25:10 +02001992 ctx.idx = 0;
1993 /* no compression when Cache-Control: no-transform found */
1994 while (http_find_header2("Cache-Control", 13, req->p, &txn->hdr_idx, &ctx)) {
1995 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) {
1996 s->comp_algo = NULL;
1997 return 0;
1998 }
1999 }
2000
2001 /* search for the algo in the backend in priority or the frontend */
2002 if ((s->be->comp && (comp_algo = s->be->comp->algos)) || (s->fe->comp && (comp_algo = s->fe->comp->algos))) {
2003 ctx.idx = 0;
2004 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2005 for (; comp_algo; comp_algo = comp_algo->next) {
2006 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2007 s->comp_algo = comp_algo;
2008 return 1;
2009 }
2010 }
2011 }
2012 }
2013
2014 /* identity is implicit does not require headers */
2015 if ((s->be->comp && (comp_algo = s->be->comp->algos)) || (s->fe->comp && (comp_algo = s->fe->comp->algos))) {
2016 for (; comp_algo; comp_algo = comp_algo->next) {
2017 if (comp_algo->add_data == identity_add_data) {
2018 s->comp_algo = comp_algo;
2019 return 1;
2020 }
2021 }
2022 }
2023
2024 s->comp_algo = NULL;
2025
2026 return 0;
2027}
2028
2029/*
2030 * Selects a comression algorithm depending of the server response.
2031 */
2032int select_compression_response_header(struct session *s, struct buffer *res)
2033{
2034 struct http_txn *txn = &s->txn;
2035 struct http_msg *msg = &txn->rsp;
2036 struct hdr_ctx ctx;
2037 struct comp_type *comp_type;
2038 char *hdr_val;
2039 int hdr_len;
2040
2041 /* no common compression algorithm was found in request header */
2042 if (s->comp_algo == NULL)
2043 goto fail;
2044
2045 /* HTTP < 1.1 should not be compressed */
2046 if (!(msg->flags & HTTP_MSGF_VER_11))
2047 goto fail;
2048
2049 hdr_val = trash;
2050 ctx.idx = 0;
2051
2052 /* Content-Length is null */
2053 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2054 goto fail;
2055
2056 /* content is already compressed */
2057 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2058 goto fail;
2059
2060 comp_type = NULL;
2061
2062 /* if there was a compression content-type option in the backend or the frontend
2063 * The backend have priority.
2064 */
2065 if ((s->be->comp && (comp_type = s->be->comp->types)) || (s->fe->comp && (comp_type = s->fe->comp->types))) {
2066 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2067 for (; comp_type; comp_type = comp_type->next) {
2068 if (strncmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
2069 /* this Content-Type should be compressed */
2070 break;
2071 }
2072 }
2073 /* this Content-Type should not be compressed */
2074 if (comp_type == NULL)
2075 goto fail;
2076 }
2077
2078 ctx.idx = 0;
2079
2080 /* remove Content-Length header */
2081 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2082 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2083
2084 /* add Transfer-Encoding header */
2085 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2086 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2087
2088 /*
2089 * Add Content-Encoding header when it's not identity encoding.
2090 * RFC 2616 : Identity encoding: This content-coding is used only in the
2091 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2092 * header.
2093 */
2094 if (s->comp_algo->add_data != identity_add_data) {
2095 hdr_len = 18;
2096 memcpy(hdr_val, "Content-Encoding: ", hdr_len);
2097 memcpy(hdr_val + hdr_len, s->comp_algo->name, s->comp_algo->name_len);
2098 hdr_len += s->comp_algo->name_len;
2099 hdr_val[hdr_len] = '\0';
2100 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, hdr_val, hdr_len);
2101 }
2102
2103 /* initialize compression */
2104 if (s->comp_algo->init(&s->comp_ctx.strm, 1) < 0)
2105 goto fail;
2106
2107 return 1;
2108
2109fail:
2110 if (s->comp_algo) {
2111 s->comp_algo->end(&s->comp_ctx.strm);
2112 s->comp_algo = NULL;
2113 }
2114 return 0;
2115}
2116
2117
Willy Tarreaud787e662009-07-07 10:14:51 +02002118/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2119 * processing can continue on next analysers, or zero if it either needs more
2120 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2121 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2122 * when it has nothing left to do, and may remove any analyser when it wants to
2123 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002124 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002125int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002126{
Willy Tarreau59234e92008-11-30 23:51:27 +01002127 /*
2128 * We will parse the partial (or complete) lines.
2129 * We will check the request syntax, and also join multi-line
2130 * headers. An index of all the lines will be elaborated while
2131 * parsing.
2132 *
2133 * For the parsing, we use a 28 states FSM.
2134 *
2135 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002136 * req->buf->p = beginning of request
2137 * req->buf->p + msg->eoh = end of processed headers / start of current one
2138 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002139 * msg->eol = end of current header or line (LF or CRLF)
2140 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002141 *
2142 * At end of parsing, we may perform a capture of the error (if any), and
2143 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2144 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2145 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002146 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002147
Willy Tarreau59234e92008-11-30 23:51:27 +01002148 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002149 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002150 struct http_txn *txn = &s->txn;
2151 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002152 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002153
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002154 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 +01002155 now_ms, __FUNCTION__,
2156 s,
2157 req,
2158 req->rex, req->wex,
2159 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002160 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002161 req->analysers);
2162
Willy Tarreau52a0c602009-08-16 22:45:38 +02002163 /* we're speaking HTTP here, so let's speak HTTP to the client */
2164 s->srv_error = http_return_srv_error;
2165
Willy Tarreau83e3af02009-12-28 17:39:57 +01002166 /* There's a protected area at the end of the buffer for rewriting
2167 * purposes. We don't want to start to parse the request if the
2168 * protected area is affected, because we may have to move processed
2169 * data later, which is much more complicated.
2170 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002171 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002172 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002173 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002174 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2175 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2176 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002177 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002178 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002179 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002180 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002181 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002182 return 0;
2183 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002184 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2185 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2186 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002187 }
2188
Willy Tarreau065e8332010-01-08 00:30:20 +01002189 /* Note that we have the same problem with the response ; we
2190 * may want to send a redirect, error or anything which requires
2191 * some spare space. So we'll ensure that we have at least
2192 * maxrewrite bytes available in the response buffer before
2193 * processing that one. This will only affect pipelined
2194 * keep-alive requests.
2195 */
2196 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002197 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002198 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2199 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2200 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002201 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002202 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002203 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002204 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002205 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002206 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002207 return 0;
2208 }
2209 }
2210
Willy Tarreau9b28e032012-10-12 23:49:43 +02002211 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002212 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002213 }
2214
Willy Tarreau59234e92008-11-30 23:51:27 +01002215 /* 1: we might have to print this header in debug mode */
2216 if (unlikely((global.mode & MODE_DEBUG) &&
2217 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002218 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002219 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002220
Willy Tarreau9b28e032012-10-12 23:49:43 +02002221 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002222 /* this is a bit complex : in case of error on the request line,
2223 * we know that rq.l is still zero, so we display only the part
2224 * up to the end of the line (truncated by debug_hdr).
2225 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002226 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002227 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002228
Willy Tarreau59234e92008-11-30 23:51:27 +01002229 sol += hdr_idx_first_pos(&txn->hdr_idx);
2230 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002231
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 while (cur_idx) {
2233 eol = sol + txn->hdr_idx.v[cur_idx].len;
2234 debug_hdr("clihdr", s, sol, eol);
2235 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2236 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002237 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002238 }
2239
Willy Tarreau58f10d72006-12-04 02:26:12 +01002240
Willy Tarreau59234e92008-11-30 23:51:27 +01002241 /*
2242 * Now we quickly check if we have found a full valid request.
2243 * If not so, we check the FD and buffer states before leaving.
2244 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002245 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002246 * requests are checked first. When waiting for a second request
2247 * on a keep-alive session, if we encounter and error, close, t/o,
2248 * we note the error in the session flags but don't set any state.
2249 * Since the error will be noted there, it will not be counted by
2250 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002251 * Last, we may increase some tracked counters' http request errors on
2252 * the cases that are deliberately the client's fault. For instance,
2253 * a timeout or connection reset is not counted as an error. However
2254 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002255 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002256
Willy Tarreau655dce92009-11-08 13:10:58 +01002257 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002258 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002259 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002260 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002261 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002262 session_inc_http_req_ctr(s);
2263 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002264 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002266 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002267
Willy Tarreau59234e92008-11-30 23:51:27 +01002268 /* 1: Since we are in header mode, if there's no space
2269 * left for headers, we won't be able to free more
2270 * later, so the session will never terminate. We
2271 * must terminate it now.
2272 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002273 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 /* FIXME: check if URI is set and return Status
2275 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002276 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002277 session_inc_http_req_ctr(s);
2278 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002279 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002280 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002281 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002282 goto return_bad_req;
2283 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002284
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002286 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002287 if (!(s->flags & SN_ERR_MASK))
2288 s->flags |= SN_ERR_CLICL;
2289
Willy Tarreaufcffa692010-01-10 14:21:19 +01002290 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002291 goto failed_keep_alive;
2292
Willy Tarreau59234e92008-11-30 23:51:27 +01002293 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002294 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002295 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002296 session_inc_http_err_ctr(s);
2297 }
2298
Willy Tarreau59234e92008-11-30 23:51:27 +01002299 msg->msg_state = HTTP_MSG_ERROR;
2300 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002301
Willy Tarreauda7ff642010-06-23 11:44:09 +02002302 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002303 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002304 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002305 if (s->listener->counters)
2306 s->listener->counters->failed_req++;
2307
Willy Tarreau59234e92008-11-30 23:51:27 +01002308 if (!(s->flags & SN_FINST_MASK))
2309 s->flags |= SN_FINST_R;
2310 return 0;
2311 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002312
Willy Tarreau59234e92008-11-30 23:51:27 +01002313 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002314 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002315 if (!(s->flags & SN_ERR_MASK))
2316 s->flags |= SN_ERR_CLITO;
2317
Willy Tarreaufcffa692010-01-10 14:21:19 +01002318 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002319 goto failed_keep_alive;
2320
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002322 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002323 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002324 session_inc_http_err_ctr(s);
2325 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002327 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002328 msg->msg_state = HTTP_MSG_ERROR;
2329 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002330
Willy Tarreauda7ff642010-06-23 11:44:09 +02002331 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002332 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002333 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002334 if (s->listener->counters)
2335 s->listener->counters->failed_req++;
2336
Willy Tarreau59234e92008-11-30 23:51:27 +01002337 if (!(s->flags & SN_FINST_MASK))
2338 s->flags |= SN_FINST_R;
2339 return 0;
2340 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002341
Willy Tarreau59234e92008-11-30 23:51:27 +01002342 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002343 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002344 if (!(s->flags & SN_ERR_MASK))
2345 s->flags |= SN_ERR_CLICL;
2346
Willy Tarreaufcffa692010-01-10 14:21:19 +01002347 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002348 goto failed_keep_alive;
2349
Willy Tarreau4076a152009-04-02 15:18:36 +02002350 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002351 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002352 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002353 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
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_err_ctr(s);
2358 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002359 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002360 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002361 if (s->listener->counters)
2362 s->listener->counters->failed_req++;
2363
Willy Tarreau59234e92008-11-30 23:51:27 +01002364 if (!(s->flags & SN_FINST_MASK))
2365 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002366 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002367 }
2368
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002369 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002370 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2371 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002372#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002373 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002374 /* We need more data, we have to re-enable quick-ack in case we
2375 * previously disabled it, otherwise we might cause the client
2376 * to delay next data.
2377 */
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002378 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002379 }
2380#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002381
Willy Tarreaufcffa692010-01-10 14:21:19 +01002382 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2383 /* If the client starts to talk, let's fall back to
2384 * request timeout processing.
2385 */
2386 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002387 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002388 }
2389
Willy Tarreau59234e92008-11-30 23:51:27 +01002390 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002391 if (!tick_isset(req->analyse_exp)) {
2392 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2393 (txn->flags & TX_WAIT_NEXT_RQ) &&
2394 tick_isset(s->be->timeout.httpka))
2395 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2396 else
2397 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2398 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002399
Willy Tarreau59234e92008-11-30 23:51:27 +01002400 /* we're not ready yet */
2401 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002402
2403 failed_keep_alive:
2404 /* Here we process low-level errors for keep-alive requests. In
2405 * short, if the request is not the first one and it experiences
2406 * a timeout, read error or shutdown, we just silently close so
2407 * that the client can try again.
2408 */
2409 txn->status = 0;
2410 msg->msg_state = HTTP_MSG_RQBEFORE;
2411 req->analysers = 0;
2412 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002413 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002414 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002415 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002416 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002417
Willy Tarreaud787e662009-07-07 10:14:51 +02002418 /* OK now we have a complete HTTP request with indexed headers. Let's
2419 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002420 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002421 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002422 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002423 * byte after the last LF. msg->sov points to the first byte of data.
2424 * msg->eol cannot be trusted because it may have been left uninitialized
2425 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002426 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002427
Willy Tarreauda7ff642010-06-23 11:44:09 +02002428 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002429 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2430
Willy Tarreaub16a5742010-01-10 14:46:16 +01002431 if (txn->flags & TX_WAIT_NEXT_RQ) {
2432 /* kill the pending keep-alive timeout */
2433 txn->flags &= ~TX_WAIT_NEXT_RQ;
2434 req->analyse_exp = TICK_ETERNITY;
2435 }
2436
2437
Willy Tarreaud787e662009-07-07 10:14:51 +02002438 /* Maybe we found in invalid header name while we were configured not
2439 * to block on that, so we have to capture it now.
2440 */
2441 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002442 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002443
Willy Tarreau59234e92008-11-30 23:51:27 +01002444 /*
2445 * 1: identify the method
2446 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002447 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002448
2449 /* we can make use of server redirect on GET and HEAD */
2450 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2451 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002452
Willy Tarreau59234e92008-11-30 23:51:27 +01002453 /*
2454 * 2: check if the URI matches the monitor_uri.
2455 * We have to do this for every request which gets in, because
2456 * the monitor-uri is defined by the frontend.
2457 */
2458 if (unlikely((s->fe->monitor_uri_len != 0) &&
2459 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002460 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 s->fe->monitor_uri,
2462 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002463 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002464 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002465 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002467
Willy Tarreau59234e92008-11-30 23:51:27 +01002468 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002469 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002470
Willy Tarreau59234e92008-11-30 23:51:27 +01002471 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002472 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002473 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002474
Willy Tarreau59234e92008-11-30 23:51:27 +01002475 ret = acl_pass(ret);
2476 if (cond->pol == ACL_COND_UNLESS)
2477 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002478
Willy Tarreau59234e92008-11-30 23:51:27 +01002479 if (ret) {
2480 /* we fail this request, let's return 503 service unavail */
2481 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002482 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002484 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002485 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002486
Willy Tarreau59234e92008-11-30 23:51:27 +01002487 /* nothing to fail, let's reply normaly */
2488 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002489 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002490 goto return_prx_cond;
2491 }
2492
2493 /*
2494 * 3: Maybe we have to copy the original REQURI for the logs ?
2495 * Note: we cannot log anymore if the request has been
2496 * classified as invalid.
2497 */
2498 if (unlikely(s->logs.logwait & LW_REQ)) {
2499 /* we have a complete HTTP request that we must log */
2500 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2501 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002502
Willy Tarreau59234e92008-11-30 23:51:27 +01002503 if (urilen >= REQURI_LEN)
2504 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002505 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 if (!(s->logs.logwait &= ~LW_REQ))
2509 s->do_log(s);
2510 } else {
2511 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002512 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 }
Willy Tarreau06619262006-12-17 08:37:22 +01002514
William Lallemanda73203e2012-03-12 12:48:57 +01002515 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2516 s->unique_id = pool_alloc2(pool2_uniqueid);
2517 }
2518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002520 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002521 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002522
Willy Tarreau5b154472009-12-21 20:11:07 +01002523 /* ... and check if the request is HTTP/1.1 or above */
2524 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002525 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2526 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2527 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002528 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002529
2530 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002531 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002532
Willy Tarreau88d349d2010-01-25 12:15:43 +01002533 /* if the frontend has "option http-use-proxy-header", we'll check if
2534 * we have what looks like a proxied connection instead of a connection,
2535 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2536 * Note that this is *not* RFC-compliant, however browsers and proxies
2537 * happen to do that despite being non-standard :-(
2538 * We consider that a request not beginning with either '/' or '*' is
2539 * a proxied connection, which covers both "scheme://location" and
2540 * CONNECT ip:port.
2541 */
2542 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002543 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002544 txn->flags |= TX_USE_PX_CONN;
2545
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002546 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002547 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002548
Willy Tarreau59234e92008-11-30 23:51:27 +01002549 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002550 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002551 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002553
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002554 /* 6: determine the transfer-length.
2555 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2556 * the presence of a message-body in a REQUEST and its transfer length
2557 * must be determined that way (in order of precedence) :
2558 * 1. The presence of a message-body in a request is signaled by the
2559 * inclusion of a Content-Length or Transfer-Encoding header field
2560 * in the request's header fields. When a request message contains
2561 * both a message-body of non-zero length and a method that does
2562 * not define any semantics for that request message-body, then an
2563 * origin server SHOULD either ignore the message-body or respond
2564 * with an appropriate error message (e.g., 413). A proxy or
2565 * gateway, when presented the same request, SHOULD either forward
2566 * the request inbound with the message- body or ignore the
2567 * message-body when determining a response.
2568 *
2569 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2570 * and the "chunked" transfer-coding (Section 6.2) is used, the
2571 * transfer-length is defined by the use of this transfer-coding.
2572 * If a Transfer-Encoding header field is present and the "chunked"
2573 * transfer-coding is not present, the transfer-length is defined
2574 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002575 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002576 * 3. If a Content-Length header field is present, its decimal value in
2577 * OCTETs represents both the entity-length and the transfer-length.
2578 * If a message is received with both a Transfer-Encoding header
2579 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002580 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002581 * 4. By the server closing the connection. (Closing the connection
2582 * cannot be used to indicate the end of a request body, since that
2583 * would leave no possibility for the server to send back a response.)
2584 *
2585 * Whenever a transfer-coding is applied to a message-body, the set of
2586 * transfer-codings MUST include "chunked", unless the message indicates
2587 * it is terminated by closing the connection. When the "chunked"
2588 * transfer-coding is used, it MUST be the last transfer-coding applied
2589 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002590 */
2591
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002592 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002593 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002594 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002595 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002596 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002597 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002598 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2599 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002600 /* bad transfer-encoding (chunked followed by something else) */
2601 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002602 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002603 break;
2604 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002605 }
2606
Willy Tarreau32b47f42009-10-18 20:55:02 +02002607 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002608 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002609 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002610 signed long long cl;
2611
Willy Tarreauad14f752011-09-02 20:33:27 +02002612 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002613 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002614 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002615 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002616
Willy Tarreauad14f752011-09-02 20:33:27 +02002617 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002618 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002619 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002620 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002621
Willy Tarreauad14f752011-09-02 20:33:27 +02002622 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002623 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002624 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002625 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002626
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002627 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002628 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002629 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002630 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002631
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002632 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002633 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002634 }
2635
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002636 /* bodyless requests have a known length */
2637 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002638 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002639
Willy Tarreaud787e662009-07-07 10:14:51 +02002640 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002641 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002642 req->analyse_exp = TICK_ETERNITY;
2643 return 1;
2644
2645 return_bad_req:
2646 /* We centralize bad requests processing here */
2647 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2648 /* we detected a parsing error. We want to archive this request
2649 * in the dedicated proxy area for later troubleshooting.
2650 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002651 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002652 }
2653
2654 txn->req.msg_state = HTTP_MSG_ERROR;
2655 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002656 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002657
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002658 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002659 if (s->listener->counters)
2660 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002661
2662 return_prx_cond:
2663 if (!(s->flags & SN_ERR_MASK))
2664 s->flags |= SN_ERR_PRXCOND;
2665 if (!(s->flags & SN_FINST_MASK))
2666 s->flags |= SN_FINST_R;
2667
2668 req->analysers = 0;
2669 req->analyse_exp = TICK_ETERNITY;
2670 return 0;
2671}
2672
Cyril Bonté70be45d2010-10-12 00:14:35 +02002673/* We reached the stats page through a POST request.
2674 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002675 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002676 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002677int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002678{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002679 struct proxy *px = NULL;
2680 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002681
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002682 char key[LINESIZE];
2683 int action = ST_ADM_ACTION_NONE;
2684 int reprocess = 0;
2685
2686 int total_servers = 0;
2687 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002688
2689 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002690 char *st_cur_param = NULL;
2691 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002692
Willy Tarreau9b28e032012-10-12 23:49:43 +02002693 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002694 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002695
2696 cur_param = next_param = end_params;
2697
Willy Tarreau9b28e032012-10-12 23:49:43 +02002698 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002699 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002700 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002701 return 1;
2702 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002703 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002704 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002705 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002706 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002707 }
2708
2709 *end_params = '\0';
2710
Willy Tarreau295a8372011-03-10 11:25:07 +01002711 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002712
2713 /*
2714 * Parse the parameters in reverse order to only store the last value.
2715 * From the html form, the backend and the action are at the end.
2716 */
2717 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002718 char *value;
2719 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002720
2721 cur_param--;
2722 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002723 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002724 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002725 poffset = (cur_param != first_param ? 1 : 0);
2726 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2727 if ((plen > 0) && (plen <= sizeof(key))) {
2728 strncpy(key, cur_param + poffset, plen);
2729 key[plen - 1] = '\0';
2730 } else {
2731 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2732 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002733 }
2734
2735 /* Parse the value */
2736 value = key;
2737 while (*value != '\0' && *value != '=') {
2738 value++;
2739 }
2740 if (*value == '=') {
2741 /* Ok, a value is found, we can mark the end of the key */
2742 *value++ = '\0';
2743 }
2744
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002745 if (!url_decode(key) || !url_decode(value))
2746 break;
2747
Cyril Bonté70be45d2010-10-12 00:14:35 +02002748 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002749 if (!px && (strcmp(key, "b") == 0)) {
2750 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2751 /* the backend name is unknown or ambiguous (duplicate names) */
2752 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2753 goto out;
2754 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002755 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002756 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002757 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002758 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002759 }
2760 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002761 action = ST_ADM_ACTION_ENABLE;
2762 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002763 else if (strcmp(value, "stop") == 0) {
2764 action = ST_ADM_ACTION_STOP;
2765 }
2766 else if (strcmp(value, "start") == 0) {
2767 action = ST_ADM_ACTION_START;
2768 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002769 else if (strcmp(value, "shutdown") == 0) {
2770 action = ST_ADM_ACTION_SHUTDOWN;
2771 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002772 else {
2773 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2774 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002775 }
2776 }
2777 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002778 if (!(px && action)) {
2779 /*
2780 * Indicates that we'll need to reprocess the parameters
2781 * as soon as backend and action are known
2782 */
2783 if (!reprocess) {
2784 st_cur_param = cur_param;
2785 st_next_param = next_param;
2786 }
2787 reprocess = 1;
2788 }
2789 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002790 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002791 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002792 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002793 /* Not already in maintenance, we can change the server state */
2794 sv->state |= SRV_MAINTAIN;
2795 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002796 altered_servers++;
2797 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002798 }
2799 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002800 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002801 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002802 /* Already in maintenance, we can change the server state */
2803 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002804 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002805 altered_servers++;
2806 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002807 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002808 break;
2809 case ST_ADM_ACTION_STOP:
2810 case ST_ADM_ACTION_START:
2811 if (action == ST_ADM_ACTION_START)
2812 sv->uweight = sv->iweight;
2813 else
2814 sv->uweight = 0;
2815
2816 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2817 /* we must take care of not pushing the server to full throttle during slow starts */
2818 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2819 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2820 else
2821 sv->eweight = BE_WEIGHT_SCALE;
2822 sv->eweight *= sv->uweight;
2823 } else {
2824 sv->eweight = sv->uweight;
2825 }
2826
2827 /* static LB algorithms are a bit harder to update */
2828 if (px->lbprm.update_server_eweight)
2829 px->lbprm.update_server_eweight(sv);
2830 else if (sv->eweight) {
2831 if (px->lbprm.set_server_status_up)
2832 px->lbprm.set_server_status_up(sv);
2833 }
2834 else {
2835 if (px->lbprm.set_server_status_down)
2836 px->lbprm.set_server_status_down(sv);
2837 }
2838 altered_servers++;
2839 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002840 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002841 case ST_ADM_ACTION_SHUTDOWN:
2842 if (px->state != PR_STSTOPPED) {
2843 struct session *sess, *sess_bck;
2844
2845 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2846 if (sess->srv_conn == sv)
2847 session_shutdown(sess, SN_ERR_KILLED);
2848
2849 altered_servers++;
2850 total_servers++;
2851 }
2852 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002853 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002854 } else {
2855 /* the server name is unknown or ambiguous (duplicate names) */
2856 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002857 }
2858 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002859 if (reprocess && px && action) {
2860 /* Now, we know the backend and the action chosen by the user.
2861 * We can safely restart from the first server parameter
2862 * to reprocess them
2863 */
2864 cur_param = st_cur_param;
2865 next_param = st_next_param;
2866 reprocess = 0;
2867 goto reprocess_servers;
2868 }
2869
Cyril Bonté70be45d2010-10-12 00:14:35 +02002870 next_param = cur_param;
2871 }
2872 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002873
2874 if (total_servers == 0) {
2875 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2876 }
2877 else if (altered_servers == 0) {
2878 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2879 }
2880 else if (altered_servers == total_servers) {
2881 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2882 }
2883 else {
2884 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2885 }
2886 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002887 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002888}
2889
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002890/* returns a pointer to the first rule which forbids access (deny or http_auth),
2891 * or NULL if everything's OK.
2892 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002893static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002894http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2895{
Willy Tarreauff011f22011-01-06 17:51:27 +01002896 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002897
Willy Tarreauff011f22011-01-06 17:51:27 +01002898 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002899 int ret = 1;
2900
Willy Tarreauff011f22011-01-06 17:51:27 +01002901 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002902 continue;
2903
2904 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002905 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002906 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002907 ret = acl_pass(ret);
2908
Willy Tarreauff011f22011-01-06 17:51:27 +01002909 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002910 ret = !ret;
2911 }
2912
2913 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002914 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002915 return NULL; /* no problem */
2916 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002917 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002918 }
2919 }
2920 return NULL;
2921}
2922
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002923/* This stream analyser runs all HTTP request processing which is common to
2924 * frontends and backends, which means blocking ACLs, filters, connection-close,
2925 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002926 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002927 * either needs more data or wants to immediately abort the request (eg: deny,
2928 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002929 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002930int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002931{
Willy Tarreaud787e662009-07-07 10:14:51 +02002932 struct http_txn *txn = &s->txn;
2933 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002934 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002935 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002936 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002937 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002938 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002939
Willy Tarreau655dce92009-11-08 13:10:58 +01002940 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002941 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002942 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002943 return 0;
2944 }
2945
Willy Tarreau3a816292009-07-07 10:55:49 +02002946 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002947 req->analyse_exp = TICK_ETERNITY;
2948
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002949 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 +02002950 now_ms, __FUNCTION__,
2951 s,
2952 req,
2953 req->rex, req->wex,
2954 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002955 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002956 req->analysers);
2957
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002958 /* first check whether we have some ACLs set to block this request */
2959 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002960 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002961
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002962 ret = acl_pass(ret);
2963 if (cond->pol == ACL_COND_UNLESS)
2964 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002965
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002966 if (ret) {
2967 txn->status = 403;
2968 /* let's log the request time */
2969 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002970 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002971 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002972 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002973 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002974 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002975
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002976 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002977 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002978
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002979 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002980 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002981 do_stats = stats_check_uri(s->rep->prod, txn, px);
2982 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002983 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 +01002984 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002985 else
2986 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002987
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002988 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002989 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002990 txn->status = 403;
2991 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002992 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002993 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002994 s->fe->fe_counters.denied_req++;
2995 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
2996 s->be->be_counters.denied_req++;
2997 if (s->listener->counters)
2998 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002999 goto return_prx_cond;
3000 }
3001
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003002 /* try headers filters */
3003 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003004 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003005 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003006
Willy Tarreau59234e92008-11-30 23:51:27 +01003007 /* has the request been denied ? */
3008 if (txn->flags & TX_CLDENY) {
3009 /* no need to go further */
3010 txn->status = 403;
3011 /* let's log the request time */
3012 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003013 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003014 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003015 goto return_prx_cond;
3016 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003017
3018 /* When a connection is tarpitted, we use the tarpit timeout,
3019 * which may be the same as the connect timeout if unspecified.
3020 * If unset, then set it to zero because we really want it to
3021 * eventually expire. We build the tarpit as an analyser.
3022 */
3023 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003024 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003025 /* wipe the request out so that we can drop the connection early
3026 * if the client closes first.
3027 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003028 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003029 req->analysers = 0; /* remove switching rules etc... */
3030 req->analysers |= AN_REQ_HTTP_TARPIT;
3031 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3032 if (!req->analyse_exp)
3033 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003034 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003035 return 1;
3036 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003037 }
Willy Tarreau06619262006-12-17 08:37:22 +01003038
Willy Tarreau5b154472009-12-21 20:11:07 +01003039 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3040 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003041 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3042 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003043 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3044 * avoid to redo the same work if FE and BE have the same settings (common).
3045 * The method consists in checking if options changed between the two calls
3046 * (implying that either one is non-null, or one of them is non-null and we
3047 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003048 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003049
Willy Tarreaudc008c52010-02-01 16:20:08 +01003050 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3051 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3052 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3053 (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 +01003054 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003055
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003056 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3057 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003058 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003059 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3060 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003061 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003062 tmp = TX_CON_WANT_CLO;
3063
Willy Tarreau5b154472009-12-21 20:11:07 +01003064 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3065 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003066
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003067 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3068 /* parse the Connection header and possibly clean it */
3069 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003070 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003071 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3072 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003073 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003074 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003075 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003076 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003077 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003078
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003079 /* check if client or config asks for explicit close in KAL/SCL */
3080 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3081 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3082 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003083 (!(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 +01003084 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003085 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003086 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003087 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3088 }
Willy Tarreau78599912009-10-17 20:12:21 +02003089
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003090 /* we can be blocked here because the request needs to be authenticated,
3091 * either to pass or to access stats.
3092 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003093 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003094 struct chunk msg;
Willy Tarreauff011f22011-01-06 17:51:27 +01003095 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003096
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003097 if (!realm)
3098 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3099
Willy Tarreau844a7e72010-01-31 21:46:18 +01003100 sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
David du Colombier7af46052012-05-16 14:16:48 +02003101 chunk_initlen(&msg, trash, trashlen, strlen(trash));
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003102 txn->status = 401;
3103 stream_int_retnclose(req->prod, &msg);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003104 /* on 401 we still count one error, because normal browsing
3105 * won't significantly increase the counter but brute force
3106 * attempts will.
3107 */
3108 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003109 goto return_prx_cond;
3110 }
3111
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003112 /* add request headers from the rule sets in the same order */
3113 list_for_each_entry(wl, &px->req_add, list) {
3114 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003115 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003116 ret = acl_pass(ret);
3117 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3118 ret = !ret;
3119 if (!ret)
3120 continue;
3121 }
3122
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003123 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003124 goto return_bad_req;
3125 }
3126
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003127 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003128 struct stats_admin_rule *stats_admin_rule;
3129
3130 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003131 * FIXME!!! that one is rather dangerous, we want to
3132 * make it follow standard rules (eg: clear req->analysers).
3133 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003134
Cyril Bonté474be412010-10-12 00:14:36 +02003135 /* now check whether we have some admin rules for this request */
3136 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3137 int ret = 1;
3138
3139 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003140 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 +02003141 ret = acl_pass(ret);
3142 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3143 ret = !ret;
3144 }
3145
3146 if (ret) {
3147 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003148 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003149 break;
3150 }
3151 }
3152
Cyril Bonté70be45d2010-10-12 00:14:35 +02003153 /* Was the status page requested with a POST ? */
3154 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003155 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003156 if (msg->msg_state < HTTP_MSG_100_SENT) {
3157 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3158 * send an HTTP/1.1 100 Continue intermediate response.
3159 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003160 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003161 struct hdr_ctx ctx;
3162 ctx.idx = 0;
3163 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003164 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01003165 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003166 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003167 }
3168 }
3169 msg->msg_state = HTTP_MSG_100_SENT;
3170 s->logs.tv_request = now; /* update the request timer to reflect full request */
3171 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003172 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003173 /* we need more data */
3174 req->analysers |= an_bit;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003175 channel_dont_connect(req);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003176 return 0;
3177 }
Cyril Bonté474be412010-10-12 00:14:36 +02003178 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003179 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003180 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003181 }
3182
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003183 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003184 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003185 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreau3cefd522012-08-30 15:49:18 +02003186 copy_target(&s->target, &s->rep->prod->conn.target); // for logging only
Willy Tarreauf7bc57c2012-10-03 00:19:48 +02003187 s->rep->prod->conn.xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01003188 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003189 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003190 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3191 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003192
3193 return 0;
3194
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003195 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003196
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003197 /* check whether we have some ACLs set to redirect this request */
3198 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003199 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003200
Willy Tarreauf285f542010-01-03 20:03:03 +01003201 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003202 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003203 ret = acl_pass(ret);
3204 if (rule->cond->pol == ACL_COND_UNLESS)
3205 ret = !ret;
3206 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003207
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003208 if (ret) {
David du Colombier7af46052012-05-16 14:16:48 +02003209 struct chunk rdr = { .str = trash, .size = trashlen, .len = 0 };
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003210 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003211
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003212 /* build redirect message */
3213 switch(rule->code) {
3214 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003215 msg_fmt = HTTP_303;
3216 break;
3217 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003218 msg_fmt = HTTP_301;
3219 break;
3220 case 302:
3221 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003222 msg_fmt = HTTP_302;
3223 break;
3224 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003225
Willy Tarreau3bb9c232010-01-03 12:24:37 +01003226 if (unlikely(!chunk_strcpy(&rdr, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003227 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003228
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003229 switch(rule->type) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003230 case REDIRECT_TYPE_SCHEME: {
3231 const char *path;
3232 const char *host;
3233 struct hdr_ctx ctx;
3234 int pathlen;
3235 int hostlen;
3236
3237 host = "";
3238 hostlen = 0;
3239 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003240 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 +02003241 host = ctx.line + ctx.val;
3242 hostlen = ctx.vlen;
3243 }
3244
3245 path = http_get_path(txn);
3246 /* build message using path */
3247 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003248 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003249 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3250 int qs = 0;
3251 while (qs < pathlen) {
3252 if (path[qs] == '?') {
3253 pathlen = qs;
3254 break;
3255 }
3256 qs++;
3257 }
3258 }
3259 } else {
3260 path = "/";
3261 pathlen = 1;
3262 }
3263
3264 /* check if we can add scheme + "://" + host + path */
3265 if (rdr.len + rule->rdr_len + 3 + hostlen + pathlen > rdr.size - 4)
3266 goto return_bad_req;
3267
3268 /* add scheme */
3269 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3270 rdr.len += rule->rdr_len;
3271
3272 /* add "://" */
3273 memcpy(rdr.str + rdr.len, "://", 3);
3274 rdr.len += 3;
3275
3276 /* add host */
3277 memcpy(rdr.str + rdr.len, host, hostlen);
3278 rdr.len += hostlen;
3279
3280 /* add path */
3281 memcpy(rdr.str + rdr.len, path, pathlen);
3282 rdr.len += pathlen;
3283
3284 /* append a slash at the end of the location is needed and missing */
3285 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3286 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3287 if (rdr.len > rdr.size - 5)
3288 goto return_bad_req;
3289 rdr.str[rdr.len] = '/';
3290 rdr.len++;
3291 }
3292
3293 break;
3294 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003295 case REDIRECT_TYPE_PREFIX: {
3296 const char *path;
3297 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003298
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003299 path = http_get_path(txn);
3300 /* build message using path */
3301 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003302 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003303 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3304 int qs = 0;
3305 while (qs < pathlen) {
3306 if (path[qs] == '?') {
3307 pathlen = qs;
3308 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003309 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003310 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003311 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003312 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003313 } else {
3314 path = "/";
3315 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003316 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003317
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003318 if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003319 goto return_bad_req;
3320
3321 /* add prefix. Note that if prefix == "/", we don't want to
3322 * add anything, otherwise it makes it hard for the user to
3323 * configure a self-redirection.
3324 */
3325 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau06b917c2009-07-06 16:34:52 +02003326 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3327 rdr.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003328 }
3329
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003330 /* add path */
3331 memcpy(rdr.str + rdr.len, path, pathlen);
3332 rdr.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003333
3334 /* append a slash at the end of the location is needed and missing */
3335 if (rdr.len && rdr.str[rdr.len - 1] != '/' &&
3336 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3337 if (rdr.len > rdr.size - 5)
3338 goto return_bad_req;
3339 rdr.str[rdr.len] = '/';
3340 rdr.len++;
3341 }
3342
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003343 break;
3344 }
3345 case REDIRECT_TYPE_LOCATION:
3346 default:
Krzysztof Piotr Oledzki78abe612009-09-27 13:23:20 +02003347 if (rdr.len + rule->rdr_len > rdr.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003348 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003349
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003350 /* add location */
3351 memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len);
3352 rdr.len += rule->rdr_len;
3353 break;
3354 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003355
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003356 if (rule->cookie_len) {
3357 memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14);
3358 rdr.len += 14;
3359 memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len);
3360 rdr.len += rule->cookie_len;
3361 memcpy(rdr.str + rdr.len, "\r\n", 2);
3362 rdr.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003363 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003364
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003365 /* add end of headers and the keep-alive/close status.
3366 * We may choose to set keep-alive if the Location begins
3367 * with a slash, because the client will come back to the
3368 * same server.
3369 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003370 txn->status = rule->code;
3371 /* let's log the request time */
3372 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003373
3374 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003375 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3376 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003377 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3378 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3379 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003380 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003381 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3382 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30);
3383 rdr.len += 30;
3384 } else {
3385 memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24);
3386 rdr.len += 24;
3387 }
Willy Tarreau75661452010-01-10 10:35:01 +01003388 }
3389 memcpy(rdr.str + rdr.len, "\r\n\r\n", 4);
3390 rdr.len += 4;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003391 bo_inject(req->prod->ob, rdr.str, rdr.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003392 /* "eat" the request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003393 bi_fast_delete(req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003394 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003395 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003396 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3397 txn->req.msg_state = HTTP_MSG_CLOSED;
3398 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003399 break;
3400 } else {
3401 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003402 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3403 memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3404 rdr.len += 29;
3405 } else {
3406 memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23);
3407 rdr.len += 23;
3408 }
Willy Tarreau148d0992010-01-10 10:21:21 +01003409 stream_int_retnclose(req->prod, &rdr);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003410 goto return_prx_cond;
3411 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003412 }
3413 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003414
Willy Tarreau2be39392010-01-03 17:24:51 +01003415 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3416 * If this happens, then the data will not come immediately, so we must
3417 * send all what we have without waiting. Note that due to the small gain
3418 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003419 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003420 * itself once used.
3421 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003422 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003423
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003424 /* that's OK for us now, let's move on to next analysers */
3425 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003426
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003427 return_bad_req:
3428 /* We centralize bad requests processing here */
3429 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3430 /* we detected a parsing error. We want to archive this request
3431 * in the dedicated proxy area for later troubleshooting.
3432 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003433 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003434 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003435
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003436 txn->req.msg_state = HTTP_MSG_ERROR;
3437 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003438 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003439
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003440 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003441 if (s->listener->counters)
3442 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003443
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003444 return_prx_cond:
3445 if (!(s->flags & SN_ERR_MASK))
3446 s->flags |= SN_ERR_PRXCOND;
3447 if (!(s->flags & SN_FINST_MASK))
3448 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 req->analysers = 0;
3451 req->analyse_exp = TICK_ETERNITY;
3452 return 0;
3453}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003454
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003455/* This function performs all the processing enabled for the current request.
3456 * It returns 1 if the processing can continue on next analysers, or zero if it
3457 * needs more data, encounters an error, or wants to immediately abort the
3458 * request. It relies on buffers flags, and updates s->req->analysers.
3459 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003460int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003461{
3462 struct http_txn *txn = &s->txn;
3463 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003464
Willy Tarreau655dce92009-11-08 13:10:58 +01003465 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003466 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003467 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003468 return 0;
3469 }
3470
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003471 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 +02003472 now_ms, __FUNCTION__,
3473 s,
3474 req,
3475 req->rex, req->wex,
3476 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003477 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003478 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003479
William Lallemand82fe75c2012-10-23 10:25:10 +02003480 if (s->fe->comp || s->be->comp)
3481 select_compression_request_header(s, req->buf);
3482
Willy Tarreau59234e92008-11-30 23:51:27 +01003483 /*
3484 * Right now, we know that we have processed the entire headers
3485 * and that unwanted requests have been filtered out. We can do
3486 * whatever we want with the remaining request. Also, now we
3487 * may have separate values for ->fe, ->be.
3488 */
Willy Tarreau06619262006-12-17 08:37:22 +01003489
Willy Tarreau59234e92008-11-30 23:51:27 +01003490 /*
3491 * If HTTP PROXY is set we simply get remote server address
3492 * parsing incoming request.
3493 */
3494 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003495 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 +01003496 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003497
Willy Tarreau59234e92008-11-30 23:51:27 +01003498 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003499 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003500 * Note that doing so might move headers in the request, but
3501 * the fields will stay coherent and the URI will not move.
3502 * This should only be performed in the backend.
3503 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003504 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003505 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3506 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003507
Willy Tarreau59234e92008-11-30 23:51:27 +01003508 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003509 * 8: the appsession cookie was looked up very early in 1.2,
3510 * so let's do the same now.
3511 */
3512
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003513 /* It needs to look into the URI unless persistence must be ignored */
3514 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003515 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 +01003516 }
3517
William Lallemanda73203e2012-03-12 12:48:57 +01003518 /* add unique-id if "header-unique-id" is specified */
3519
3520 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3521 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3522
3523 if (s->fe->header_unique_id && s->unique_id) {
3524 int ret = snprintf(trash, global.tune.bufsize, "%s: %s", s->fe->header_unique_id, s->unique_id);
3525 if (ret < 0 || ret > global.tune.bufsize)
3526 goto return_bad_req;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003527 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, trash) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003528 goto return_bad_req;
3529 }
3530
Cyril Bontéb21570a2009-11-29 20:04:48 +01003531 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003532 * 9: add X-Forwarded-For if either the frontend or the backend
3533 * asks for it.
3534 */
3535 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003536 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003537 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003538 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3539 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003540 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003541 /* The header is set to be added only if none is present
3542 * and we found it, so don't do anything.
3543 */
3544 }
Willy Tarreau986a9d22012-08-30 21:11:38 +02003545 else if (s->req->prod->conn.addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003546 /* Add an X-Forwarded-For header unless the source IP is
3547 * in the 'except' network range.
3548 */
3549 if ((!s->fe->except_mask.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003550 (((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 +01003551 != s->fe->except_net.s_addr) &&
3552 (!s->be->except_mask.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003553 (((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 +01003554 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003555 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003556 unsigned char *pn;
Willy Tarreau986a9d22012-08-30 21:11:38 +02003557 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn.addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003558
3559 /* Note: we rely on the backend to get the header name to be used for
3560 * x-forwarded-for, because the header is really meant for the backends.
3561 * However, if the backend did not specify any option, we have to rely
3562 * on the frontend's header name.
3563 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003564 if (s->be->fwdfor_hdr_len) {
3565 len = s->be->fwdfor_hdr_len;
3566 memcpy(trash, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003567 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003568 len = s->fe->fwdfor_hdr_len;
3569 memcpy(trash, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003570 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003571 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003572
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003573 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003574 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003575 }
3576 }
Willy Tarreau986a9d22012-08-30 21:11:38 +02003577 else if (s->req->prod->conn.addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003578 /* FIXME: for the sake of completeness, we should also support
3579 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003580 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003581 int len;
3582 char pn[INET6_ADDRSTRLEN];
3583 inet_ntop(AF_INET6,
Willy Tarreau986a9d22012-08-30 21:11:38 +02003584 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn.addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003585 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003586
Willy Tarreau59234e92008-11-30 23:51:27 +01003587 /* Note: we rely on the backend to get the header name to be used for
3588 * x-forwarded-for, because the header is really meant for the backends.
3589 * However, if the backend did not specify any option, we have to rely
3590 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003591 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003592 if (s->be->fwdfor_hdr_len) {
3593 len = s->be->fwdfor_hdr_len;
3594 memcpy(trash, s->be->fwdfor_hdr_name, len);
3595 } else {
3596 len = s->fe->fwdfor_hdr_len;
3597 memcpy(trash, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003598 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003599 len += sprintf(trash + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003600
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003601 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003602 goto return_bad_req;
3603 }
3604 }
3605
3606 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003607 * 10: add X-Original-To if either the frontend or the backend
3608 * asks for it.
3609 */
3610 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3611
3612 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreau986a9d22012-08-30 21:11:38 +02003613 if (s->req->prod->conn.addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003614 /* Add an X-Original-To header unless the destination IP is
3615 * in the 'except' network range.
3616 */
Willy Tarreau986a9d22012-08-30 21:11:38 +02003617 conn_get_to_addr(&s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003618
Willy Tarreau986a9d22012-08-30 21:11:38 +02003619 if (s->req->prod->conn.addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003620 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003621 (((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 +02003622 != s->fe->except_to.s_addr) &&
3623 (!s->be->except_mask_to.s_addr ||
Willy Tarreau986a9d22012-08-30 21:11:38 +02003624 (((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 +02003625 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003626 int len;
3627 unsigned char *pn;
Willy Tarreau986a9d22012-08-30 21:11:38 +02003628 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn.addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003629
3630 /* Note: we rely on the backend to get the header name to be used for
3631 * x-original-to, because the header is really meant for the backends.
3632 * However, if the backend did not specify any option, we have to rely
3633 * on the frontend's header name.
3634 */
3635 if (s->be->orgto_hdr_len) {
3636 len = s->be->orgto_hdr_len;
3637 memcpy(trash, s->be->orgto_hdr_name, len);
3638 } else {
3639 len = s->fe->orgto_hdr_len;
3640 memcpy(trash, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003641 }
Maik Broemme2850cb42009-04-17 18:53:21 +02003642 len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
3643
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003644 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003645 goto return_bad_req;
3646 }
3647 }
3648 }
3649
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003650 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3651 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003652 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003653 unsigned int want_flags = 0;
3654
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003655 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003656 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3657 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3658 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003659 want_flags |= TX_CON_CLO_SET;
3660 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003661 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3662 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3663 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003664 want_flags |= TX_CON_KAL_SET;
3665 }
3666
3667 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003668 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003669 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003670
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003671
Willy Tarreau522d6c02009-12-06 18:49:18 +01003672 /* If we have no server assigned yet and we're balancing on url_param
3673 * with a POST request, we may be interested in checking the body for
3674 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003675 */
3676 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3677 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003678 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003679 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003680 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003681 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003682 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003683
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003684 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003685 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003686#ifdef TCP_QUICKACK
3687 /* We expect some data from the client. Unless we know for sure
3688 * we already have a full request, we have to re-enable quick-ack
3689 * in case we previously disabled it, otherwise we might cause
3690 * the client to delay further data.
3691 */
3692 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003693 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003694 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003695 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003696#endif
3697 }
Willy Tarreau03945942009-12-22 16:50:27 +01003698
Willy Tarreau59234e92008-11-30 23:51:27 +01003699 /*************************************************************
3700 * OK, that's finished for the headers. We have done what we *
3701 * could. Let's switch to the DATA state. *
3702 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003703 req->analyse_exp = TICK_ETERNITY;
3704 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003705
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003706 /* if the server closes the connection, we want to immediately react
3707 * and close the socket to save packets and syscalls.
3708 */
3709 req->cons->flags |= SI_FL_NOHALF;
3710
Willy Tarreau59234e92008-11-30 23:51:27 +01003711 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003712 /* OK let's go on with the BODY now */
3713 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003714
Willy Tarreau59234e92008-11-30 23:51:27 +01003715 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003716 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003717 /* we detected a parsing error. We want to archive this request
3718 * in the dedicated proxy area for later troubleshooting.
3719 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003720 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003721 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003722
Willy Tarreau59234e92008-11-30 23:51:27 +01003723 txn->req.msg_state = HTTP_MSG_ERROR;
3724 txn->status = 400;
3725 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003726 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003727
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003728 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003729 if (s->listener->counters)
3730 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003731
Willy Tarreau59234e92008-11-30 23:51:27 +01003732 if (!(s->flags & SN_ERR_MASK))
3733 s->flags |= SN_ERR_PRXCOND;
3734 if (!(s->flags & SN_FINST_MASK))
3735 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003736 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003737}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003738
Willy Tarreau60b85b02008-11-30 23:28:40 +01003739/* This function is an analyser which processes the HTTP tarpit. It always
3740 * returns zero, at the beginning because it prevents any other processing
3741 * from occurring, and at the end because it terminates the request.
3742 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003743int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003744{
3745 struct http_txn *txn = &s->txn;
3746
3747 /* This connection is being tarpitted. The CLIENT side has
3748 * already set the connect expiration date to the right
3749 * timeout. We just have to check that the client is still
3750 * there and that the timeout has not expired.
3751 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003752 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003753 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003754 !tick_is_expired(req->analyse_exp, now_ms))
3755 return 0;
3756
3757 /* We will set the queue timer to the time spent, just for
3758 * logging purposes. We fake a 500 server error, so that the
3759 * attacker will not suspect his connection has been tarpitted.
3760 * It will not cause trouble to the logs because we can exclude
3761 * the tarpitted connections by filtering on the 'PT' status flags.
3762 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003763 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3764
3765 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003766 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003767 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003768
3769 req->analysers = 0;
3770 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003771
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003772 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003773 if (s->listener->counters)
3774 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003775
Willy Tarreau60b85b02008-11-30 23:28:40 +01003776 if (!(s->flags & SN_ERR_MASK))
3777 s->flags |= SN_ERR_PRXCOND;
3778 if (!(s->flags & SN_FINST_MASK))
3779 s->flags |= SN_FINST_T;
3780 return 0;
3781}
3782
Willy Tarreaud34af782008-11-30 23:36:37 +01003783/* This function is an analyser which processes the HTTP request body. It looks
3784 * for parameters to be used for the load balancing algorithm (url_param). It
3785 * must only be called after the standard HTTP request processing has occurred,
3786 * because it expects the request to be parsed. It returns zero if it needs to
3787 * read more data, or 1 once it has completed its analysis.
3788 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003789int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003790{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003791 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003792 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003793 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003794
3795 /* We have to parse the HTTP request body to find any required data.
3796 * "balance url_param check_post" should have been the only way to get
3797 * into this. We were brought here after HTTP header analysis, so all
3798 * related structures are ready.
3799 */
3800
Willy Tarreau522d6c02009-12-06 18:49:18 +01003801 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3802 goto missing_data;
3803
3804 if (msg->msg_state < HTTP_MSG_100_SENT) {
3805 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3806 * send an HTTP/1.1 100 Continue intermediate response.
3807 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003808 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003809 struct hdr_ctx ctx;
3810 ctx.idx = 0;
3811 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003812 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003813 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003814 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003815 }
3816 }
3817 msg->msg_state = HTTP_MSG_100_SENT;
3818 }
3819
3820 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003821 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02003822 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003823 * is still null. We must save the body in msg->next because it
3824 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003825 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003826 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003827
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003828 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003829 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3830 else
3831 msg->msg_state = HTTP_MSG_DATA;
3832 }
3833
3834 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003835 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003836 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003837 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003838 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003839 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003840
Willy Tarreau115acb92009-12-26 13:56:06 +01003841 if (!ret)
3842 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003843 else if (ret < 0) {
3844 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003845 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003846 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003847 }
3848
Willy Tarreaud98cf932009-12-27 22:54:55 +01003849 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003850 * We have the first data byte is in msg->sov. We're waiting for at
3851 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003852 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003853
Willy Tarreau124d9912011-03-01 20:30:48 +01003854 if (msg->body_len < limit)
3855 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003856
Willy Tarreau9b28e032012-10-12 23:49:43 +02003857 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003858 goto http_end;
3859
3860 missing_data:
3861 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003862 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02003863 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003864 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003865 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003866
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003867 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003868 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02003869 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003870
3871 if (!(s->flags & SN_ERR_MASK))
3872 s->flags |= SN_ERR_CLITO;
3873 if (!(s->flags & SN_FINST_MASK))
3874 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003875 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003876 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003877
3878 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003879 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003880 /* Not enough data. We'll re-use the http-request
3881 * timeout here. Ideally, we should set the timeout
3882 * relative to the accept() date. We just set the
3883 * request timeout once at the beginning of the
3884 * request.
3885 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003886 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003887 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003888 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003889 return 0;
3890 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003891
3892 http_end:
3893 /* The situation will not evolve, so let's give up on the analysis. */
3894 s->logs.tv_request = now; /* update the request timer to reflect full request */
3895 req->analysers &= ~an_bit;
3896 req->analyse_exp = TICK_ETERNITY;
3897 return 1;
3898
3899 return_bad_req: /* let's centralize all bad requests */
3900 txn->req.msg_state = HTTP_MSG_ERROR;
3901 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003902 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003903
Willy Tarreau79ebac62010-06-07 13:47:49 +02003904 if (!(s->flags & SN_ERR_MASK))
3905 s->flags |= SN_ERR_PRXCOND;
3906 if (!(s->flags & SN_FINST_MASK))
3907 s->flags |= SN_FINST_R;
3908
Willy Tarreau522d6c02009-12-06 18:49:18 +01003909 return_err_msg:
3910 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003911 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003912 if (s->listener->counters)
3913 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003914 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003915}
3916
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003917/* send a server's name with an outgoing request over an established connection.
3918 * Note: this function is designed to be called once the request has been scheduled
3919 * for being forwarded. This is the reason why it rewinds the buffer before
3920 * proceeding.
3921 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003922int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003923
3924 struct hdr_ctx ctx;
3925
Mark Lamourinec2247f02012-01-04 13:02:01 -05003926 char *hdr_name = be->server_id_hdr_name;
3927 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003928 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003929 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003930 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003931
William Lallemandd9e90662012-01-30 17:27:17 +01003932 ctx.idx = 0;
3933
Willy Tarreau9b28e032012-10-12 23:49:43 +02003934 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003935 if (old_o) {
3936 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003937 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003938 }
3939
Willy Tarreau9b28e032012-10-12 23:49:43 +02003940 old_i = chn->buf->i;
3941 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 -05003942 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003943 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003944 }
3945
3946 /* Add the new header requested with the server value */
3947 hdr_val = trash;
3948 memcpy(hdr_val, hdr_name, hdr_name_len);
3949 hdr_val += hdr_name_len;
3950 *hdr_val++ = ':';
3951 *hdr_val++ = ' ';
David du Colombier7af46052012-05-16 14:16:48 +02003952 hdr_val += strlcpy2(hdr_val, srv_name, trash + trashlen - hdr_val);
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003953 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash, hdr_val - trash);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003954
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003955 if (old_o) {
3956 /* If this was a forwarded request, we must readjust the amount of
3957 * data to be forwarded in order to take into account the size
3958 * variations.
3959 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003960 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003961 }
3962
Mark Lamourinec2247f02012-01-04 13:02:01 -05003963 return 0;
3964}
3965
Willy Tarreau610ecce2010-01-04 21:15:02 +01003966/* Terminate current transaction and prepare a new one. This is very tricky
3967 * right now but it works.
3968 */
3969void http_end_txn_clean_session(struct session *s)
3970{
3971 /* FIXME: We need a more portable way of releasing a backend's and a
3972 * server's connections. We need a safer way to reinitialize buffer
3973 * flags. We also need a more accurate method for computing per-request
3974 * data.
3975 */
3976 http_silent_debug(__LINE__, s);
3977
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003978 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02003979 si_shutr(s->req->cons);
3980 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003981
3982 http_silent_debug(__LINE__, s);
3983
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003984 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003985 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003986 if (unlikely(s->srv_conn))
3987 sess_change_server(s, NULL);
3988 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003989
3990 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3991 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003992 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003993
3994 if (s->txn.status) {
3995 int n;
3996
3997 n = s->txn.status / 100;
3998 if (n < 1 || n > 5)
3999 n = 0;
4000
4001 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004002 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003
Willy Tarreau24657792010-02-26 10:30:28 +01004004 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004005 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004006 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007 }
4008
4009 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004010 s->logs.bytes_in -= s->req->buf->i;
4011 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004012
4013 /* let's do a final log if we need it */
4014 if (s->logs.logwait &&
4015 !(s->flags & SN_MONITOR) &&
4016 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4017 s->do_log(s);
4018 }
4019
4020 s->logs.accept_date = date; /* user-visible date for logging */
4021 s->logs.tv_accept = now; /* corrected date for internal use */
4022 tv_zero(&s->logs.tv_request);
4023 s->logs.t_queue = -1;
4024 s->logs.t_connect = -1;
4025 s->logs.t_data = -1;
4026 s->logs.t_close = 0;
4027 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4028 s->logs.srv_queue_size = 0; /* we will get this number soon */
4029
Willy Tarreau9b28e032012-10-12 23:49:43 +02004030 s->logs.bytes_in = s->req->total = s->req->buf->i;
4031 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004032
4033 if (s->pend_pos)
4034 pendconn_free(s->pend_pos);
4035
Willy Tarreau827aee92011-03-10 16:55:02 +01004036 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004037 if (s->flags & SN_CURR_SESS) {
4038 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01004039 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004040 }
Willy Tarreau827aee92011-03-10 16:55:02 +01004041 if (may_dequeue_tasks(target_srv(&s->target), s->be))
4042 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004043 }
4044
Willy Tarreau9e000c62011-03-10 14:03:36 +01004045 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004046
4047 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreaufb7508a2012-05-21 16:47:54 +02004048 s->req->cons->conn.t.sock.fd = -1; /* just to help with debugging */
Willy Tarreau505e34a2012-07-06 10:17:53 +02004049 s->req->cons->conn.flags = CO_FL_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004050 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004051 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004052 s->req->cons->err_loc = NULL;
4053 s->req->cons->exp = TICK_ETERNITY;
4054 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004055 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4056 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 +02004057 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 +01004058 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
4059 s->txn.meth = 0;
4060 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004061 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004062 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004063 s->req->cons->flags |= SI_FL_INDEP_STR;
4064
Willy Tarreau96e31212011-05-30 18:10:30 +02004065 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004066 s->req->flags |= CF_NEVER_WAIT;
4067 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004068 }
4069
Willy Tarreau610ecce2010-01-04 21:15:02 +01004070 /* if the request buffer is not empty, it means we're
4071 * about to process another request, so send pending
4072 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004073 * Just don't do this if the buffer is close to be full,
4074 * because the request will wait for it to flush a little
4075 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004076 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004077 if (s->req->buf->i) {
4078 if (s->rep->buf->o &&
4079 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4080 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004081 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004082 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004083
4084 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004085 channel_auto_read(s->req);
4086 channel_auto_close(s->req);
4087 channel_auto_read(s->rep);
4088 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004089
Willy Tarreau342b11c2010-11-24 16:22:09 +01004090 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004091 s->rep->analysers = 0;
4092
4093 http_silent_debug(__LINE__, s);
4094}
4095
4096
4097/* This function updates the request state machine according to the response
4098 * state machine and buffer flags. It returns 1 if it changes anything (flag
4099 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4100 * it is only used to find when a request/response couple is complete. Both
4101 * this function and its equivalent should loop until both return zero. It
4102 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4103 */
4104int http_sync_req_state(struct session *s)
4105{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004106 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004107 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004108 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004109 unsigned int old_state = txn->req.msg_state;
4110
4111 http_silent_debug(__LINE__, s);
4112 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4113 return 0;
4114
4115 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004116 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004117 * We can shut the read side unless we want to abort_on_close,
4118 * or we have a POST request. The issue with POST requests is
4119 * that some browsers still send a CRLF after the request, and
4120 * this CRLF must be read so that it does not remain in the kernel
4121 * buffers, otherwise a close could cause an RST on some systems
4122 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004123 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004124 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004125 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004126
4127 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4128 goto wait_other_side;
4129
4130 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4131 /* The server has not finished to respond, so we
4132 * don't want to move in order not to upset it.
4133 */
4134 goto wait_other_side;
4135 }
4136
4137 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4138 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004139 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004140 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004141 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004142 goto wait_other_side;
4143 }
4144
4145 /* When we get here, it means that both the request and the
4146 * response have finished receiving. Depending on the connection
4147 * mode, we'll have to wait for the last bytes to leave in either
4148 * direction, and sometimes for a close to be effective.
4149 */
4150
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004151 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4152 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004153 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4154 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004155 }
4156 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4157 /* Option forceclose is set, or either side wants to close,
4158 * let's enforce it now that we're not expecting any new
4159 * data to come. The caller knows the session is complete
4160 * once both states are CLOSED.
4161 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004162 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4163 channel_shutr_now(chn);
4164 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004165 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004166 }
4167 else {
4168 /* The last possible modes are keep-alive and tunnel. Since tunnel
4169 * mode does not set the body analyser, we can't reach this place
4170 * in tunnel mode, so we're left with keep-alive only.
4171 * This mode is currently not implemented, we switch to tunnel mode.
4172 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004173 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +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 }
4177
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004178 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004179 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004180 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004181
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004182 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004183 txn->req.msg_state = HTTP_MSG_CLOSING;
4184 goto http_msg_closing;
4185 }
4186 else {
4187 txn->req.msg_state = HTTP_MSG_CLOSED;
4188 goto http_msg_closed;
4189 }
4190 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004191 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004192 }
4193
4194 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4195 http_msg_closing:
4196 /* nothing else to forward, just waiting for the output buffer
4197 * to be empty and for the shutw_now to take effect.
4198 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004199 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200 txn->req.msg_state = HTTP_MSG_CLOSED;
4201 goto http_msg_closed;
4202 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004203 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004204 txn->req.msg_state = HTTP_MSG_ERROR;
4205 goto wait_other_side;
4206 }
4207 }
4208
4209 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4210 http_msg_closed:
4211 goto wait_other_side;
4212 }
4213
4214 wait_other_side:
4215 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004216 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004217}
4218
4219
4220/* This function updates the response state machine according to the request
4221 * state machine and buffer flags. It returns 1 if it changes anything (flag
4222 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4223 * it is only used to find when a request/response couple is complete. Both
4224 * this function and its equivalent should loop until both return zero. It
4225 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4226 */
4227int http_sync_res_state(struct session *s)
4228{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004229 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004230 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004231 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004232 unsigned int old_state = txn->rsp.msg_state;
4233
4234 http_silent_debug(__LINE__, s);
4235 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4236 return 0;
4237
4238 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4239 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004240 * still monitor the server connection for a possible close
4241 * while the request is being uploaded, so we don't disable
4242 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004243 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004244 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004245
4246 if (txn->req.msg_state == HTTP_MSG_ERROR)
4247 goto wait_other_side;
4248
4249 if (txn->req.msg_state < HTTP_MSG_DONE) {
4250 /* The client seems to still be sending data, probably
4251 * because we got an error response during an upload.
4252 * We have the choice of either breaking the connection
4253 * or letting it pass through. Let's do the later.
4254 */
4255 goto wait_other_side;
4256 }
4257
4258 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4259 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004260 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004261 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004262 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 goto wait_other_side;
4264 }
4265
4266 /* When we get here, it means that both the request and the
4267 * response have finished receiving. Depending on the connection
4268 * mode, we'll have to wait for the last bytes to leave in either
4269 * direction, and sometimes for a close to be effective.
4270 */
4271
4272 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4273 /* Server-close mode : shut read and wait for the request
4274 * side to close its output buffer. The caller will detect
4275 * when we're in DONE and the other is in CLOSED and will
4276 * catch that for the final cleanup.
4277 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004278 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4279 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004280 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004281 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4282 /* Option forceclose is set, or either side wants to close,
4283 * let's enforce it now that we're not expecting any new
4284 * data to come. The caller knows the session is complete
4285 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004286 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004287 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4288 channel_shutr_now(chn);
4289 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004290 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004291 }
4292 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004293 /* The last possible modes are keep-alive and tunnel. Since tunnel
4294 * mode does not set the body analyser, we can't reach this place
4295 * in tunnel mode, so we're left with keep-alive only.
4296 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004297 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004298 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004300 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 }
4302
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004303 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004305 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004306 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4307 goto http_msg_closing;
4308 }
4309 else {
4310 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4311 goto http_msg_closed;
4312 }
4313 }
4314 goto wait_other_side;
4315 }
4316
4317 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4318 http_msg_closing:
4319 /* nothing else to forward, just waiting for the output buffer
4320 * to be empty and for the shutw_now to take effect.
4321 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004322 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004323 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4324 goto http_msg_closed;
4325 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004326 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004327 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004328 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004329 if (target_srv(&s->target))
4330 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 goto wait_other_side;
4332 }
4333 }
4334
4335 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4336 http_msg_closed:
4337 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004338 bi_erase(chn);
4339 channel_auto_close(chn);
4340 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341 goto wait_other_side;
4342 }
4343
4344 wait_other_side:
4345 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004346 /* We force the response to leave immediately if we're waiting for the
4347 * other side, since there is no pending shutdown to push it out.
4348 */
4349 if (!channel_is_empty(chn))
4350 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004351 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004352}
4353
4354
4355/* Resync the request and response state machines. Return 1 if either state
4356 * changes.
4357 */
4358int http_resync_states(struct session *s)
4359{
4360 struct http_txn *txn = &s->txn;
4361 int old_req_state = txn->req.msg_state;
4362 int old_res_state = txn->rsp.msg_state;
4363
4364 http_silent_debug(__LINE__, s);
4365 http_sync_req_state(s);
4366 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004367 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004368 if (!http_sync_res_state(s))
4369 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004370 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004371 if (!http_sync_req_state(s))
4372 break;
4373 }
4374 http_silent_debug(__LINE__, s);
4375 /* OK, both state machines agree on a compatible state.
4376 * There are a few cases we're interested in :
4377 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4378 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4379 * directions, so let's simply disable both analysers.
4380 * - HTTP_MSG_CLOSED on the response only means we must abort the
4381 * request.
4382 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4383 * with server-close mode means we've completed one request and we
4384 * must re-initialize the server connection.
4385 */
4386
4387 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4388 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4389 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4390 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4391 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004392 channel_auto_close(s->req);
4393 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004394 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004395 channel_auto_close(s->rep);
4396 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004397 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004398 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4399 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004400 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004401 (s->rep->flags & CF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004402 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004403 channel_auto_close(s->rep);
4404 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004405 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004406 channel_abort(s->req);
4407 channel_auto_close(s->req);
4408 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004409 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004410 }
4411 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4412 txn->rsp.msg_state == HTTP_MSG_DONE &&
4413 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4414 /* server-close: terminate this server connection and
4415 * reinitialize a fresh-new transaction.
4416 */
4417 http_end_txn_clean_session(s);
4418 }
4419
4420 http_silent_debug(__LINE__, s);
4421 return txn->req.msg_state != old_req_state ||
4422 txn->rsp.msg_state != old_res_state;
4423}
4424
Willy Tarreaud98cf932009-12-27 22:54:55 +01004425/* This function is an analyser which forwards request body (including chunk
4426 * sizes if any). It is called as soon as we must forward, even if we forward
4427 * zero byte. The only situation where it must not be called is when we're in
4428 * tunnel mode and we want to forward till the close. It's used both to forward
4429 * remaining data and to resync after end of body. It expects the msg_state to
4430 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4431 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004432 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004433 * bytes of pending data + the headers if not already done (between sol and sov).
4434 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004435 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004436int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004437{
4438 struct http_txn *txn = &s->txn;
4439 struct http_msg *msg = &s->txn.req;
4440
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004441 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4442 return 0;
4443
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004444 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004445 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004446 /* Output closed while we were sending data. We must abort and
4447 * wake the other side up.
4448 */
4449 msg->msg_state = HTTP_MSG_ERROR;
4450 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004451 return 1;
4452 }
4453
Willy Tarreau4fe41902010-06-07 22:27:41 +02004454 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004455 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004456
4457 /* Note that we don't have to send 100-continue back because we don't
4458 * need the data to complete our job, and it's up to the server to
4459 * decide whether to return 100, 417 or anything else in return of
4460 * an "Expect: 100-continue" header.
4461 */
4462
4463 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004464 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004465 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004466 * is still null. We must save the body in msg->next because it
4467 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004468 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004469 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004470
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004471 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004472 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004473 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004474 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004475 }
4476
Willy Tarreaud98cf932009-12-27 22:54:55 +01004477 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004478 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004479
Willy Tarreau610ecce2010-01-04 21:15:02 +01004480 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004481 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004482 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004483 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004484 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004485 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004486 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004487 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004488 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004489
Willy Tarreaucaabe412010-01-03 23:08:28 +01004490 if (msg->msg_state == HTTP_MSG_DATA) {
4491 /* must still forward */
4492 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004493 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004494
4495 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004496 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004497 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004498 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004499 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004500 }
4501 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004502 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004503 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004504 * TRAILERS state.
4505 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004506 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004507
Willy Tarreau54d23df2012-10-25 19:04:45 +02004508 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004509 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004510 else if (ret < 0) {
4511 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004512 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004513 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004514 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004515 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004516 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004517 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004518 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004519 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004520 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004521
4522 if (ret == 0)
4523 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004524 else if (ret < 0) {
4525 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004526 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004527 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004528 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004529 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004530 /* we're in MSG_CHUNK_SIZE now */
4531 }
4532 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004533 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004534
4535 if (ret == 0)
4536 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004537 else if (ret < 0) {
4538 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004539 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004540 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004541 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004542 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004543 /* we're in HTTP_MSG_DONE now */
4544 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004546 int old_state = msg->msg_state;
4547
Willy Tarreau610ecce2010-01-04 21:15:02 +01004548 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004549 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004550 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4551 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004552 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004553 if (http_resync_states(s)) {
4554 /* some state changes occurred, maybe the analyser
4555 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004556 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004557 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004558 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004559 /* request errors are most likely due to
4560 * the server aborting the transfer.
4561 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004562 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004563 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004564 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004565 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004566 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004567 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004568 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004569 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004570
4571 /* If "option abortonclose" is set on the backend, we
4572 * want to monitor the client's connection and forward
4573 * any shutdown notification to the server, which will
4574 * decide whether to close or to go on processing the
4575 * request.
4576 */
4577 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004578 channel_auto_read(req);
4579 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004580 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004581 else if (s->txn.meth == HTTP_METH_POST) {
4582 /* POST requests may require to read extra CRLF
4583 * sent by broken browsers and which could cause
4584 * an RST to be sent upon close on some systems
4585 * (eg: Linux).
4586 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004587 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004588 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004589
Willy Tarreau610ecce2010-01-04 21:15:02 +01004590 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004591 }
4592 }
4593
Willy Tarreaud98cf932009-12-27 22:54:55 +01004594 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004595 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004596 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004597 if (!(s->flags & SN_ERR_MASK))
4598 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004599 if (!(s->flags & SN_FINST_MASK)) {
4600 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4601 s->flags |= SN_FINST_H;
4602 else
4603 s->flags |= SN_FINST_D;
4604 }
4605
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004606 s->fe->fe_counters.cli_aborts++;
4607 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004608 if (target_srv(&s->target))
4609 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004610
4611 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004612 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004613
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004614 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004615 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004616 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004617
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004618 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004619 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004620 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004621 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004622 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004623
Willy Tarreau5c620922011-05-11 19:56:11 +02004624 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004625 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004626 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004627 * modes are already handled by the stream sock layer. We must not do
4628 * this in content-length mode because it could present the MSG_MORE
4629 * flag with the last block of forwarded data, which would cause an
4630 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004631 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004632 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004633 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004634
Willy Tarreau610ecce2010-01-04 21:15:02 +01004635 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004636 return 0;
4637
Willy Tarreaud98cf932009-12-27 22:54:55 +01004638 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004639 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004640 if (s->listener->counters)
4641 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004642 return_bad_req_stats_ok:
4643 txn->req.msg_state = HTTP_MSG_ERROR;
4644 if (txn->status) {
4645 /* Note: we don't send any error if some data were already sent */
4646 stream_int_retnclose(req->prod, NULL);
4647 } else {
4648 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004649 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004650 }
4651 req->analysers = 0;
4652 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004653
4654 if (!(s->flags & SN_ERR_MASK))
4655 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004656 if (!(s->flags & SN_FINST_MASK)) {
4657 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4658 s->flags |= SN_FINST_H;
4659 else
4660 s->flags |= SN_FINST_D;
4661 }
4662 return 0;
4663
4664 aborted_xfer:
4665 txn->req.msg_state = HTTP_MSG_ERROR;
4666 if (txn->status) {
4667 /* Note: we don't send any error if some data were already sent */
4668 stream_int_retnclose(req->prod, NULL);
4669 } else {
4670 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004671 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004672 }
4673 req->analysers = 0;
4674 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4675
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004676 s->fe->fe_counters.srv_aborts++;
4677 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004678 if (target_srv(&s->target))
4679 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004680
4681 if (!(s->flags & SN_ERR_MASK))
4682 s->flags |= SN_ERR_SRVCL;
4683 if (!(s->flags & SN_FINST_MASK)) {
4684 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4685 s->flags |= SN_FINST_H;
4686 else
4687 s->flags |= SN_FINST_D;
4688 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004689 return 0;
4690}
4691
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004692/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4693 * processing can continue on next analysers, or zero if it either needs more
4694 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4695 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4696 * when it has nothing left to do, and may remove any analyser when it wants to
4697 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004698 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004699int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004700{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004701 struct http_txn *txn = &s->txn;
4702 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004703 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004704 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004705 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004706 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004707
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004708 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 +02004709 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004710 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004711 rep,
4712 rep->rex, rep->wex,
4713 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004714 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004715 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004716
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004717 /*
4718 * Now parse the partial (or complete) lines.
4719 * We will check the response syntax, and also join multi-line
4720 * headers. An index of all the lines will be elaborated while
4721 * parsing.
4722 *
4723 * For the parsing, we use a 28 states FSM.
4724 *
4725 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004726 * rep->buf->p = beginning of response
4727 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4728 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004729 * msg->eol = end of current header or line (LF or CRLF)
4730 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004731 */
4732
Willy Tarreau83e3af02009-12-28 17:39:57 +01004733 /* There's a protected area at the end of the buffer for rewriting
4734 * purposes. We don't want to start to parse the request if the
4735 * protected area is affected, because we may have to move processed
4736 * data later, which is much more complicated.
4737 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004738 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004739 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004740 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4741 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4742 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004743 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004744 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004745 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004746 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004747 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004748 return 0;
4749 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004750 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4751 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004752 }
4753
Willy Tarreau9b28e032012-10-12 23:49:43 +02004754 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004755 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004756 }
4757
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004758 /* 1: we might have to print this header in debug mode */
4759 if (unlikely((global.mode & MODE_DEBUG) &&
4760 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004761 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004762 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004763
Willy Tarreau9b28e032012-10-12 23:49:43 +02004764 sol = rep->buf->p;
4765 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004766 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004767
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004768 sol += hdr_idx_first_pos(&txn->hdr_idx);
4769 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004770
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004771 while (cur_idx) {
4772 eol = sol + txn->hdr_idx.v[cur_idx].len;
4773 debug_hdr("srvhdr", s, sol, eol);
4774 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4775 cur_idx = txn->hdr_idx.v[cur_idx].next;
4776 }
4777 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004778
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004779 /*
4780 * Now we quickly check if we have found a full valid response.
4781 * If not so, we check the FD and buffer states before leaving.
4782 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004783 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004784 * responses are checked first.
4785 *
4786 * Depending on whether the client is still there or not, we
4787 * may send an error response back or not. Note that normally
4788 * we should only check for HTTP status there, and check I/O
4789 * errors somewhere else.
4790 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004791
Willy Tarreau655dce92009-11-08 13:10:58 +01004792 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004793 /* Invalid response */
4794 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4795 /* we detected a parsing error. We want to archive this response
4796 * in the dedicated proxy area for later troubleshooting.
4797 */
4798 hdr_response_bad:
4799 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004800 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004801
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004802 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004803 if (target_srv(&s->target)) {
4804 target_srv(&s->target)->counters.failed_resp++;
4805 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004806 }
Willy Tarreau64648412010-03-05 10:41:54 +01004807 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004808 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004809 rep->analysers = 0;
4810 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004811 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004812 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004813 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004814
4815 if (!(s->flags & SN_ERR_MASK))
4816 s->flags |= SN_ERR_PRXCOND;
4817 if (!(s->flags & SN_FINST_MASK))
4818 s->flags |= SN_FINST_H;
4819
4820 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004821 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004822
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004823 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004824 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004825 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02004826 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004827 goto hdr_response_bad;
4828 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004829
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004830 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004831 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004832 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004833 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004834
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004835 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004836 if (target_srv(&s->target)) {
4837 target_srv(&s->target)->counters.failed_resp++;
4838 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004839 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004840
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004841 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004842 rep->analysers = 0;
4843 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004844 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004845 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004846 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004847
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004848 if (!(s->flags & SN_ERR_MASK))
4849 s->flags |= SN_ERR_SRVCL;
4850 if (!(s->flags & SN_FINST_MASK))
4851 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004852 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004853 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004854
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004855 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004856 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004857 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004858 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004859
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004860 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004861 if (target_srv(&s->target)) {
4862 target_srv(&s->target)->counters.failed_resp++;
4863 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004864 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004865
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004866 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004867 rep->analysers = 0;
4868 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004869 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004870 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004871 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004872
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004873 if (!(s->flags & SN_ERR_MASK))
4874 s->flags |= SN_ERR_SRVTO;
4875 if (!(s->flags & SN_FINST_MASK))
4876 s->flags |= SN_FINST_H;
4877 return 0;
4878 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004879
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004880 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004881 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004882 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004883 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004884
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004885 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004886 if (target_srv(&s->target)) {
4887 target_srv(&s->target)->counters.failed_resp++;
4888 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004889 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004890
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004891 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004892 rep->analysers = 0;
4893 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004894 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004895 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004896 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004897
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004898 if (!(s->flags & SN_ERR_MASK))
4899 s->flags |= SN_ERR_SRVCL;
4900 if (!(s->flags & SN_FINST_MASK))
4901 s->flags |= SN_FINST_H;
4902 return 0;
4903 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004904
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004905 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004906 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004907 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004908 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004909
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004910 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004911 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004912 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004913
4914 if (!(s->flags & SN_ERR_MASK))
4915 s->flags |= SN_ERR_CLICL;
4916 if (!(s->flags & SN_FINST_MASK))
4917 s->flags |= SN_FINST_H;
4918
4919 /* process_session() will take care of the error */
4920 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004921 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004922
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004923 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004924 return 0;
4925 }
4926
4927 /* More interesting part now : we know that we have a complete
4928 * response which at least looks like HTTP. We have an indicator
4929 * of each header's length, so we can parse them quickly.
4930 */
4931
4932 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004933 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004934
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004935 /*
4936 * 1: get the status code
4937 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004938 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004939 if (n < 1 || n > 5)
4940 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004941 /* when the client triggers a 4xx from the server, it's most often due
4942 * to a missing object or permission. These events should be tracked
4943 * because if they happen often, it may indicate a brute force or a
4944 * vulnerability scan.
4945 */
4946 if (n == 4)
4947 session_inc_http_err_ctr(s);
4948
Willy Tarreau827aee92011-03-10 16:55:02 +01004949 if (target_srv(&s->target))
4950 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004951
Willy Tarreau5b154472009-12-21 20:11:07 +01004952 /* check if the response is HTTP/1.1 or above */
4953 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02004954 ((rep->buf->p[5] > '1') ||
4955 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004956 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004957
4958 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004959 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01004960
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004961 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004962 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004963
Willy Tarreau9b28e032012-10-12 23:49:43 +02004964 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004965
Willy Tarreau39650402010-03-15 19:44:39 +01004966 /* Adjust server's health based on status code. Note: status codes 501
4967 * and 505 are triggered on demand by client request, so we must not
4968 * count them as server failures.
4969 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004970 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004971 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004972 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004973 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004974 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004975 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004976
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004977 /*
4978 * 2: check for cacheability.
4979 */
4980
4981 switch (txn->status) {
4982 case 200:
4983 case 203:
4984 case 206:
4985 case 300:
4986 case 301:
4987 case 410:
4988 /* RFC2616 @13.4:
4989 * "A response received with a status code of
4990 * 200, 203, 206, 300, 301 or 410 MAY be stored
4991 * by a cache (...) unless a cache-control
4992 * directive prohibits caching."
4993 *
4994 * RFC2616 @9.5: POST method :
4995 * "Responses to this method are not cacheable,
4996 * unless the response includes appropriate
4997 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004998 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004999 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005000 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005001 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5002 break;
5003 default:
5004 break;
5005 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005006
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005007 /*
5008 * 3: we may need to capture headers
5009 */
5010 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005011 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005012 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005013 txn->rsp.cap, s->fe->rsp_cap);
5014
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005015 /* 4: determine the transfer-length.
5016 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5017 * the presence of a message-body in a RESPONSE and its transfer length
5018 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005019 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005020 * All responses to the HEAD request method MUST NOT include a
5021 * message-body, even though the presence of entity-header fields
5022 * might lead one to believe they do. All 1xx (informational), 204
5023 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5024 * message-body. All other responses do include a message-body,
5025 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005026 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005027 * 1. Any response which "MUST NOT" include a message-body (such as the
5028 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5029 * always terminated by the first empty line after the header fields,
5030 * regardless of the entity-header fields present in the message.
5031 *
5032 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5033 * the "chunked" transfer-coding (Section 6.2) is used, the
5034 * transfer-length is defined by the use of this transfer-coding.
5035 * If a Transfer-Encoding header field is present and the "chunked"
5036 * transfer-coding is not present, the transfer-length is defined by
5037 * the sender closing the connection.
5038 *
5039 * 3. If a Content-Length header field is present, its decimal value in
5040 * OCTETs represents both the entity-length and the transfer-length.
5041 * If a message is received with both a Transfer-Encoding header
5042 * field and a Content-Length header field, the latter MUST be ignored.
5043 *
5044 * 4. If the message uses the media type "multipart/byteranges", and
5045 * the transfer-length is not otherwise specified, then this self-
5046 * delimiting media type defines the transfer-length. This media
5047 * type MUST NOT be used unless the sender knows that the recipient
5048 * can parse it; the presence in a request of a Range header with
5049 * multiple byte-range specifiers from a 1.1 client implies that the
5050 * client can parse multipart/byteranges responses.
5051 *
5052 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005053 */
5054
5055 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005056 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005057 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005058 * FIXME: should we parse anyway and return an error on chunked encoding ?
5059 */
5060 if (txn->meth == HTTP_METH_HEAD ||
5061 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005062 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005063 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005064 goto skip_content_length;
5065 }
5066
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005067 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005068 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005069 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005070 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005071 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005072 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5073 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005074 /* bad transfer-encoding (chunked followed by something else) */
5075 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005076 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005077 break;
5078 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005079 }
5080
5081 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5082 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005083 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005084 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005085 signed long long cl;
5086
Willy Tarreauad14f752011-09-02 20:33:27 +02005087 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005088 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005089 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005090 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005091
Willy Tarreauad14f752011-09-02 20:33:27 +02005092 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005093 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005094 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005095 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005096
Willy Tarreauad14f752011-09-02 20:33:27 +02005097 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005098 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005099 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005100 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005101
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005102 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005103 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005104 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005105 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005106
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005107 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005108 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005109 }
5110
William Lallemand82fe75c2012-10-23 10:25:10 +02005111 if (s->fe->comp || s->be->comp)
5112 select_compression_response_header(s, rep->buf);
5113
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005114 /* FIXME: we should also implement the multipart/byterange method.
5115 * For now on, we resort to close mode in this case (unknown length).
5116 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005117skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005118
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005119 /* end of job, return OK */
5120 rep->analysers &= ~an_bit;
5121 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005122 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005123 return 1;
5124}
5125
5126/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005127 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5128 * and updates t->rep->analysers. It might make sense to explode it into several
5129 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005130 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005131int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132{
5133 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134 struct http_msg *msg = &txn->rsp;
5135 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005136 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005137
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005138 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 +02005139 now_ms, __FUNCTION__,
5140 t,
5141 rep,
5142 rep->rex, rep->wex,
5143 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005144 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005145 rep->analysers);
5146
Willy Tarreau655dce92009-11-08 13:10:58 +01005147 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005148 return 0;
5149
5150 rep->analysers &= ~an_bit;
5151 rep->analyse_exp = TICK_ETERNITY;
5152
Willy Tarreau5b154472009-12-21 20:11:07 +01005153 /* Now we have to check if we need to modify the Connection header.
5154 * This is more difficult on the response than it is on the request,
5155 * because we can have two different HTTP versions and we don't know
5156 * how the client will interprete a response. For instance, let's say
5157 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5158 * HTTP/1.1 response without any header. Maybe it will bound itself to
5159 * HTTP/1.0 because it only knows about it, and will consider the lack
5160 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5161 * the lack of header as a keep-alive. Thus we will use two flags
5162 * indicating how a request MAY be understood by the client. In case
5163 * of multiple possibilities, we'll fix the header to be explicit. If
5164 * ambiguous cases such as both close and keepalive are seen, then we
5165 * will fall back to explicit close. Note that we won't take risks with
5166 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005167 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005168 */
5169
Willy Tarreaudc008c52010-02-01 16:20:08 +01005170 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5171 txn->status == 101)) {
5172 /* Either we've established an explicit tunnel, or we're
5173 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005174 * to understand the next protocols. We have to switch to tunnel
5175 * mode, so that we transfer the request and responses then let
5176 * this protocol pass unmodified. When we later implement specific
5177 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005178 * header which contains information about that protocol for
5179 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005180 */
5181 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5182 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005183 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5184 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5185 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005186 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005187
Willy Tarreau60466522010-01-18 19:08:45 +01005188 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005189 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005190 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5191 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005192
Willy Tarreau60466522010-01-18 19:08:45 +01005193 /* now adjust header transformations depending on current state */
5194 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5195 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5196 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005197 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005198 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005199 }
Willy Tarreau60466522010-01-18 19:08:45 +01005200 else { /* SCL / KAL */
5201 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005202 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005203 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005204 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005205
Willy Tarreau60466522010-01-18 19:08:45 +01005206 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005207 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005208
Willy Tarreau60466522010-01-18 19:08:45 +01005209 /* Some keep-alive responses are converted to Server-close if
5210 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005211 */
Willy Tarreau60466522010-01-18 19:08:45 +01005212 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5213 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005214 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005215 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005216 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005217 }
5218
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005219 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005220 /*
5221 * 3: we will have to evaluate the filters.
5222 * As opposed to version 1.2, now they will be evaluated in the
5223 * filters order and not in the header order. This means that
5224 * each filter has to be validated among all headers.
5225 *
5226 * Filters are tried with ->be first, then with ->fe if it is
5227 * different from ->be.
5228 */
5229
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005230 cur_proxy = t->be;
5231 while (1) {
5232 struct proxy *rule_set = cur_proxy;
5233
5234 /* try headers filters */
5235 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005236 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005237 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005238 if (target_srv(&t->target)) {
5239 target_srv(&t->target)->counters.failed_resp++;
5240 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005241 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005242 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005243 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005244 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005246 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005247 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005248 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005249 if (!(t->flags & SN_ERR_MASK))
5250 t->flags |= SN_ERR_PRXCOND;
5251 if (!(t->flags & SN_FINST_MASK))
5252 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005253 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005254 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005255 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005256
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005257 /* has the response been denied ? */
5258 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005259 if (target_srv(&t->target))
5260 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005261
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005262 t->be->be_counters.denied_resp++;
5263 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005264 if (t->listener->counters)
5265 t->listener->counters->denied_resp++;
5266
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005267 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005268 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005269
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005270 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005271 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005272 if (txn->status < 200)
5273 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005274 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005275 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005276 ret = acl_pass(ret);
5277 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5278 ret = !ret;
5279 if (!ret)
5280 continue;
5281 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005282 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005283 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005284 }
5285
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005286 /* check whether we're already working on the frontend */
5287 if (cur_proxy == t->fe)
5288 break;
5289 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005290 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005291
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005292 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005293 * We may be facing a 100-continue response, in which case this
5294 * is not the right response, and we're waiting for the next one.
5295 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005296 * next one.
5297 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005298 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005299 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005300 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005301 msg->msg_state = HTTP_MSG_RPBEFORE;
5302 txn->status = 0;
5303 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5304 return 1;
5305 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005306 else if (unlikely(txn->status < 200))
5307 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005308
5309 /* we don't have any 1xx status code now */
5310
5311 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005312 * 4: check for server cookie.
5313 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005314 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5315 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005316 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005317
Willy Tarreaubaaee002006-06-26 02:48:02 +02005318
Willy Tarreaua15645d2007-03-18 16:22:39 +01005319 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005320 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005321 */
Willy Tarreau67402132012-05-31 20:40:20 +02005322 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005323 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005324
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005325 /*
5326 * 6: add server cookie in the response if needed
5327 */
Willy Tarreau67402132012-05-31 20:40:20 +02005328 if (target_srv(&t->target) && (t->be->ck_opts & PR_CK_INS) &&
5329 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005330 (!(t->flags & SN_DIRECT) ||
5331 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5332 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5333 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5334 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005335 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005336 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005337 int len;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005338 /* the server is known, it's not the one the client requested, or the
5339 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005340 * insert a set-cookie here, except if we want to insert only on POST
5341 * requests and this one isn't. Note that servers which don't have cookies
5342 * (eg: some backup servers) will return a full cookie removal request.
5343 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005344 if (!target_srv(&t->target)->cookie) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005345 len = sprintf(trash,
5346 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5347 t->be->cookie_name);
5348 }
5349 else {
Willy Tarreau827aee92011-03-10 16:55:02 +01005350 len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005351
5352 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5353 /* emit last_date, which is mandatory */
5354 trash[len++] = COOKIE_DELIM_DATE;
5355 s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5;
5356 if (t->be->cookie_maxlife) {
5357 /* emit first_date, which is either the original one or
5358 * the current date.
5359 */
5360 trash[len++] = COOKIE_DELIM_DATE;
5361 s30tob64(txn->cookie_first_date ?
5362 txn->cookie_first_date >> 2 :
5363 (date.tv_sec+3) >> 2, trash + len);
5364 len += 5;
5365 }
5366 }
5367 len += sprintf(trash + len, "; path=/");
5368 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005369
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005370 if (t->be->cookie_domain)
5371 len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005372
Willy Tarreau4992dd22012-05-31 21:02:17 +02005373 if (t->be->ck_opts & PR_CK_HTTPONLY)
5374 len += sprintf(trash+len, "; HttpOnly");
5375
5376 if (t->be->ck_opts & PR_CK_SECURE)
5377 len += sprintf(trash+len, "; Secure");
5378
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005379 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash, len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005380 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005381
Willy Tarreauf1348312010-10-07 15:54:11 +02005382 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005383 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005384 /* the server did not change, only the date was updated */
5385 txn->flags |= TX_SCK_UPDATED;
5386 else
5387 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005388
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005389 /* Here, we will tell an eventual cache on the client side that we don't
5390 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5391 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5392 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5393 */
Willy Tarreau67402132012-05-31 20:40:20 +02005394 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005395
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005396 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5397
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005398 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005399 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005400 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005401 }
5402 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005403
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005404 /*
5405 * 7: check if result will be cacheable with a cookie.
5406 * We'll block the response if security checks have caught
5407 * nasty things such as a cacheable cookie.
5408 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005409 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5410 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005411 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005412
5413 /* we're in presence of a cacheable response containing
5414 * a set-cookie header. We'll block it as requested by
5415 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005416 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005417 if (target_srv(&t->target))
5418 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005419
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005420 t->be->be_counters.denied_resp++;
5421 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005422 if (t->listener->counters)
5423 t->listener->counters->denied_resp++;
5424
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005425 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005426 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005427 send_log(t->be, LOG_ALERT,
5428 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005429 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005430 goto return_srv_prx_502;
5431 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005432
5433 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005434 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005435 */
Willy Tarreau60466522010-01-18 19:08:45 +01005436 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5437 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5438 unsigned int want_flags = 0;
5439
5440 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5441 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5442 /* we want a keep-alive response here. Keep-alive header
5443 * required if either side is not 1.1.
5444 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005445 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005446 want_flags |= TX_CON_KAL_SET;
5447 }
5448 else {
5449 /* we want a close response here. Close header required if
5450 * the server is 1.1, regardless of the client.
5451 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005452 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005453 want_flags |= TX_CON_CLO_SET;
5454 }
5455
5456 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005457 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005458 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005459
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005460 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005461 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005462 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005463 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005464
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005465 /*************************************************************
5466 * OK, that's finished for the headers. We have done what we *
5467 * could. Let's switch to the DATA state. *
5468 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005469
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005470 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005471
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005472 /* if the user wants to log as soon as possible, without counting
5473 * bytes from the server, then this is the right moment. We have
5474 * to temporarily assign bytes_out to log what we currently have.
5475 */
5476 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5477 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5478 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005479 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005480 t->logs.bytes_out = 0;
5481 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005482
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005483 /* Note: we must not try to cheat by jumping directly to DATA,
5484 * otherwise we would not let the client side wake up.
5485 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005486
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005487 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005488 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005489 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005490}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005491
Willy Tarreaud98cf932009-12-27 22:54:55 +01005492/* This function is an analyser which forwards response body (including chunk
5493 * sizes if any). It is called as soon as we must forward, even if we forward
5494 * zero byte. The only situation where it must not be called is when we're in
5495 * tunnel mode and we want to forward till the close. It's used both to forward
5496 * remaining data and to resync after end of body. It expects the msg_state to
5497 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5498 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005499 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005500 * bytes of pending data + the headers if not already done (between sol and sov).
5501 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005502 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005503int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005504{
5505 struct http_txn *txn = &s->txn;
5506 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005507 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005508 static struct buffer *tmpbuf = NULL;
5509 int compressing = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005510
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005511 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5512 return 0;
5513
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005514 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005515 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005516 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005517 /* Output closed while we were sending data. We must abort and
5518 * wake the other side up.
5519 */
5520 msg->msg_state = HTTP_MSG_ERROR;
5521 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005522 return 1;
5523 }
5524
Willy Tarreau4fe41902010-06-07 22:27:41 +02005525 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005526 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005527
William Lallemand82fe75c2012-10-23 10:25:10 +02005528 /* no data */
5529 if (res->buf->i == 0)
5530 return 0;
5531
5532 /* this is the first time we need the compression buffer */
5533 if (s->comp_algo != NULL && tmpbuf == NULL) {
5534 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5535 goto aborted_xfer; /* no memory */
5536 }
5537
Willy Tarreaud98cf932009-12-27 22:54:55 +01005538 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005539 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005540 * rep->buf.p still points to the beginning of the message and msg->sol
5541 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005542 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005543 channel_forward(res, msg->sov);
5544 msg->next = 0;
5545 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005546
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005547 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005548 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005549 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005550 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005551 }
5552
William Lallemand82fe75c2012-10-23 10:25:10 +02005553 if (s->comp_algo != NULL) {
5554 int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
5555 if (ret < 0)
5556 goto missing_data; /* not enough spaces in buffers */
5557 compressing = 1;
5558 }
5559
Willy Tarreaud98cf932009-12-27 22:54:55 +01005560 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005561 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005562 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005563 if (s->comp_algo == NULL) {
5564 bytes = msg->sov - msg->sol;
5565 if (msg->chunk_len || bytes) {
5566 msg->sol = msg->sov;
5567 msg->next -= bytes; /* will be forwarded */
5568 msg->chunk_len += bytes;
5569 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5570 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005571 }
5572
Willy Tarreaucaabe412010-01-03 23:08:28 +01005573 if (msg->msg_state == HTTP_MSG_DATA) {
5574 /* must still forward */
William Lallemand82fe75c2012-10-23 10:25:10 +02005575 if (compressing)
5576 http_compression_buffer_add_data(s, res->buf, tmpbuf);
5577
5578 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005579 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005580
5581 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005582 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005583 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005584 else
5585 msg->msg_state = HTTP_MSG_DONE;
5586 }
5587 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005588 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005589 * set ->sov and ->next to point to the body and switch to DATA or
5590 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005591 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005592 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005593
Willy Tarreau54d23df2012-10-25 19:04:45 +02005594 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005595 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005596 else if (ret < 0) {
5597 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005598 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005599 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005600 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005601 /* skipping data if we are in compression mode */
5602 if (compressing && msg->chunk_len > 0) {
5603 b_adv(res->buf, msg->next);
5604 msg->next = 0;
5605 msg->sov = 0;
5606 msg->sol = 0;
5607 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005608 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005609 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005610 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005611 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005612 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005613
Willy Tarreau54d23df2012-10-25 19:04:45 +02005614 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005615 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005616 else if (ret < 0) {
5617 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005618 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005619 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005620 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005621 /* skipping data in buffer for compression */
5622 if (compressing) {
5623 b_adv(res->buf, msg->next);
5624 msg->next = 0;
5625 msg->sov = 0;
5626 msg->sol = 0;
5627 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005628 /* we're in MSG_CHUNK_SIZE now */
5629 }
5630 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005631 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005632
Willy Tarreaud98cf932009-12-27 22:54:55 +01005633 if (ret == 0)
5634 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005635 else if (ret < 0) {
5636 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005637 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005638 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005639 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005640 if (compressing) {
5641 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5642 compressing = 0;
5643 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005644 /* we're in HTTP_MSG_DONE now */
5645 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005646 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005647 int old_state = msg->msg_state;
5648
William Lallemand82fe75c2012-10-23 10:25:10 +02005649 if (compressing) {
5650 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5651 compressing = 0;
5652 }
5653
Willy Tarreau610ecce2010-01-04 21:15:02 +01005654 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005655 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005656 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5657 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005658 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005659 if (http_resync_states(s)) {
5660 http_silent_debug(__LINE__, s);
5661 /* some state changes occurred, maybe the analyser
5662 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005663 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005664 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005665 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005666 /* response errors are most likely due to
5667 * the client aborting the transfer.
5668 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005669 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005670 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005671 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005672 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005673 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005674 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005675 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005676 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005677 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005678 }
5679 }
5680
Willy Tarreaud98cf932009-12-27 22:54:55 +01005681 missing_data:
William Lallemand82fe75c2012-10-23 10:25:10 +02005682 if (compressing) {
5683 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5684 compressing = 0;
5685 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005686 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005687 if (res->flags & CF_SHUTR) {
Willy Tarreau40dba092010-03-04 18:14:51 +01005688 if (!(s->flags & SN_ERR_MASK))
5689 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005690 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005691 if (target_srv(&s->target))
5692 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005693 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005694 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005695
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005696 if (res->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005697 goto aborted_xfer;
5698
Willy Tarreau40dba092010-03-04 18:14:51 +01005699 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005700 if (!s->req->analysers)
5701 goto return_bad_res;
5702
Willy Tarreauea953162012-05-18 23:41:28 +02005703 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005704 if (s->comp_algo == NULL) {
5705 bytes = msg->sov - msg->sol;
5706 if (msg->chunk_len || bytes) {
5707 msg->sol = msg->sov;
5708 msg->next -= bytes; /* will be forwarded */
5709 msg->chunk_len += bytes;
5710 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5711 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005712 }
5713
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005714 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005715 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005716 * Similarly, with keep-alive on the client side, we don't want to forward a
5717 * close.
5718 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005719 if ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005720 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5721 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005722 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005723
Willy Tarreau5c620922011-05-11 19:56:11 +02005724 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005725 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005726 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005727 * modes are already handled by the stream sock layer. We must not do
5728 * this in content-length mode because it could present the MSG_MORE
5729 * flag with the last block of forwarded data, which would cause an
5730 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005731 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005732 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005733 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005734
Willy Tarreaud98cf932009-12-27 22:54:55 +01005735 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005736 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005737 return 0;
5738
Willy Tarreau40dba092010-03-04 18:14:51 +01005739 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005740 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005741 if (target_srv(&s->target))
5742 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005743
5744 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005745 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005746 /* don't send any error message as we're in the body */
5747 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005748 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005749 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005750 if (target_srv(&s->target))
5751 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005752
5753 if (!(s->flags & SN_ERR_MASK))
5754 s->flags |= SN_ERR_PRXCOND;
5755 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005756 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005757 return 0;
5758
5759 aborted_xfer:
5760 txn->rsp.msg_state = HTTP_MSG_ERROR;
5761 /* don't send any error message as we're in the body */
5762 stream_int_retnclose(res->cons, NULL);
5763 res->analysers = 0;
5764 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5765
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005766 s->fe->fe_counters.cli_aborts++;
5767 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005768 if (target_srv(&s->target))
5769 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005770
5771 if (!(s->flags & SN_ERR_MASK))
5772 s->flags |= SN_ERR_CLICL;
5773 if (!(s->flags & SN_FINST_MASK))
5774 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005775 return 0;
5776}
5777
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005778/* Iterate the same filter through all request headers.
5779 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005780 * Since it can manage the switch to another backend, it updates the per-proxy
5781 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005782 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005783int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005784{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005785 char term;
5786 char *cur_ptr, *cur_end, *cur_next;
5787 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005788 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005789 struct hdr_idx_elem *cur_hdr;
5790 int len, delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005791
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005792 last_hdr = 0;
5793
Willy Tarreau9b28e032012-10-12 23:49:43 +02005794 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005795 old_idx = 0;
5796
5797 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005798 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005799 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005800 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005801 (exp->action == ACT_ALLOW ||
5802 exp->action == ACT_DENY ||
5803 exp->action == ACT_TARPIT))
5804 return 0;
5805
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005806 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005807 if (!cur_idx)
5808 break;
5809
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005810 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005811 cur_ptr = cur_next;
5812 cur_end = cur_ptr + cur_hdr->len;
5813 cur_next = cur_end + cur_hdr->cr + 1;
5814
5815 /* Now we have one header between cur_ptr and cur_end,
5816 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005817 */
5818
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005819 /* The annoying part is that pattern matching needs
5820 * that we modify the contents to null-terminate all
5821 * strings before testing them.
5822 */
5823
5824 term = *cur_end;
5825 *cur_end = '\0';
5826
5827 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5828 switch (exp->action) {
5829 case ACT_SETBE:
5830 /* It is not possible to jump a second time.
5831 * FIXME: should we return an HTTP/500 here so that
5832 * the admin knows there's a problem ?
5833 */
5834 if (t->be != t->fe)
5835 break;
5836
5837 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005838 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005839 last_hdr = 1;
5840 break;
5841
5842 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005843 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005844 last_hdr = 1;
5845 break;
5846
5847 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005848 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005849 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005850
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005851 t->fe->fe_counters.denied_req++;
5852 if (t->fe != t->be)
5853 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005854 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005855 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005856
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005857 break;
5858
5859 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005860 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005861 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005862
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005863 t->fe->fe_counters.denied_req++;
5864 if (t->fe != t->be)
5865 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005866 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005867 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005868
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005869 break;
5870
5871 case ACT_REPLACE:
5872 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreau9b28e032012-10-12 23:49:43 +02005873 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash, len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005874 /* FIXME: if the user adds a newline in the replacement, the
5875 * index will not be recalculated for now, and the new line
5876 * will not be counted as a new header.
5877 */
5878
5879 cur_end += delta;
5880 cur_next += delta;
5881 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005882 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005883 break;
5884
5885 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02005886 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005887 cur_next += delta;
5888
Willy Tarreaufa355d42009-11-29 18:12:29 +01005889 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005890 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5891 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005892 cur_hdr->len = 0;
5893 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005894 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005895 break;
5896
5897 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005898 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005899 if (cur_end)
5900 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005901
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005902 /* keep the link from this header to next one in case of later
5903 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005904 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005905 old_idx = cur_idx;
5906 }
5907 return 0;
5908}
5909
5910
5911/* Apply the filter to the request line.
5912 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5913 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005914 * Since it can manage the switch to another backend, it updates the per-proxy
5915 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005916 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005917int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005918{
5919 char term;
5920 char *cur_ptr, *cur_end;
5921 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005922 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005923 int len, delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005924
Willy Tarreau58f10d72006-12-04 02:26:12 +01005925
Willy Tarreau3d300592007-03-18 18:34:41 +01005926 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005927 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005928 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005929 (exp->action == ACT_ALLOW ||
5930 exp->action == ACT_DENY ||
5931 exp->action == ACT_TARPIT))
5932 return 0;
5933 else if (exp->action == ACT_REMOVE)
5934 return 0;
5935
5936 done = 0;
5937
Willy Tarreau9b28e032012-10-12 23:49:43 +02005938 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005939 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005940
5941 /* Now we have the request line between cur_ptr and cur_end */
5942
5943 /* The annoying part is that pattern matching needs
5944 * that we modify the contents to null-terminate all
5945 * strings before testing them.
5946 */
5947
5948 term = *cur_end;
5949 *cur_end = '\0';
5950
5951 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5952 switch (exp->action) {
5953 case ACT_SETBE:
5954 /* It is not possible to jump a second time.
5955 * FIXME: should we return an HTTP/500 here so that
5956 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005957 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005958 if (t->be != t->fe)
5959 break;
5960
5961 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005962 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005963 done = 1;
5964 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005965
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005966 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005967 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968 done = 1;
5969 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005970
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005971 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005972 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005973
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005974 t->fe->fe_counters.denied_req++;
5975 if (t->fe != t->be)
5976 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005977 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005978 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005979
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005980 done = 1;
5981 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005982
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005983 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005984 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005985
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005986 t->fe->fe_counters.denied_req++;
5987 if (t->fe != t->be)
5988 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005989 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005990 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005991
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005992 done = 1;
5993 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005994
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005995 case ACT_REPLACE:
5996 *cur_end = term; /* restore the string terminator */
5997 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreau9b28e032012-10-12 23:49:43 +02005998 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash, len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005999 /* FIXME: if the user adds a newline in the replacement, the
6000 * index will not be recalculated for now, and the new line
6001 * will not be counted as a new header.
6002 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006003
Willy Tarreaufa355d42009-11-29 18:12:29 +01006004 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006005 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006006 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006007 HTTP_MSG_RQMETH,
6008 cur_ptr, cur_end + 1,
6009 NULL, NULL);
6010 if (unlikely(!cur_end))
6011 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006012
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006013 /* we have a full request and we know that we have either a CR
6014 * or an LF at <ptr>.
6015 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006016 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6017 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006018 /* there is no point trying this regex on headers */
6019 return 1;
6020 }
6021 }
6022 *cur_end = term; /* restore the string terminator */
6023 return done;
6024}
Willy Tarreau97de6242006-12-27 17:18:38 +01006025
Willy Tarreau58f10d72006-12-04 02:26:12 +01006026
Willy Tarreau58f10d72006-12-04 02:26:12 +01006027
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006028/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006029 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006030 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006031 * unparsable request. Since it can manage the switch to another backend, it
6032 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006033 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006034int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006035{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006036 struct http_txn *txn = &s->txn;
6037 struct hdr_exp *exp;
6038
6039 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006040 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006041
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006042 /*
6043 * The interleaving of transformations and verdicts
6044 * makes it difficult to decide to continue or stop
6045 * the evaluation.
6046 */
6047
Willy Tarreau6c123b12010-01-28 20:22:06 +01006048 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6049 break;
6050
Willy Tarreau3d300592007-03-18 18:34:41 +01006051 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006052 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006053 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006054 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006055
6056 /* if this filter had a condition, evaluate it now and skip to
6057 * next filter if the condition does not match.
6058 */
6059 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006060 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006061 ret = acl_pass(ret);
6062 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6063 ret = !ret;
6064
6065 if (!ret)
6066 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006067 }
6068
6069 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006070 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006071 if (unlikely(ret < 0))
6072 return -1;
6073
6074 if (likely(ret == 0)) {
6075 /* The filter did not match the request, it can be
6076 * iterated through all headers.
6077 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006078 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006079 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006080 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006081 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006082}
6083
6084
Willy Tarreaua15645d2007-03-18 16:22:39 +01006085
Willy Tarreau58f10d72006-12-04 02:26:12 +01006086/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006087 * Try to retrieve the server associated to the appsession.
6088 * If the server is found, it's assigned to the session.
6089 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006090void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006091 struct http_txn *txn = &t->txn;
6092 appsess *asession = NULL;
6093 char *sessid_temp = NULL;
6094
Cyril Bontéb21570a2009-11-29 20:04:48 +01006095 if (len > t->be->appsession_len) {
6096 len = t->be->appsession_len;
6097 }
6098
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006099 if (t->be->options2 & PR_O2_AS_REQL) {
6100 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006101 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006102 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006103 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006104 }
6105
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006106 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006107 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6108 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6109 return;
6110 }
6111
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006112 memcpy(txn->sessid, buf, len);
6113 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006114 }
6115
6116 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6117 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6118 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6119 return;
6120 }
6121
Cyril Bontéb21570a2009-11-29 20:04:48 +01006122 memcpy(sessid_temp, buf, len);
6123 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006124
6125 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6126 /* free previously allocated memory */
6127 pool_free2(apools.sessid, sessid_temp);
6128
6129 if (asession != NULL) {
6130 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6131 if (!(t->be->options2 & PR_O2_AS_REQL))
6132 asession->request_count++;
6133
6134 if (asession->serverid != NULL) {
6135 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006136
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006137 while (srv) {
6138 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006139 if ((srv->state & SRV_RUNNING) ||
6140 (t->be->options & PR_O_PERSIST) ||
6141 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006142 /* we found the server and it's usable */
6143 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006144 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006145 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006146 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006147
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006148 break;
6149 } else {
6150 txn->flags &= ~TX_CK_MASK;
6151 txn->flags |= TX_CK_DOWN;
6152 }
6153 }
6154 srv = srv->next;
6155 }
6156 }
6157 }
6158}
6159
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006160/* Find the end of a cookie value contained between <s> and <e>. It works the
6161 * same way as with headers above except that the semi-colon also ends a token.
6162 * See RFC2965 for more information. Note that it requires a valid header to
6163 * return a valid result.
6164 */
6165char *find_cookie_value_end(char *s, const char *e)
6166{
6167 int quoted, qdpair;
6168
6169 quoted = qdpair = 0;
6170 for (; s < e; s++) {
6171 if (qdpair) qdpair = 0;
6172 else if (quoted) {
6173 if (*s == '\\') qdpair = 1;
6174 else if (*s == '"') quoted = 0;
6175 }
6176 else if (*s == '"') quoted = 1;
6177 else if (*s == ',' || *s == ';') return s;
6178 }
6179 return s;
6180}
6181
6182/* Delete a value in a header between delimiters <from> and <next> in buffer
6183 * <buf>. The number of characters displaced is returned, and the pointer to
6184 * the first delimiter is updated if required. The function tries as much as
6185 * possible to respect the following principles :
6186 * - replace <from> delimiter by the <next> one unless <from> points to a
6187 * colon, in which case <next> is simply removed
6188 * - set exactly one space character after the new first delimiter, unless
6189 * there are not enough characters in the block being moved to do so.
6190 * - remove unneeded spaces before the previous delimiter and after the new
6191 * one.
6192 *
6193 * It is the caller's responsibility to ensure that :
6194 * - <from> points to a valid delimiter or the colon ;
6195 * - <next> points to a valid delimiter or the final CR/LF ;
6196 * - there are non-space chars before <from> ;
6197 * - there is a CR/LF at or after <next>.
6198 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006199int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006200{
6201 char *prev = *from;
6202
6203 if (*prev == ':') {
6204 /* We're removing the first value, preserve the colon and add a
6205 * space if possible.
6206 */
6207 if (!http_is_crlf[(unsigned char)*next])
6208 next++;
6209 prev++;
6210 if (prev < next)
6211 *prev++ = ' ';
6212
6213 while (http_is_spht[(unsigned char)*next])
6214 next++;
6215 } else {
6216 /* Remove useless spaces before the old delimiter. */
6217 while (http_is_spht[(unsigned char)*(prev-1)])
6218 prev--;
6219 *from = prev;
6220
6221 /* copy the delimiter and if possible a space if we're
6222 * not at the end of the line.
6223 */
6224 if (!http_is_crlf[(unsigned char)*next]) {
6225 *prev++ = *next++;
6226 if (prev + 1 < next)
6227 *prev++ = ' ';
6228 while (http_is_spht[(unsigned char)*next])
6229 next++;
6230 }
6231 }
6232 return buffer_replace2(buf, prev, next, NULL, 0);
6233}
6234
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006235/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006236 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006237 * desirable to call it only when needed. This code is quite complex because
6238 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6239 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006240 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006241void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006242{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006243 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006244 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006245 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6247 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006248
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006249 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006250 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006251 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006252
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006253 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006255 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006257 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006258 hdr_beg = hdr_next;
6259 hdr_end = hdr_beg + cur_hdr->len;
6260 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006261
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006262 /* We have one full header between hdr_beg and hdr_end, and the
6263 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006264 * "Cookie:" headers.
6265 */
6266
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006267 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006268 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269 old_idx = cur_idx;
6270 continue;
6271 }
6272
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006273 del_from = NULL; /* nothing to be deleted */
6274 preserve_hdr = 0; /* assume we may kill the whole header */
6275
Willy Tarreau58f10d72006-12-04 02:26:12 +01006276 /* Now look for cookies. Conforming to RFC2109, we have to support
6277 * attributes whose name begin with a '$', and associate them with
6278 * the right cookie, if we want to delete this cookie.
6279 * So there are 3 cases for each cookie read :
6280 * 1) it's a special attribute, beginning with a '$' : ignore it.
6281 * 2) it's a server id cookie that we *MAY* want to delete : save
6282 * some pointers on it (last semi-colon, beginning of cookie...)
6283 * 3) it's an application cookie : we *MAY* have to delete a previous
6284 * "special" cookie.
6285 * At the end of loop, if a "special" cookie remains, we may have to
6286 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006287 * *MUST* delete it.
6288 *
6289 * Note: RFC2965 is unclear about the processing of spaces around
6290 * the equal sign in the ATTR=VALUE form. A careful inspection of
6291 * the RFC explicitly allows spaces before it, and not within the
6292 * tokens (attrs or values). An inspection of RFC2109 allows that
6293 * too but section 10.1.3 lets one think that spaces may be allowed
6294 * after the equal sign too, resulting in some (rare) buggy
6295 * implementations trying to do that. So let's do what servers do.
6296 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6297 * allowed quoted strings in values, with any possible character
6298 * after a backslash, including control chars and delimitors, which
6299 * causes parsing to become ambiguous. Browsers also allow spaces
6300 * within values even without quotes.
6301 *
6302 * We have to keep multiple pointers in order to support cookie
6303 * removal at the beginning, middle or end of header without
6304 * corrupting the header. All of these headers are valid :
6305 *
6306 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6307 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6308 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6309 * | | | | | | | | |
6310 * | | | | | | | | hdr_end <--+
6311 * | | | | | | | +--> next
6312 * | | | | | | +----> val_end
6313 * | | | | | +-----------> val_beg
6314 * | | | | +--------------> equal
6315 * | | | +----------------> att_end
6316 * | | +---------------------> att_beg
6317 * | +--------------------------> prev
6318 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006319 */
6320
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006321 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6322 /* Iterate through all cookies on this line */
6323
6324 /* find att_beg */
6325 att_beg = prev + 1;
6326 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6327 att_beg++;
6328
6329 /* find att_end : this is the first character after the last non
6330 * space before the equal. It may be equal to hdr_end.
6331 */
6332 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006333
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006334 while (equal < hdr_end) {
6335 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006336 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006337 if (http_is_spht[(unsigned char)*equal++])
6338 continue;
6339 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006340 }
6341
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006342 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6343 * is between <att_beg> and <equal>, both may be identical.
6344 */
6345
6346 /* look for end of cookie if there is an equal sign */
6347 if (equal < hdr_end && *equal == '=') {
6348 /* look for the beginning of the value */
6349 val_beg = equal + 1;
6350 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6351 val_beg++;
6352
6353 /* find the end of the value, respecting quotes */
6354 next = find_cookie_value_end(val_beg, hdr_end);
6355
6356 /* make val_end point to the first white space or delimitor after the value */
6357 val_end = next;
6358 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6359 val_end--;
6360 } else {
6361 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006362 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006363
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006364 /* We have nothing to do with attributes beginning with '$'. However,
6365 * they will automatically be removed if a header before them is removed,
6366 * since they're supposed to be linked together.
6367 */
6368 if (*att_beg == '$')
6369 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006370
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006371 /* Ignore cookies with no equal sign */
6372 if (equal == next) {
6373 /* This is not our cookie, so we must preserve it. But if we already
6374 * scheduled another cookie for removal, we cannot remove the
6375 * complete header, but we can remove the previous block itself.
6376 */
6377 preserve_hdr = 1;
6378 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006379 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006380 val_end += delta;
6381 next += delta;
6382 hdr_end += delta;
6383 hdr_next += delta;
6384 cur_hdr->len += delta;
6385 http_msg_move_end(&txn->req, delta);
6386 prev = del_from;
6387 del_from = NULL;
6388 }
6389 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006390 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006391
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006392 /* if there are spaces around the equal sign, we need to
6393 * strip them otherwise we'll get trouble for cookie captures,
6394 * or even for rewrites. Since this happens extremely rarely,
6395 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006396 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006397 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6398 int stripped_before = 0;
6399 int stripped_after = 0;
6400
6401 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006402 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006403 equal += stripped_before;
6404 val_beg += stripped_before;
6405 }
6406
6407 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006408 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006409 val_beg += stripped_after;
6410 stripped_before += stripped_after;
6411 }
6412
6413 val_end += stripped_before;
6414 next += stripped_before;
6415 hdr_end += stripped_before;
6416 hdr_next += stripped_before;
6417 cur_hdr->len += stripped_before;
6418 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006419 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006420 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006421
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006422 /* First, let's see if we want to capture this cookie. We check
6423 * that we don't already have a client side cookie, because we
6424 * can only capture one. Also as an optimisation, we ignore
6425 * cookies shorter than the declared name.
6426 */
6427 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6428 (val_end - att_beg >= t->fe->capture_namelen) &&
6429 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6430 int log_len = val_end - att_beg;
6431
6432 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6433 Alert("HTTP logging : out of memory.\n");
6434 } else {
6435 if (log_len > t->fe->capture_len)
6436 log_len = t->fe->capture_len;
6437 memcpy(txn->cli_cookie, att_beg, log_len);
6438 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006439 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006440 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006441
Willy Tarreaubca99692010-10-06 19:25:55 +02006442 /* Persistence cookies in passive, rewrite or insert mode have the
6443 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006444 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006445 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006446 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006447 * For cookies in prefix mode, the form is :
6448 *
6449 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006450 */
6451 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6452 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6453 struct server *srv = t->be->srv;
6454 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006455
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006456 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6457 * have the server ID between val_beg and delim, and the original cookie between
6458 * delim+1 and val_end. Otherwise, delim==val_end :
6459 *
6460 * Cookie: NAME=SRV; # in all but prefix modes
6461 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6462 * | || || | |+-> next
6463 * | || || | +--> val_end
6464 * | || || +---------> delim
6465 * | || |+------------> val_beg
6466 * | || +-------------> att_end = equal
6467 * | |+-----------------> att_beg
6468 * | +------------------> prev
6469 * +-------------------------> hdr_beg
6470 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006471
Willy Tarreau67402132012-05-31 20:40:20 +02006472 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006473 for (delim = val_beg; delim < val_end; delim++)
6474 if (*delim == COOKIE_DELIM)
6475 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006476 } else {
6477 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006478 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006479 /* Now check if the cookie contains a date field, which would
6480 * appear after a vertical bar ('|') just after the server name
6481 * and before the delimiter.
6482 */
6483 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6484 if (vbar1) {
6485 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006486 * right is the last seen date. It is a base64 encoded
6487 * 30-bit value representing the UNIX date since the
6488 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006489 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006490 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006491 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006492 if (val_end - vbar1 >= 5) {
6493 val = b64tos30(vbar1);
6494 if (val > 0)
6495 txn->cookie_last_date = val << 2;
6496 }
6497 /* look for a second vertical bar */
6498 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6499 if (vbar1 && (val_end - vbar1 > 5)) {
6500 val = b64tos30(vbar1 + 1);
6501 if (val > 0)
6502 txn->cookie_first_date = val << 2;
6503 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006504 }
6505 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006506
Willy Tarreauf64d1412010-10-07 20:06:11 +02006507 /* if the cookie has an expiration date and the proxy wants to check
6508 * it, then we do that now. We first check if the cookie is too old,
6509 * then only if it has expired. We detect strict overflow because the
6510 * time resolution here is not great (4 seconds). Cookies with dates
6511 * in the future are ignored if their offset is beyond one day. This
6512 * allows an admin to fix timezone issues without expiring everyone
6513 * and at the same time avoids keeping unwanted side effects for too
6514 * long.
6515 */
6516 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006517 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6518 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006519 txn->flags &= ~TX_CK_MASK;
6520 txn->flags |= TX_CK_OLD;
6521 delim = val_beg; // let's pretend we have not found the cookie
6522 txn->cookie_first_date = 0;
6523 txn->cookie_last_date = 0;
6524 }
6525 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006526 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6527 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006528 txn->flags &= ~TX_CK_MASK;
6529 txn->flags |= TX_CK_EXPIRED;
6530 delim = val_beg; // let's pretend we have not found the cookie
6531 txn->cookie_first_date = 0;
6532 txn->cookie_last_date = 0;
6533 }
6534
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006535 /* Here, we'll look for the first running server which supports the cookie.
6536 * This allows to share a same cookie between several servers, for example
6537 * to dedicate backup servers to specific servers only.
6538 * However, to prevent clients from sticking to cookie-less backup server
6539 * when they have incidentely learned an empty cookie, we simply ignore
6540 * empty cookies and mark them as invalid.
6541 * The same behaviour is applied when persistence must be ignored.
6542 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006543 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006544 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006545
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006546 while (srv) {
6547 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6548 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6549 if ((srv->state & SRV_RUNNING) ||
6550 (t->be->options & PR_O_PERSIST) ||
6551 (t->flags & SN_FORCE_PRST)) {
6552 /* we found the server and we can use it */
6553 txn->flags &= ~TX_CK_MASK;
6554 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6555 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006556 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006557 break;
6558 } else {
6559 /* we found a server, but it's down,
6560 * mark it as such and go on in case
6561 * another one is available.
6562 */
6563 txn->flags &= ~TX_CK_MASK;
6564 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006565 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006566 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006567 srv = srv->next;
6568 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006569
Willy Tarreauf64d1412010-10-07 20:06:11 +02006570 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006571 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006572 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006573 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6574 txn->flags |= TX_CK_UNUSED;
6575 else
6576 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006577 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006578
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006579 /* depending on the cookie mode, we may have to either :
6580 * - delete the complete cookie if we're in insert+indirect mode, so that
6581 * the server never sees it ;
6582 * - remove the server id from the cookie value, and tag the cookie as an
6583 * application cookie so that it does not get accidentely removed later,
6584 * if we're in cookie prefix mode
6585 */
Willy Tarreau67402132012-05-31 20:40:20 +02006586 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006587 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588
Willy Tarreau9b28e032012-10-12 23:49:43 +02006589 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006590 val_end += delta;
6591 next += delta;
6592 hdr_end += delta;
6593 hdr_next += delta;
6594 cur_hdr->len += delta;
6595 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006596
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006597 del_from = NULL;
6598 preserve_hdr = 1; /* we want to keep this cookie */
6599 }
6600 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006601 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 del_from = prev;
6603 }
6604 } else {
6605 /* This is not our cookie, so we must preserve it. But if we already
6606 * scheduled another cookie for removal, we cannot remove the
6607 * complete header, but we can remove the previous block itself.
6608 */
6609 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006610
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006611 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006612 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006613 if (att_beg >= del_from)
6614 att_beg += delta;
6615 if (att_end >= del_from)
6616 att_end += delta;
6617 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006618 val_end += delta;
6619 next += delta;
6620 hdr_end += delta;
6621 hdr_next += delta;
6622 cur_hdr->len += delta;
6623 http_msg_move_end(&txn->req, delta);
6624 prev = del_from;
6625 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006626 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006627 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006628
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006629 /* Look for the appsession cookie unless persistence must be ignored */
6630 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6631 int cmp_len, value_len;
6632 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006633
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006634 if (t->be->options2 & PR_O2_AS_PFX) {
6635 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6636 value_begin = att_beg + t->be->appsession_name_len;
6637 value_len = val_end - att_beg - t->be->appsession_name_len;
6638 } else {
6639 cmp_len = att_end - att_beg;
6640 value_begin = val_beg;
6641 value_len = val_end - val_beg;
6642 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006643
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006644 /* let's see if the cookie is our appcookie */
6645 if (cmp_len == t->be->appsession_name_len &&
6646 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6647 manage_client_side_appsession(t, value_begin, value_len);
6648 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006649 }
6650
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006651 /* continue with next cookie on this header line */
6652 att_beg = next;
6653 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006654
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006655 /* There are no more cookies on this line.
6656 * We may still have one (or several) marked for deletion at the
6657 * end of the line. We must do this now in two ways :
6658 * - if some cookies must be preserved, we only delete from the
6659 * mark to the end of line ;
6660 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006661 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006662 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006663 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006664 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006665 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006667 cur_hdr->len += delta;
6668 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006669 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006670
6671 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006672 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6673 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006674 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006675 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006676 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006677 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006678 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006679 }
6680
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006681 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006682 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006684}
6685
6686
Willy Tarreaua15645d2007-03-18 16:22:39 +01006687/* Iterate the same filter through all response headers contained in <rtr>.
6688 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6689 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006690int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006691{
6692 char term;
6693 char *cur_ptr, *cur_end, *cur_next;
6694 int cur_idx, old_idx, last_hdr;
6695 struct http_txn *txn = &t->txn;
6696 struct hdr_idx_elem *cur_hdr;
6697 int len, delta;
6698
6699 last_hdr = 0;
6700
Willy Tarreau9b28e032012-10-12 23:49:43 +02006701 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006702 old_idx = 0;
6703
6704 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006705 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006706 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006707 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 (exp->action == ACT_ALLOW ||
6709 exp->action == ACT_DENY))
6710 return 0;
6711
6712 cur_idx = txn->hdr_idx.v[old_idx].next;
6713 if (!cur_idx)
6714 break;
6715
6716 cur_hdr = &txn->hdr_idx.v[cur_idx];
6717 cur_ptr = cur_next;
6718 cur_end = cur_ptr + cur_hdr->len;
6719 cur_next = cur_end + cur_hdr->cr + 1;
6720
6721 /* Now we have one header between cur_ptr and cur_end,
6722 * and the next header starts at cur_next.
6723 */
6724
6725 /* The annoying part is that pattern matching needs
6726 * that we modify the contents to null-terminate all
6727 * strings before testing them.
6728 */
6729
6730 term = *cur_end;
6731 *cur_end = '\0';
6732
6733 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6734 switch (exp->action) {
6735 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006736 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006737 last_hdr = 1;
6738 break;
6739
6740 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006741 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006742 last_hdr = 1;
6743 break;
6744
6745 case ACT_REPLACE:
6746 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreau9b28e032012-10-12 23:49:43 +02006747 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash, len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006748 /* FIXME: if the user adds a newline in the replacement, the
6749 * index will not be recalculated for now, and the new line
6750 * will not be counted as a new header.
6751 */
6752
6753 cur_end += delta;
6754 cur_next += delta;
6755 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006756 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006757 break;
6758
6759 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006760 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006761 cur_next += delta;
6762
Willy Tarreaufa355d42009-11-29 18:12:29 +01006763 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006764 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6765 txn->hdr_idx.used--;
6766 cur_hdr->len = 0;
6767 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006768 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006769 break;
6770
6771 }
6772 }
6773 if (cur_end)
6774 *cur_end = term; /* restore the string terminator */
6775
6776 /* keep the link from this header to next one in case of later
6777 * removal of next header.
6778 */
6779 old_idx = cur_idx;
6780 }
6781 return 0;
6782}
6783
6784
6785/* Apply the filter to the status line in the response buffer <rtr>.
6786 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6787 * or -1 if a replacement resulted in an invalid status line.
6788 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006789int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006790{
6791 char term;
6792 char *cur_ptr, *cur_end;
6793 int done;
6794 struct http_txn *txn = &t->txn;
6795 int len, delta;
6796
6797
Willy Tarreau3d300592007-03-18 18:34:41 +01006798 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006799 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006800 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801 (exp->action == ACT_ALLOW ||
6802 exp->action == ACT_DENY))
6803 return 0;
6804 else if (exp->action == ACT_REMOVE)
6805 return 0;
6806
6807 done = 0;
6808
Willy Tarreau9b28e032012-10-12 23:49:43 +02006809 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006810 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811
6812 /* Now we have the status line between cur_ptr and cur_end */
6813
6814 /* The annoying part is that pattern matching needs
6815 * that we modify the contents to null-terminate all
6816 * strings before testing them.
6817 */
6818
6819 term = *cur_end;
6820 *cur_end = '\0';
6821
6822 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6823 switch (exp->action) {
6824 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006825 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006826 done = 1;
6827 break;
6828
6829 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006830 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006831 done = 1;
6832 break;
6833
6834 case ACT_REPLACE:
6835 *cur_end = term; /* restore the string terminator */
6836 len = exp_replace(trash, cur_ptr, exp->replace, pmatch);
Willy Tarreau9b28e032012-10-12 23:49:43 +02006837 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash, len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006838 /* FIXME: if the user adds a newline in the replacement, the
6839 * index will not be recalculated for now, and the new line
6840 * will not be counted as a new header.
6841 */
6842
Willy Tarreaufa355d42009-11-29 18:12:29 +01006843 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006844 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006845 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006846 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006847 cur_ptr, cur_end + 1,
6848 NULL, NULL);
6849 if (unlikely(!cur_end))
6850 return -1;
6851
6852 /* we have a full respnse and we know that we have either a CR
6853 * or an LF at <ptr>.
6854 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006855 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006856 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006857 /* there is no point trying this regex on headers */
6858 return 1;
6859 }
6860 }
6861 *cur_end = term; /* restore the string terminator */
6862 return done;
6863}
6864
6865
6866
6867/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006868 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006869 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6870 * unparsable response.
6871 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006872int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006873{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006874 struct http_txn *txn = &s->txn;
6875 struct hdr_exp *exp;
6876
6877 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006878 int ret;
6879
6880 /*
6881 * The interleaving of transformations and verdicts
6882 * makes it difficult to decide to continue or stop
6883 * the evaluation.
6884 */
6885
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006886 if (txn->flags & TX_SVDENY)
6887 break;
6888
Willy Tarreau3d300592007-03-18 18:34:41 +01006889 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006890 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6891 exp->action == ACT_PASS)) {
6892 exp = exp->next;
6893 continue;
6894 }
6895
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006896 /* if this filter had a condition, evaluate it now and skip to
6897 * next filter if the condition does not match.
6898 */
6899 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006900 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006901 ret = acl_pass(ret);
6902 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6903 ret = !ret;
6904 if (!ret)
6905 continue;
6906 }
6907
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006909 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006910 if (unlikely(ret < 0))
6911 return -1;
6912
6913 if (likely(ret == 0)) {
6914 /* The filter did not match the response, it can be
6915 * iterated through all headers.
6916 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006917 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006919 }
6920 return 0;
6921}
6922
6923
Willy Tarreaua15645d2007-03-18 16:22:39 +01006924/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006925 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006926 * desirable to call it only when needed. This function is also used when we
6927 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006928 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006929void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006930{
6931 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006932 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006933 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006934 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006935 char *hdr_beg, *hdr_end, *hdr_next;
6936 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006937
Willy Tarreaua15645d2007-03-18 16:22:39 +01006938 /* Iterate through the headers.
6939 * we start with the start line.
6940 */
6941 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006942 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006943
6944 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6945 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006946 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006947
6948 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006949 hdr_beg = hdr_next;
6950 hdr_end = hdr_beg + cur_hdr->len;
6951 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006952
Willy Tarreau24581ba2010-08-31 22:39:35 +02006953 /* We have one full header between hdr_beg and hdr_end, and the
6954 * next header starts at hdr_next. We're only interested in
6955 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006956 */
6957
Willy Tarreau24581ba2010-08-31 22:39:35 +02006958 is_cookie2 = 0;
6959 prev = hdr_beg + 10;
6960 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006961 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006962 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6963 if (!val) {
6964 old_idx = cur_idx;
6965 continue;
6966 }
6967 is_cookie2 = 1;
6968 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006969 }
6970
Willy Tarreau24581ba2010-08-31 22:39:35 +02006971 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6972 * <prev> points to the colon.
6973 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006974 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006975
Willy Tarreau24581ba2010-08-31 22:39:35 +02006976 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6977 * check-cache is enabled) and we are not interested in checking
6978 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006979 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006980 if (t->be->cookie_name == NULL &&
6981 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006982 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006983 return;
6984
Willy Tarreau24581ba2010-08-31 22:39:35 +02006985 /* OK so now we know we have to process this response cookie.
6986 * The format of the Set-Cookie header is slightly different
6987 * from the format of the Cookie header in that it does not
6988 * support the comma as a cookie delimiter (thus the header
6989 * cannot be folded) because the Expires attribute described in
6990 * the original Netscape's spec may contain an unquoted date
6991 * with a comma inside. We have to live with this because
6992 * many browsers don't support Max-Age and some browsers don't
6993 * support quoted strings. However the Set-Cookie2 header is
6994 * clean.
6995 *
6996 * We have to keep multiple pointers in order to support cookie
6997 * removal at the beginning, middle or end of header without
6998 * corrupting the header (in case of set-cookie2). A special
6999 * pointer, <scav> points to the beginning of the set-cookie-av
7000 * fields after the first semi-colon. The <next> pointer points
7001 * either to the end of line (set-cookie) or next unquoted comma
7002 * (set-cookie2). All of these headers are valid :
7003 *
7004 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7005 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7006 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7007 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7008 * | | | | | | | | | |
7009 * | | | | | | | | +-> next hdr_end <--+
7010 * | | | | | | | +------------> scav
7011 * | | | | | | +--------------> val_end
7012 * | | | | | +--------------------> val_beg
7013 * | | | | +----------------------> equal
7014 * | | | +------------------------> att_end
7015 * | | +----------------------------> att_beg
7016 * | +------------------------------> prev
7017 * +-----------------------------------------> hdr_beg
7018 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007019
Willy Tarreau24581ba2010-08-31 22:39:35 +02007020 for (; prev < hdr_end; prev = next) {
7021 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007022
Willy Tarreau24581ba2010-08-31 22:39:35 +02007023 /* find att_beg */
7024 att_beg = prev + 1;
7025 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7026 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007027
Willy Tarreau24581ba2010-08-31 22:39:35 +02007028 /* find att_end : this is the first character after the last non
7029 * space before the equal. It may be equal to hdr_end.
7030 */
7031 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007032
Willy Tarreau24581ba2010-08-31 22:39:35 +02007033 while (equal < hdr_end) {
7034 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7035 break;
7036 if (http_is_spht[(unsigned char)*equal++])
7037 continue;
7038 att_end = equal;
7039 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007040
Willy Tarreau24581ba2010-08-31 22:39:35 +02007041 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7042 * is between <att_beg> and <equal>, both may be identical.
7043 */
7044
7045 /* look for end of cookie if there is an equal sign */
7046 if (equal < hdr_end && *equal == '=') {
7047 /* look for the beginning of the value */
7048 val_beg = equal + 1;
7049 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7050 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007051
Willy Tarreau24581ba2010-08-31 22:39:35 +02007052 /* find the end of the value, respecting quotes */
7053 next = find_cookie_value_end(val_beg, hdr_end);
7054
7055 /* make val_end point to the first white space or delimitor after the value */
7056 val_end = next;
7057 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7058 val_end--;
7059 } else {
7060 /* <equal> points to next comma, semi-colon or EOL */
7061 val_beg = val_end = next = equal;
7062 }
7063
7064 if (next < hdr_end) {
7065 /* Set-Cookie2 supports multiple cookies, and <next> points to
7066 * a colon or semi-colon before the end. So skip all attr-value
7067 * pairs and look for the next comma. For Set-Cookie, since
7068 * commas are permitted in values, skip to the end.
7069 */
7070 if (is_cookie2)
7071 next = find_hdr_value_end(next, hdr_end);
7072 else
7073 next = hdr_end;
7074 }
7075
7076 /* Now everything is as on the diagram above */
7077
7078 /* Ignore cookies with no equal sign */
7079 if (equal == val_end)
7080 continue;
7081
7082 /* If there are spaces around the equal sign, we need to
7083 * strip them otherwise we'll get trouble for cookie captures,
7084 * or even for rewrites. Since this happens extremely rarely,
7085 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007086 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007087 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7088 int stripped_before = 0;
7089 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007090
Willy Tarreau24581ba2010-08-31 22:39:35 +02007091 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007092 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007093 equal += stripped_before;
7094 val_beg += stripped_before;
7095 }
7096
7097 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007098 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007099 val_beg += stripped_after;
7100 stripped_before += stripped_after;
7101 }
7102
7103 val_end += stripped_before;
7104 next += stripped_before;
7105 hdr_end += stripped_before;
7106 hdr_next += stripped_before;
7107 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007108 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007109 }
7110
7111 /* First, let's see if we want to capture this cookie. We check
7112 * that we don't already have a server side cookie, because we
7113 * can only capture one. Also as an optimisation, we ignore
7114 * cookies shorter than the declared name.
7115 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007116 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007117 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007118 (val_end - att_beg >= t->fe->capture_namelen) &&
7119 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7120 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007121 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007122 Alert("HTTP logging : out of memory.\n");
7123 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007124 else {
7125 if (log_len > t->fe->capture_len)
7126 log_len = t->fe->capture_len;
7127 memcpy(txn->srv_cookie, att_beg, log_len);
7128 txn->srv_cookie[log_len] = 0;
7129 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007130 }
7131
Willy Tarreau827aee92011-03-10 16:55:02 +01007132 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007133 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007134 if (!(t->flags & SN_IGNORE_PRST) &&
7135 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7136 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007137 /* assume passive cookie by default */
7138 txn->flags &= ~TX_SCK_MASK;
7139 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140
7141 /* If the cookie is in insert mode on a known server, we'll delete
7142 * this occurrence because we'll insert another one later.
7143 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007144 * a direct access.
7145 */
Willy Tarreau67402132012-05-31 20:40:20 +02007146 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007147 /* The "preserve" flag was set, we don't want to touch the
7148 * server's cookie.
7149 */
7150 }
Willy Tarreau67402132012-05-31 20:40:20 +02007151 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7152 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007153 /* this cookie must be deleted */
7154 if (*prev == ':' && next == hdr_end) {
7155 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007156 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007157 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7158 txn->hdr_idx.used--;
7159 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007160 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007161 hdr_next += delta;
7162 http_msg_move_end(&txn->rsp, delta);
7163 /* note: while both invalid now, <next> and <hdr_end>
7164 * are still equal, so the for() will stop as expected.
7165 */
7166 } else {
7167 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007168 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007169 next = prev;
7170 hdr_end += delta;
7171 hdr_next += delta;
7172 cur_hdr->len += delta;
7173 http_msg_move_end(&txn->rsp, delta);
7174 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007175 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007176 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007177 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007178 }
Willy Tarreau67402132012-05-31 20:40:20 +02007179 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007180 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007181 * with this server since we know it.
7182 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007183 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007184 next += delta;
7185 hdr_end += delta;
7186 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007187 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007188 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189
Willy Tarreauf1348312010-10-07 15:54:11 +02007190 txn->flags &= ~TX_SCK_MASK;
7191 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007192 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007193 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007194 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007195 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007197 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007198 next += delta;
7199 hdr_end += delta;
7200 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007201 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007202 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203
Willy Tarreau827aee92011-03-10 16:55:02 +01007204 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007205 txn->flags &= ~TX_SCK_MASK;
7206 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007207 }
7208 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007209 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7210 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007211 int cmp_len, value_len;
7212 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007213
Cyril Bontéb21570a2009-11-29 20:04:48 +01007214 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007215 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7216 value_begin = att_beg + t->be->appsession_name_len;
7217 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007218 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007219 cmp_len = att_end - att_beg;
7220 value_begin = val_beg;
7221 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007222 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007223
Cyril Bonté17530c32010-04-06 21:11:10 +02007224 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007225 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7226 /* free a possibly previously allocated memory */
7227 pool_free2(apools.sessid, txn->sessid);
7228
Cyril Bontéb21570a2009-11-29 20:04:48 +01007229 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007230 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007231 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7232 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7233 return;
7234 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007235 memcpy(txn->sessid, value_begin, value_len);
7236 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007238 }
7239 /* that's done for this cookie, check the next one on the same
7240 * line when next != hdr_end (only if is_cookie2).
7241 */
7242 }
7243 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007244 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007245 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007246
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007247 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007248 appsess *asession = NULL;
7249 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007250 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007251 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007252 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007253 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7254 Alert("Not enough Memory process_srv():asession:calloc().\n");
7255 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7256 return;
7257 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007258 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7259
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007260 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7261 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7262 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007263 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007264 return;
7265 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007266 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007267 asession->sessid[t->be->appsession_len] = 0;
7268
Willy Tarreau827aee92011-03-10 16:55:02 +01007269 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007270 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007271 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007272 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007273 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007274 return;
7275 }
7276 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007277 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007278
7279 asession->request_count = 0;
7280 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7281 }
7282
7283 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7284 asession->request_count++;
7285 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007286}
7287
7288
Willy Tarreaua15645d2007-03-18 16:22:39 +01007289/*
7290 * Check if response is cacheable or not. Updates t->flags.
7291 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007292void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007293{
7294 struct http_txn *txn = &t->txn;
7295 char *p1, *p2;
7296
7297 char *cur_ptr, *cur_end, *cur_next;
7298 int cur_idx;
7299
Willy Tarreau5df51872007-11-25 16:20:08 +01007300 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007301 return;
7302
7303 /* Iterate through the headers.
7304 * we start with the start line.
7305 */
7306 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007307 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007308
7309 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7310 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007311 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007312
7313 cur_hdr = &txn->hdr_idx.v[cur_idx];
7314 cur_ptr = cur_next;
7315 cur_end = cur_ptr + cur_hdr->len;
7316 cur_next = cur_end + cur_hdr->cr + 1;
7317
7318 /* We have one full header between cur_ptr and cur_end, and the
7319 * next header starts at cur_next. We're only interested in
7320 * "Cookie:" headers.
7321 */
7322
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007323 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7324 if (val) {
7325 if ((cur_end - (cur_ptr + val) >= 8) &&
7326 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7327 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7328 return;
7329 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007330 }
7331
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007332 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7333 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334 continue;
7335
7336 /* OK, right now we know we have a cache-control header at cur_ptr */
7337
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007338 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339
7340 if (p1 >= cur_end) /* no more info */
7341 continue;
7342
7343 /* p1 is at the beginning of the value */
7344 p2 = p1;
7345
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007346 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007347 p2++;
7348
7349 /* we have a complete value between p1 and p2 */
7350 if (p2 < cur_end && *p2 == '=') {
7351 /* we have something of the form no-cache="set-cookie" */
7352 if ((cur_end - p1 >= 21) &&
7353 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7354 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007355 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007356 continue;
7357 }
7358
7359 /* OK, so we know that either p2 points to the end of string or to a comma */
7360 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7361 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7362 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7363 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007364 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007365 return;
7366 }
7367
7368 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007369 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007370 continue;
7371 }
7372 }
7373}
7374
7375
Willy Tarreau58f10d72006-12-04 02:26:12 +01007376/*
7377 * Try to retrieve a known appsession in the URI, then the associated server.
7378 * If the server is found, it's assigned to the session.
7379 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007380void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007381{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007382 char *end_params, *first_param, *cur_param, *next_param;
7383 char separator;
7384 int value_len;
7385
7386 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007387
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007388 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007389 (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 +01007390 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007391 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007392
Cyril Bontéb21570a2009-11-29 20:04:48 +01007393 first_param = NULL;
7394 switch (mode) {
7395 case PR_O2_AS_M_PP:
7396 first_param = memchr(begin, ';', len);
7397 break;
7398 case PR_O2_AS_M_QS:
7399 first_param = memchr(begin, '?', len);
7400 break;
7401 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007402
Cyril Bontéb21570a2009-11-29 20:04:48 +01007403 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007404 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007405 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007406
Cyril Bontéb21570a2009-11-29 20:04:48 +01007407 switch (mode) {
7408 case PR_O2_AS_M_PP:
7409 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7410 end_params = (char *) begin + len;
7411 }
7412 separator = ';';
7413 break;
7414 case PR_O2_AS_M_QS:
7415 end_params = (char *) begin + len;
7416 separator = '&';
7417 break;
7418 default:
7419 /* unknown mode, shouldn't happen */
7420 return;
7421 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007422
Cyril Bontéb21570a2009-11-29 20:04:48 +01007423 cur_param = next_param = end_params;
7424 while (cur_param > first_param) {
7425 cur_param--;
7426 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7427 /* let's see if this is the appsession parameter */
7428 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7429 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7430 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7431 /* Cool... it's the right one */
7432 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7433 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7434 if (value_len > 0) {
7435 manage_client_side_appsession(t, cur_param, value_len);
7436 }
7437 break;
7438 }
7439 next_param = cur_param;
7440 }
7441 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007442#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007443 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007444 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007445#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007446}
7447
Willy Tarreaub2513902006-12-17 14:52:38 +01007448/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007449 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007450 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007451 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007452 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007453 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007454 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007455 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007456 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007457int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007458{
7459 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007460 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007461 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007462 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007463
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007464 if (!uri_auth)
7465 return 0;
7466
Cyril Bonté70be45d2010-10-12 00:14:35 +02007467 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007468 return 0;
7469
Willy Tarreau295a8372011-03-10 11:25:07 +01007470 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007471 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007472
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007473 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007474 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007475 return 0;
7476
Willy Tarreau3a215be2012-03-09 21:39:51 +01007477 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007478 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007479 return 0;
7480
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007481 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007482 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007483 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007484 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007485 break;
7486 }
7487 h++;
7488 }
7489
7490 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007491 h = uri + uri_auth->uri_len;
7492 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007493 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007494 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007495 break;
7496 }
7497 h++;
7498 }
7499 }
7500
Willy Tarreau3a215be2012-03-09 21:39:51 +01007501 h = uri + uri_auth->uri_len;
7502 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007503 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007504 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007505 break;
7506 }
7507 h++;
7508 }
7509
Willy Tarreau3a215be2012-03-09 21:39:51 +01007510 h = uri + uri_auth->uri_len;
7511 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007512 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007513 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007514 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007515 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007516 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7517 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7518 si->applet.ctx.stats.st_code = i;
7519 break;
7520 }
7521 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007522 break;
7523 }
7524 h++;
7525 }
7526
Willy Tarreau295a8372011-03-10 11:25:07 +01007527 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007528
Willy Tarreaub2513902006-12-17 14:52:38 +01007529 return 1;
7530}
7531
Willy Tarreau4076a152009-04-02 15:18:36 +02007532/*
7533 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007534 * By default it tries to report the error position as msg->err_pos. However if
7535 * this one is not set, it will then report msg->next, which is the last known
7536 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007537 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007538 */
7539void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007540 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007541 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007542{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007543 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007544 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007545
Willy Tarreau9b28e032012-10-12 23:49:43 +02007546 es->len = MIN(chn->buf->i, sizeof(es->buf));
7547 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007548 len1 = MIN(len1, es->len);
7549 len2 = es->len - len1; /* remaining data if buffer wraps */
7550
Willy Tarreau9b28e032012-10-12 23:49:43 +02007551 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007552 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007553 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007554
Willy Tarreau4076a152009-04-02 15:18:36 +02007555 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007556 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007557 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007558 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007559
Willy Tarreau4076a152009-04-02 15:18:36 +02007560 es->when = date; // user-visible date
7561 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007562 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007563 es->oe = other_end;
Willy Tarreau986a9d22012-08-30 21:11:38 +02007564 es->src = s->req->prod->conn.addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007565 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007566 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007567 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007568 es->s_flags = s->flags;
7569 es->t_flags = s->txn.flags;
7570 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007571 es->b_out = chn->buf->o;
7572 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007573 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007574 es->m_clen = msg->chunk_len;
7575 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007576}
Willy Tarreaub2513902006-12-17 14:52:38 +01007577
Willy Tarreau294c4732011-12-16 21:35:50 +01007578/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7579 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7580 * performed over the whole headers. Otherwise it must contain a valid header
7581 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7582 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7583 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7584 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7585 * -1.
7586 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007587 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007588unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007589 struct hdr_idx *idx, int occ,
7590 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007591{
Willy Tarreau294c4732011-12-16 21:35:50 +01007592 struct hdr_ctx local_ctx;
7593 char *ptr_hist[MAX_HDR_HISTORY];
7594 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007595 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007596 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007597
Willy Tarreau294c4732011-12-16 21:35:50 +01007598 if (!ctx) {
7599 local_ctx.idx = 0;
7600 ctx = &local_ctx;
7601 }
7602
Willy Tarreaubce70882009-09-07 11:51:47 +02007603 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007604 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007605 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007606 occ--;
7607 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007608 *vptr = ctx->line + ctx->val;
7609 *vlen = ctx->vlen;
7610 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007611 }
7612 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007613 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007614 }
7615
7616 /* negative occurrence, we scan all the list then walk back */
7617 if (-occ > MAX_HDR_HISTORY)
7618 return 0;
7619
Willy Tarreau294c4732011-12-16 21:35:50 +01007620 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007621 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007622 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7623 len_hist[hist_ptr] = ctx->vlen;
7624 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007625 hist_ptr = 0;
7626 found++;
7627 }
7628 if (-occ > found)
7629 return 0;
7630 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7631 * find occurrence -occ, so we have to check [hist_ptr+occ].
7632 */
7633 hist_ptr += occ;
7634 if (hist_ptr >= MAX_HDR_HISTORY)
7635 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007636 *vptr = ptr_hist[hist_ptr];
7637 *vlen = len_hist[hist_ptr];
7638 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007639}
7640
Willy Tarreaubaaee002006-06-26 02:48:02 +02007641/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007642 * Print a debug line with a header. Always stop at the first CR or LF char,
7643 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7644 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007645 */
7646void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7647{
7648 int len, max;
7649 len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02007650 dir, (unsigned short)si_fd(t->req->prod), (unsigned short)si_fd(t->req->cons));
Willy Tarreaue92693a2012-09-24 21:13:39 +02007651
7652 for (max = 0; start + max < end; max++)
7653 if (start[max] == '\r' || start[max] == '\n')
7654 break;
7655
7656 UBOUND(max, trashlen - len - 3);
Willy Tarreau58f10d72006-12-04 02:26:12 +01007657 len += strlcpy2(trash + len, start, max + 1);
7658 trash[len++] = '\n';
Willy Tarreau21337822012-04-29 14:11:38 +02007659 if (write(1, trash, len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007660}
7661
Willy Tarreau0937bc42009-12-22 15:03:09 +01007662/*
7663 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7664 * the required fields are properly allocated and that we only need to (re)init
7665 * them. This should be used before processing any new request.
7666 */
7667void http_init_txn(struct session *s)
7668{
7669 struct http_txn *txn = &s->txn;
7670 struct proxy *fe = s->fe;
7671
7672 txn->flags = 0;
7673 txn->status = -1;
7674
William Lallemand5f232402012-04-05 18:02:55 +02007675 global.req_count++;
7676
Willy Tarreauf64d1412010-10-07 20:06:11 +02007677 txn->cookie_first_date = 0;
7678 txn->cookie_last_date = 0;
7679
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007680 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007681 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007682 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007683 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007684 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007685 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007686 txn->req.chunk_len = 0LL;
7687 txn->req.body_len = 0LL;
7688 txn->rsp.chunk_len = 0LL;
7689 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007690 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7691 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007692 txn->req.chn = s->req;
7693 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007694
7695 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007696
7697 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7698 if (fe->options2 & PR_O2_REQBUG_OK)
7699 txn->req.err_pos = -1; /* let buggy requests pass */
7700
Willy Tarreau46023632010-01-07 22:51:47 +01007701 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007702 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7703
Willy Tarreau46023632010-01-07 22:51:47 +01007704 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007705 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7706
7707 if (txn->hdr_idx.v)
7708 hdr_idx_init(&txn->hdr_idx);
7709}
7710
7711/* to be used at the end of a transaction */
7712void http_end_txn(struct session *s)
7713{
7714 struct http_txn *txn = &s->txn;
7715
7716 /* these ones will have been dynamically allocated */
7717 pool_free2(pool2_requri, txn->uri);
7718 pool_free2(pool2_capture, txn->cli_cookie);
7719 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007720 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007721 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007722
William Lallemanda73203e2012-03-12 12:48:57 +01007723 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007724 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007725 txn->uri = NULL;
7726 txn->srv_cookie = NULL;
7727 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007728
7729 if (txn->req.cap) {
7730 struct cap_hdr *h;
7731 for (h = s->fe->req_cap; h; h = h->next)
7732 pool_free2(h->pool, txn->req.cap[h->index]);
7733 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7734 }
7735
7736 if (txn->rsp.cap) {
7737 struct cap_hdr *h;
7738 for (h = s->fe->rsp_cap; h; h = h->next)
7739 pool_free2(h->pool, txn->rsp.cap[h->index]);
7740 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7741 }
7742
Willy Tarreau0937bc42009-12-22 15:03:09 +01007743}
7744
7745/* to be used at the end of a transaction to prepare a new one */
7746void http_reset_txn(struct session *s)
7747{
7748 http_end_txn(s);
7749 http_init_txn(s);
7750
7751 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007752 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007753 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007754 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007755 /* re-init store persistence */
7756 s->store_count = 0;
7757
Willy Tarreau0937bc42009-12-22 15:03:09 +01007758 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007759
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007760 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007761
Willy Tarreau739cfba2010-01-25 23:11:14 +01007762 /* We must trim any excess data from the response buffer, because we
7763 * may have blocked an invalid response from a server that we don't
7764 * want to accidentely forward once we disable the analysers, nor do
7765 * we want those data to come along with next response. A typical
7766 * example of such data would be from a buggy server responding to
7767 * a HEAD with some data, or sending more than the advertised
7768 * content-length.
7769 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007770 if (unlikely(s->rep->buf->i))
7771 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007772
Willy Tarreau0937bc42009-12-22 15:03:09 +01007773 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007774 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007775
Willy Tarreaud04e8582010-05-31 12:31:35 +02007776 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007777 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007778
7779 s->req->rex = TICK_ETERNITY;
7780 s->req->wex = TICK_ETERNITY;
7781 s->req->analyse_exp = TICK_ETERNITY;
7782 s->rep->rex = TICK_ETERNITY;
7783 s->rep->wex = TICK_ETERNITY;
7784 s->rep->analyse_exp = TICK_ETERNITY;
7785}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007786
Willy Tarreauff011f22011-01-06 17:51:27 +01007787void free_http_req_rules(struct list *r) {
7788 struct http_req_rule *tr, *pr;
7789
7790 list_for_each_entry_safe(pr, tr, r, list) {
7791 LIST_DEL(&pr->list);
7792 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7793 free(pr->http_auth.realm);
7794
7795 free(pr);
7796 }
7797}
7798
7799struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7800{
7801 struct http_req_rule *rule;
7802 int cur_arg;
7803
7804 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7805 if (!rule) {
7806 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7807 return NULL;
7808 }
7809
7810 if (!*args[0]) {
7811 goto req_error_parsing;
7812 } else if (!strcmp(args[0], "allow")) {
7813 rule->action = HTTP_REQ_ACT_ALLOW;
7814 cur_arg = 1;
7815 } else if (!strcmp(args[0], "deny")) {
7816 rule->action = HTTP_REQ_ACT_DENY;
7817 cur_arg = 1;
7818 } else if (!strcmp(args[0], "auth")) {
7819 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7820 cur_arg = 1;
7821
7822 while(*args[cur_arg]) {
7823 if (!strcmp(args[cur_arg], "realm")) {
7824 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7825 cur_arg+=2;
7826 continue;
7827 } else
7828 break;
7829 }
7830 } else {
7831req_error_parsing:
7832 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7833 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7834 return NULL;
7835 }
7836
7837 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7838 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007839 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007840
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007841 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7842 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7843 file, linenum, args[0], errmsg);
7844 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007845 return NULL;
7846 }
7847 rule->cond = cond;
7848 }
7849 else if (*args[cur_arg]) {
7850 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7851 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7852 file, linenum, args[0], args[cur_arg]);
7853 return NULL;
7854 }
7855
7856 return rule;
7857}
7858
Willy Tarreau8797c062007-05-07 00:55:35 +02007859/************************************************************************/
7860/* The code below is dedicated to ACL parsing and matching */
7861/************************************************************************/
7862
7863
Willy Tarreau14174bc2012-04-16 14:34:04 +02007864/* This function ensures that the prerequisites for an L7 fetch are ready,
7865 * which means that a request or response is ready. If some data is missing,
7866 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007867 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7868 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007869 *
7870 * The function returns :
7871 * 0 if some data is missing or if the requested data cannot be fetched
7872 * -1 if it is certain that we'll never have any HTTP message there
7873 * 1 if an HTTP message is ready
7874 */
7875static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007876acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007877 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007878{
7879 struct http_txn *txn = l7;
7880 struct http_msg *msg = &txn->req;
7881
7882 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7883 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7884 */
7885
7886 if (unlikely(!s || !txn))
7887 return 0;
7888
7889 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007890 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007891
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007892 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007893 if (unlikely(!s->req))
7894 return 0;
7895
7896 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007897 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007898 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007899 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007900 return -1;
7901 }
7902
7903 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007904 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02007905 http_msg_analyzer(msg, &txn->hdr_idx);
7906
7907 /* Still no valid request ? */
7908 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007909 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007910 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007911 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007912 return -1;
7913 }
7914 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007915 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007916 return 0;
7917 }
7918
7919 /* OK we just got a valid HTTP request. We have some minor
7920 * preparation to perform so that further checks can rely
7921 * on HTTP tests.
7922 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007923 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007924 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7925 s->flags |= SN_REDIRECTABLE;
7926
7927 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007928 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007929 return -1;
7930 }
7931 }
7932
Willy Tarreau24e32d82012-04-23 23:55:44 +02007933 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007934 return 0; /* data might have moved and indexes changed */
7935
7936 /* otherwise everything's ready for the request */
7937 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007938 else {
7939 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007940 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7941 return 0;
7942 }
7943
7944 /* everything's OK */
7945 return 1;
7946}
Willy Tarreau8797c062007-05-07 00:55:35 +02007947
Willy Tarreauc0239e02012-04-16 14:42:55 +02007948#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007949 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 +02007950
Willy Tarreau24e32d82012-04-23 23:55:44 +02007951#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007952 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 +02007953
Willy Tarreau8797c062007-05-07 00:55:35 +02007954
7955/* 1. Check on METHOD
7956 * We use the pre-parsed method if it is known, and store its number as an
7957 * integer. If it is unknown, we use the pointer and the length.
7958 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007959static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007960{
7961 int len, meth;
7962
Willy Tarreauae8b7962007-06-09 23:10:04 +02007963 len = strlen(*text);
7964 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007965
7966 pattern->val.i = meth;
7967 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007968 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007969 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007970 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007971 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007972 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007973 pattern->len = len;
7974 }
7975 return 1;
7976}
7977
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007978/* This function fetches the method of current HTTP request and stores
7979 * it in the global pattern struct as a chunk. There are two possibilities :
7980 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7981 * in <len> and <ptr> is NULL ;
7982 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7983 * <len> to its length.
7984 * This is intended to be used with acl_match_meth() only.
7985 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007986static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007987acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007988 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007989{
7990 int meth;
7991 struct http_txn *txn = l7;
7992
Willy Tarreau24e32d82012-04-23 23:55:44 +02007993 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007994
Willy Tarreau8797c062007-05-07 00:55:35 +02007995 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007996 smp->type = SMP_T_UINT;
7997 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02007998 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02007999 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8000 /* ensure the indexes are not affected */
8001 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008002 smp->type = SMP_T_CSTR;
8003 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008004 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008005 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008006 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008007 return 1;
8008}
8009
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008010/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008011static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008012{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008013 int icase;
8014
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008015
Willy Tarreauf853c462012-04-23 18:53:56 +02008016 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008017 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008018 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008019 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008020 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008021 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008022
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008023 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8024 if (pattern->val.i != HTTP_METH_OTHER)
8025 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008026
8027 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008028 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008029 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008030
8031 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008032 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8033 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008034 return ACL_PAT_FAIL;
8035 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008036}
8037
8038/* 2. Check on Request/Status Version
8039 * We simply compare strings here.
8040 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008041static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008042{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008043 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008044 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008045 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008046 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008047 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008048 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008049 return 1;
8050}
8051
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008052static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008053acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008054 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008055{
8056 struct http_txn *txn = l7;
8057 char *ptr;
8058 int len;
8059
Willy Tarreauc0239e02012-04-16 14:42:55 +02008060 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008061
Willy Tarreau8797c062007-05-07 00:55:35 +02008062 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008063 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008064
8065 while ((len-- > 0) && (*ptr++ != '/'));
8066 if (len <= 0)
8067 return 0;
8068
Willy Tarreauf853c462012-04-23 18:53:56 +02008069 smp->type = SMP_T_CSTR;
8070 smp->data.str.str = ptr;
8071 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008072
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008073 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008074 return 1;
8075}
8076
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008077static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008078acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008079 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008080{
8081 struct http_txn *txn = l7;
8082 char *ptr;
8083 int len;
8084
Willy Tarreauc0239e02012-04-16 14:42:55 +02008085 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008086
Willy Tarreau8797c062007-05-07 00:55:35 +02008087 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008088 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008089
8090 while ((len-- > 0) && (*ptr++ != '/'));
8091 if (len <= 0)
8092 return 0;
8093
Willy Tarreauf853c462012-04-23 18:53:56 +02008094 smp->type = SMP_T_CSTR;
8095 smp->data.str.str = ptr;
8096 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008097
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008098 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008099 return 1;
8100}
8101
8102/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008103static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008104acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008105 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008106{
8107 struct http_txn *txn = l7;
8108 char *ptr;
8109 int len;
8110
Willy Tarreauc0239e02012-04-16 14:42:55 +02008111 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008112
Willy Tarreau8797c062007-05-07 00:55:35 +02008113 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008114 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008115
Willy Tarreauf853c462012-04-23 18:53:56 +02008116 smp->type = SMP_T_UINT;
8117 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008118 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008119 return 1;
8120}
8121
8122/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008123static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008124smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008125 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008126{
8127 struct http_txn *txn = l7;
8128
Willy Tarreauc0239e02012-04-16 14:42:55 +02008129 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008130
Willy Tarreauf853c462012-04-23 18:53:56 +02008131 smp->type = SMP_T_CSTR;
8132 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008133 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008134 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008135 return 1;
8136}
8137
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008138static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008139smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008140 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008141{
8142 struct http_txn *txn = l7;
8143
Willy Tarreauc0239e02012-04-16 14:42:55 +02008144 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008145
8146 /* Parse HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008147 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn.addr.to);
Willy Tarreau986a9d22012-08-30 21:11:38 +02008148 if (((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008149 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008150 smp->type = SMP_T_IPV4;
Willy Tarreau986a9d22012-08-30 21:11:38 +02008151 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008152
8153 /*
8154 * If we are parsing url in frontend space, we prepare backend stage
8155 * to not parse again the same url ! optimization lazyness...
8156 */
8157 if (px->options & PR_O_HTTP_PROXY)
8158 l4->flags |= SN_ADDR_SET;
8159
Willy Tarreau37406352012-04-23 16:16:37 +02008160 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008161 return 1;
8162}
8163
8164static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008165smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008166 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008167{
8168 struct http_txn *txn = l7;
8169
Willy Tarreauc0239e02012-04-16 14:42:55 +02008170 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008171
8172 /* Same optimization as url_ip */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008173 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 +02008174 smp->type = SMP_T_UINT;
Willy Tarreau986a9d22012-08-30 21:11:38 +02008175 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn.addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008176
8177 if (px->options & PR_O_HTTP_PROXY)
8178 l4->flags |= SN_ADDR_SET;
8179
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008180 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008181 return 1;
8182}
8183
Willy Tarreau185b5c42012-04-26 15:11:51 +02008184/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8185 * Accepts an optional argument of type string containing the header field name,
8186 * and an optional argument of type signed or unsigned integer to request an
8187 * explicit occurrence of the header. Note that in the event of a missing name,
8188 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008189 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008190static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008191smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008192 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008193{
8194 struct http_txn *txn = l7;
8195 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008196 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008197 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008198 int occ = 0;
8199 const char *name_str = NULL;
8200 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008201
Willy Tarreau185b5c42012-04-26 15:11:51 +02008202 if (args) {
8203 if (args[0].type != ARGT_STR)
8204 return 0;
8205 name_str = args[0].data.str.str;
8206 name_len = args[0].data.str.len;
8207
8208 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8209 occ = args[1].data.uint;
8210 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008211
Willy Tarreaue333ec92012-04-16 16:26:40 +02008212 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008213
Willy Tarreau185b5c42012-04-26 15:11:51 +02008214 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008215 /* search for header from the beginning */
8216 ctx->idx = 0;
8217
Willy Tarreau185b5c42012-04-26 15:11:51 +02008218 if (!occ && !(opt & SMP_OPT_ITERATE))
8219 /* no explicit occurrence and single fetch => last header by default */
8220 occ = -1;
8221
8222 if (!occ)
8223 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008224 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008225
Willy Tarreau185b5c42012-04-26 15:11:51 +02008226 smp->type = SMP_T_CSTR;
8227 smp->flags |= SMP_F_VOL_HDR;
8228 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 +02008229 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008230
Willy Tarreau37406352012-04-23 16:16:37 +02008231 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008232 return 0;
8233}
8234
Willy Tarreauc11416f2007-06-17 16:58:38 +02008235/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008236 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008237 */
8238static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008239smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008240 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008241{
8242 struct http_txn *txn = l7;
8243 struct hdr_idx *idx = &txn->hdr_idx;
8244 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008245 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008246 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008247
Willy Tarreau24e32d82012-04-23 23:55:44 +02008248 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008249 return 0;
8250
Willy Tarreaue333ec92012-04-16 16:26:40 +02008251 CHECK_HTTP_MESSAGE_FIRST();
8252
Willy Tarreau33a7e692007-06-10 19:45:56 +02008253 ctx.idx = 0;
8254 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008255 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 +02008256 cnt++;
8257
Willy Tarreauf853c462012-04-23 18:53:56 +02008258 smp->type = SMP_T_UINT;
8259 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008260 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008261 return 1;
8262}
8263
Willy Tarreau185b5c42012-04-26 15:11:51 +02008264/* Fetch an HTTP header's integer value. The integer value is returned. It
8265 * takes a mandatory argument of type string and an optional one of type int
8266 * to designate a specific occurrence. It returns an unsigned integer, which
8267 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008268 */
8269static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008270smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008271 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008272{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008273 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008274
Willy Tarreauf853c462012-04-23 18:53:56 +02008275 if (ret > 0) {
8276 smp->type = SMP_T_UINT;
8277 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8278 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008279
Willy Tarreaud53e2422012-04-16 17:21:11 +02008280 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008281}
8282
Cyril Bonté69fa9922012-10-25 00:01:06 +02008283/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8284 * and an optional one of type int to designate a specific occurrence.
8285 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008286 */
8287static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008288smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008289 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008290{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008291 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008292
Willy Tarreau185b5c42012-04-26 15:11:51 +02008293 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008294 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8295 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008296 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008297 } else {
8298 struct chunk *temp = sample_get_trash_chunk();
8299 if (smp->data.str.len < temp->size - 1) {
8300 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8301 temp->str[smp->data.str.len] = '\0';
8302 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8303 smp->type = SMP_T_IPV6;
8304 break;
8305 }
8306 }
8307 }
8308
Willy Tarreaud53e2422012-04-16 17:21:11 +02008309 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008310 if (!(smp->flags & SMP_F_NOT_LAST))
8311 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008312 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008313 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008314}
8315
Willy Tarreau737b0c12007-06-10 21:28:46 +02008316/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8317 * the first '/' after the possible hostname, and ends before the possible '?'.
8318 */
8319static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008320smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008321 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008322{
8323 struct http_txn *txn = l7;
8324 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008325
Willy Tarreauc0239e02012-04-16 14:42:55 +02008326 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008327
Willy Tarreau9b28e032012-10-12 23:49:43 +02008328 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008329 ptr = http_get_path(txn);
8330 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008331 return 0;
8332
8333 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008334 smp->type = SMP_T_CSTR;
8335 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008336
8337 while (ptr < end && *ptr != '?')
8338 ptr++;
8339
Willy Tarreauf853c462012-04-23 18:53:56 +02008340 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008341 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008342 return 1;
8343}
8344
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008345/* This produces a concatenation of the first occurrence of the Host header
8346 * followed by the path component if it begins with a slash ('/'). This means
8347 * that '*' will not be added, resulting in exactly the first Host entry.
8348 * If no Host header is found, then the path is returned as-is. The returned
8349 * value is stored in the trash so it does not need to be marked constant.
8350 */
8351static int
8352smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8353 const struct arg *args, struct sample *smp)
8354{
8355 struct http_txn *txn = l7;
8356 char *ptr, *end, *beg;
8357 struct hdr_ctx ctx;
8358
8359 CHECK_HTTP_MESSAGE_FIRST();
8360
8361 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008362 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 +02008363 !ctx.vlen)
8364 return smp_fetch_path(px, l4, l7, opt, args, smp);
8365
8366 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8367 memcpy(trash, ctx.line + ctx.val, ctx.vlen);
8368 smp->type = SMP_T_STR;
8369 smp->data.str.str = trash;
8370 smp->data.str.len = ctx.vlen;
8371
8372 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008373 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 +02008374 beg = http_get_path(txn);
8375 if (!beg)
8376 beg = end;
8377
8378 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8379
8380 if (beg < ptr && *beg == '/') {
8381 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8382 smp->data.str.len += ptr - beg;
8383 }
8384
8385 smp->flags = SMP_F_VOL_1ST;
8386 return 1;
8387}
8388
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008389static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008390acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008391 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008392{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008393 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8394 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8395 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008396
Willy Tarreau24e32d82012-04-23 23:55:44 +02008397 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008398
Willy Tarreauf853c462012-04-23 18:53:56 +02008399 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008400 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008401 return 1;
8402}
8403
Willy Tarreau7f18e522010-10-22 20:04:13 +02008404/* return a valid test if the current request is the first one on the connection */
8405static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008406acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008407 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008408{
8409 if (!s)
8410 return 0;
8411
Willy Tarreauf853c462012-04-23 18:53:56 +02008412 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008413 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008414 return 1;
8415}
8416
Willy Tarreau34db1082012-04-19 17:16:54 +02008417/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008418static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008419acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008420 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008421{
8422
Willy Tarreau24e32d82012-04-23 23:55:44 +02008423 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008424 return 0;
8425
Willy Tarreauc0239e02012-04-16 14:42:55 +02008426 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008427
Willy Tarreauc0239e02012-04-16 14:42:55 +02008428 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008429 return 0;
8430
Willy Tarreauf853c462012-04-23 18:53:56 +02008431 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008432 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 +01008433 return 1;
8434}
Willy Tarreau8797c062007-05-07 00:55:35 +02008435
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008436/* Accepts exactly 1 argument of type userlist */
8437static int
8438acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8439 const struct arg *args, struct sample *smp)
8440{
8441
8442 if (!args || args->type != ARGT_USR)
8443 return 0;
8444
8445 CHECK_HTTP_MESSAGE_FIRST();
8446
8447 if (!get_http_auth(l4))
8448 return 0;
8449
8450 /* acl_match_auth() will need several information at once */
8451 smp->ctx.a[0] = args->data.usr; /* user list */
8452 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8453 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8454
8455 /* if the user does not belong to the userlist or has a wrong password,
8456 * report that it unconditionally does not match. Otherwise we return
8457 * a non-zero integer which will be ignored anyway since all the params
8458 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8459 */
8460 smp->type = SMP_T_BOOL;
8461 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8462 if (smp->data.uint)
8463 smp->type = SMP_T_UINT;
8464
8465 return 1;
8466}
8467
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008468/* Try to find the next occurrence of a cookie name in a cookie header value.
8469 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8470 * the cookie value is returned into *value and *value_l, and the function
8471 * returns a pointer to the next pointer to search from if the value was found.
8472 * Otherwise if the cookie was not found, NULL is returned and neither value
8473 * nor value_l are touched. The input <hdr> string should first point to the
8474 * header's value, and the <hdr_end> pointer must point to the first character
8475 * not part of the value. <list> must be non-zero if value may represent a list
8476 * of values (cookie headers). This makes it faster to abort parsing when no
8477 * list is expected.
8478 */
8479static char *
8480extract_cookie_value(char *hdr, const char *hdr_end,
8481 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008482 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008483{
8484 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8485 char *next;
8486
8487 /* we search at least a cookie name followed by an equal, and more
8488 * generally something like this :
8489 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8490 */
8491 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8492 /* Iterate through all cookies on this line */
8493
8494 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8495 att_beg++;
8496
8497 /* find att_end : this is the first character after the last non
8498 * space before the equal. It may be equal to hdr_end.
8499 */
8500 equal = att_end = att_beg;
8501
8502 while (equal < hdr_end) {
8503 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8504 break;
8505 if (http_is_spht[(unsigned char)*equal++])
8506 continue;
8507 att_end = equal;
8508 }
8509
8510 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8511 * is between <att_beg> and <equal>, both may be identical.
8512 */
8513
8514 /* look for end of cookie if there is an equal sign */
8515 if (equal < hdr_end && *equal == '=') {
8516 /* look for the beginning of the value */
8517 val_beg = equal + 1;
8518 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8519 val_beg++;
8520
8521 /* find the end of the value, respecting quotes */
8522 next = find_cookie_value_end(val_beg, hdr_end);
8523
8524 /* make val_end point to the first white space or delimitor after the value */
8525 val_end = next;
8526 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8527 val_end--;
8528 } else {
8529 val_beg = val_end = next = equal;
8530 }
8531
8532 /* We have nothing to do with attributes beginning with '$'. However,
8533 * they will automatically be removed if a header before them is removed,
8534 * since they're supposed to be linked together.
8535 */
8536 if (*att_beg == '$')
8537 continue;
8538
8539 /* Ignore cookies with no equal sign */
8540 if (equal == next)
8541 continue;
8542
8543 /* Now we have the cookie name between att_beg and att_end, and
8544 * its value between val_beg and val_end.
8545 */
8546
8547 if (att_end - att_beg == cookie_name_l &&
8548 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8549 /* let's return this value and indicate where to go on from */
8550 *value = val_beg;
8551 *value_l = val_end - val_beg;
8552 return next + 1;
8553 }
8554
8555 /* Set-Cookie headers only have the name in the first attr=value part */
8556 if (!list)
8557 break;
8558 }
8559
8560 return NULL;
8561}
8562
Willy Tarreaue333ec92012-04-16 16:26:40 +02008563/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008564 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8565 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008566 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008567 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008568 * Accepts exactly 1 argument of type string. If the input options indicate
8569 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008570 */
8571static int
Willy Tarreau51539362012-05-08 12:46:28 +02008572smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8573 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008574{
8575 struct http_txn *txn = l7;
8576 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008577 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008578 const struct http_msg *msg;
8579 const char *hdr_name;
8580 int hdr_name_len;
8581 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008582 int occ = 0;
8583 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008584
Willy Tarreau24e32d82012-04-23 23:55:44 +02008585 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008586 return 0;
8587
Willy Tarreaue333ec92012-04-16 16:26:40 +02008588 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008589
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008590 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008591 msg = &txn->req;
8592 hdr_name = "Cookie";
8593 hdr_name_len = 6;
8594 } else {
8595 msg = &txn->rsp;
8596 hdr_name = "Set-Cookie";
8597 hdr_name_len = 10;
8598 }
8599
Willy Tarreau28376d62012-04-26 21:26:10 +02008600 if (!occ && !(opt & SMP_OPT_ITERATE))
8601 /* no explicit occurrence and single fetch => last cookie by default */
8602 occ = -1;
8603
8604 /* OK so basically here, either we want only one value and it's the
8605 * last one, or we want to iterate over all of them and we fetch the
8606 * next one.
8607 */
8608
Willy Tarreau9b28e032012-10-12 23:49:43 +02008609 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008610 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008611 /* search for the header from the beginning, we must first initialize
8612 * the search parameters.
8613 */
Willy Tarreau37406352012-04-23 16:16:37 +02008614 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008615 ctx->idx = 0;
8616 }
8617
Willy Tarreau28376d62012-04-26 21:26:10 +02008618 smp->flags |= SMP_F_VOL_HDR;
8619
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008620 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008621 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8622 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008623 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8624 goto out;
8625
Willy Tarreau24e32d82012-04-23 23:55:44 +02008626 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008627 continue;
8628
Willy Tarreau37406352012-04-23 16:16:37 +02008629 smp->ctx.a[0] = ctx->line + ctx->val;
8630 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008631 }
8632
Willy Tarreauf853c462012-04-23 18:53:56 +02008633 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008634 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008635 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008636 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008637 &smp->data.str.str,
8638 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008639 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008640 found = 1;
8641 if (occ >= 0) {
8642 /* one value was returned into smp->data.str.{str,len} */
8643 smp->flags |= SMP_F_NOT_LAST;
8644 return 1;
8645 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008646 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008647 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008648 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008649 /* all cookie headers and values were scanned. If we're looking for the
8650 * last occurrence, we may return it now.
8651 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008652 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008653 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008654 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008655}
8656
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008657/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008658 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008659 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008660 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008661 */
8662static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008663acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008664 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008665{
8666 struct http_txn *txn = l7;
8667 struct hdr_idx *idx = &txn->hdr_idx;
8668 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008669 const struct http_msg *msg;
8670 const char *hdr_name;
8671 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008672 int cnt;
8673 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008674 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008675
Willy Tarreau24e32d82012-04-23 23:55:44 +02008676 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008677 return 0;
8678
Willy Tarreaue333ec92012-04-16 16:26:40 +02008679 CHECK_HTTP_MESSAGE_FIRST();
8680
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008681 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008682 msg = &txn->req;
8683 hdr_name = "Cookie";
8684 hdr_name_len = 6;
8685 } else {
8686 msg = &txn->rsp;
8687 hdr_name = "Set-Cookie";
8688 hdr_name_len = 10;
8689 }
8690
Willy Tarreau9b28e032012-10-12 23:49:43 +02008691 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008692 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008693 ctx.idx = 0;
8694 cnt = 0;
8695
8696 while (1) {
8697 /* Note: val_beg == NULL every time we need to fetch a new header */
8698 if (!val_beg) {
8699 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8700 break;
8701
Willy Tarreau24e32d82012-04-23 23:55:44 +02008702 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008703 continue;
8704
8705 val_beg = ctx.line + ctx.val;
8706 val_end = val_beg + ctx.vlen;
8707 }
8708
Willy Tarreauf853c462012-04-23 18:53:56 +02008709 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008710 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008711 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008712 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008713 &smp->data.str.str,
8714 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008715 cnt++;
8716 }
8717 }
8718
Willy Tarreauf853c462012-04-23 18:53:56 +02008719 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008720 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008721 return 1;
8722}
8723
Willy Tarreau51539362012-05-08 12:46:28 +02008724/* Fetch an cookie's integer value. The integer value is returned. It
8725 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8726 */
8727static int
8728smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8729 const struct arg *args, struct sample *smp)
8730{
8731 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8732
8733 if (ret > 0) {
8734 smp->type = SMP_T_UINT;
8735 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8736 }
8737
8738 return ret;
8739}
8740
Willy Tarreau8797c062007-05-07 00:55:35 +02008741/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008742/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008743/************************************************************************/
8744
David Cournapeau16023ee2010-12-23 20:55:41 +09008745/*
8746 * Given a path string and its length, find the position of beginning of the
8747 * query string. Returns NULL if no query string is found in the path.
8748 *
8749 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8750 *
8751 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8752 */
bedis4c75cca2012-10-05 08:38:24 +02008753static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008754{
8755 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008756
bedis4c75cca2012-10-05 08:38:24 +02008757 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09008758 return p ? p + 1 : NULL;
8759}
8760
bedis4c75cca2012-10-05 08:38:24 +02008761static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008762{
bedis4c75cca2012-10-05 08:38:24 +02008763 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09008764}
8765
8766/*
8767 * Given a url parameter, find the starting position of the first occurence,
8768 * or NULL if the parameter is not found.
8769 *
8770 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8771 * the function will return query_string+8.
8772 */
8773static char*
8774find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02008775 char* url_param_name, size_t url_param_name_l,
8776 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008777{
8778 char *pos, *last;
8779
8780 pos = query_string;
8781 last = query_string + query_string_l - url_param_name_l - 1;
8782
8783 while (pos <= last) {
8784 if (pos[url_param_name_l] == '=') {
8785 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8786 return pos;
8787 pos += url_param_name_l + 1;
8788 }
bedis4c75cca2012-10-05 08:38:24 +02008789 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008790 pos++;
8791 pos++;
8792 }
8793 return NULL;
8794}
8795
8796/*
8797 * Given a url parameter name, returns its value and size into *value and
8798 * *value_l respectively, and returns non-zero. If the parameter is not found,
8799 * zero is returned and value/value_l are not touched.
8800 */
8801static int
8802find_url_param_value(char* path, size_t path_l,
8803 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02008804 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008805{
8806 char *query_string, *qs_end;
8807 char *arg_start;
8808 char *value_start, *value_end;
8809
bedis4c75cca2012-10-05 08:38:24 +02008810 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008811 if (!query_string)
8812 return 0;
8813
8814 qs_end = path + path_l;
8815 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02008816 url_param_name, url_param_name_l,
8817 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008818 if (!arg_start)
8819 return 0;
8820
8821 value_start = arg_start + url_param_name_l + 1;
8822 value_end = value_start;
8823
bedis4c75cca2012-10-05 08:38:24 +02008824 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008825 value_end++;
8826
8827 *value = value_start;
8828 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008829 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008830}
8831
8832static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008833smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8834 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008835{
bedis4c75cca2012-10-05 08:38:24 +02008836 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09008837 struct http_txn *txn = l7;
8838 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008839
bedis4c75cca2012-10-05 08:38:24 +02008840 if (!args || args[0].type != ARGT_STR ||
8841 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008842 return 0;
8843
8844 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008845
bedis4c75cca2012-10-05 08:38:24 +02008846 if (args[1].type)
8847 delim = *args[1].data.str.str;
8848
Willy Tarreau9b28e032012-10-12 23:49:43 +02008849 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02008850 args->data.str.str, args->data.str.len,
8851 &smp->data.str.str, &smp->data.str.len,
8852 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008853 return 0;
8854
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008855 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008856 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008857 return 1;
8858}
8859
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008860/* Return the signed integer value for the specified url parameter (see url_param
8861 * above).
8862 */
8863static int
8864smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8865 const struct arg *args, struct sample *smp)
8866{
8867 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
8868
8869 if (ret > 0) {
8870 smp->type = SMP_T_UINT;
8871 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8872 }
8873
8874 return ret;
8875}
8876
Willy Tarreau185b5c42012-04-26 15:11:51 +02008877/* This function is used to validate the arguments passed to any "hdr" fetch
8878 * keyword. These keywords support an optional positive or negative occurrence
8879 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8880 * is assumed that the types are already the correct ones. Returns 0 on error,
8881 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8882 * error message in case of error, that the caller is responsible for freeing.
8883 * The initial location must either be freeable or NULL.
8884 */
8885static int val_hdr(struct arg *arg, char **err_msg)
8886{
8887 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008888 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02008889 return 0;
8890 }
8891 return 1;
8892}
8893
Willy Tarreau4a568972010-05-12 08:08:50 +02008894/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008895/* All supported ACL keywords must be declared here. */
8896/************************************************************************/
8897
8898/* Note: must not be declared <const> as its list will be overwritten.
8899 * Please take care of keeping this list alphabetically sorted.
8900 */
8901static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008902 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8903 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8904 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8905 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8906 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8907 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8908 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8909 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8910
Willy Tarreau51539362012-05-08 12:46:28 +02008911 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8912 { "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 +02008913 { "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 +02008914 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8915 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8916 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8917 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8918 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8919 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8920 { "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 +02008921
Willy Tarreau185b5c42012-04-26 15:11:51 +02008922 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8923 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8924 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8925 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8926 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8927 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8928 { "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 },
8929 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8930 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8931 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8932 { "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 +02008933
8934 { "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 +02008935 { "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 +02008936 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8937
8938 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8939
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008940 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8941 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8942 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8943 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8944 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8945 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8946 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8947 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008948
8949 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8950 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8951 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8952
Willy Tarreau51539362012-05-08 12:46:28 +02008953 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8954 { "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 +02008955 { "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 +02008956 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8957 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8958 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8959 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8960 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8961 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8962 { "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 +02008963
Willy Tarreau185b5c42012-04-26 15:11:51 +02008964 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8965 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8966 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8967 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8968 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8969 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8970 { "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 },
8971 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8972 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8973 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8974 { "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 +02008975
8976 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8977
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008978 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8979 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8980 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8981 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8982 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8983 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8984 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8985 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8986 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8987 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008988
8989 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8990 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8991 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8992 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8993 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8994 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8995 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8996 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8997 { "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 +02008998 { "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 +02008999
9000 { NULL, NULL, NULL, NULL },
9001}};
9002
9003/************************************************************************/
9004/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009005/************************************************************************/
9006/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009007static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009008 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9009 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9010 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9011 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9012 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
9013 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
9014 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9015 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
9016 { "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 +02009017 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009018}};
9019
Willy Tarreau8797c062007-05-07 00:55:35 +02009020
9021__attribute__((constructor))
9022static void __http_protocol_init(void)
9023{
9024 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009025 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009026}
9027
9028
Willy Tarreau58f10d72006-12-04 02:26:12 +01009029/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009030 * Local variables:
9031 * c-indent-level: 8
9032 * c-basic-offset: 8
9033 * End:
9034 */