blob: 8e96ae684d414902e60226020917154845f1f28d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * HTTP protocol analyzer
3 *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <ctype.h>
14#include <errno.h>
15#include <fcntl.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19#include <syslog.h>
Willy Tarreau42250582007-04-01 01:30:43 +020020#include <time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#include <sys/socket.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25
Willy Tarreaub05405a2012-01-23 15:35:52 +010026#include <netinet/tcp.h>
27
Willy Tarreau2dd0d472006-06-29 17:53:05 +020028#include <common/appsession.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010029#include <common/base64.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020030#include <common/chunk.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020031#include <common/compat.h>
32#include <common/config.h>
Willy Tarreaua4cd1f52006-12-16 19:57:26 +010033#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020034#include <common/memory.h>
35#include <common/mini-clist.h>
36#include <common/standard.h>
Willy Tarreau0c303ee2008-07-07 00:09:58 +020037#include <common/ticks.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020038#include <common/time.h>
39#include <common/uri_auth.h>
40#include <common/version.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
42#include <types/capture.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020043#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044
Willy Tarreau8797c062007-05-07 00:55:35 +020045#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020046#include <proto/arg.h>
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +010047#include <proto/auth.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020048#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020049#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010050#include <proto/checks.h>
William Lallemand82fe75c2012-10-23 10:25:10 +020051#include <proto/compression.h>
Willy Tarreau91861262007-10-17 17:06:05 +020052#include <proto/dumpstats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/fd.h>
Willy Tarreau03fa5df2010-05-24 21:02:37 +020054#include <proto/frontend.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020055#include <proto/log.h>
Willy Tarreau58f10d72006-12-04 02:26:12 +010056#include <proto/hdr_idx.h>
Willy Tarreaub6866442008-07-14 23:54:42 +020057#include <proto/proto_tcp.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058#include <proto/proto_http.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010059#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020060#include <proto/queue.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020061#include <proto/sample.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010062#include <proto/server.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020063#include <proto/session.h>
Willy Tarreaucff64112008-11-03 06:26:53 +010064#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065#include <proto/task.h>
66
Willy Tarreau522d6c02009-12-06 18:49:18 +010067const char HTTP_100[] =
68 "HTTP/1.1 100 Continue\r\n\r\n";
69
70const struct chunk http_100_chunk = {
71 .str = (char *)&HTTP_100,
72 .len = sizeof(HTTP_100)-1
73};
74
Willy Tarreaua9679ac2010-01-03 17:32:57 +010075/* Warning: no "connection" header is provided with the 3xx messages below */
Willy Tarreaub463dfb2008-06-07 23:08:56 +020076const char *HTTP_301 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010077 "HTTP/1.1 301 Moved Permanently\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020078 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010079 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020080 "Location: "; /* not terminated since it will be concatenated with the URL */
81
Willy Tarreau0f772532006-12-23 20:51:41 +010082const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010083 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010084 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010085 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010086 "Location: "; /* not terminated since it will be concatenated with the URL */
87
88/* same as 302 except that the browser MUST retry with the GET method */
89const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010090 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010091 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010092 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010093 "Location: "; /* not terminated since it will be concatenated with the URL */
94
Willy Tarreaubaaee002006-06-26 02:48:02 +020095/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
96const char *HTTP_401_fmt =
97 "HTTP/1.0 401 Unauthorized\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200100 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
102 "\r\n"
103 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
104
Willy Tarreau844a7e72010-01-31 21:46:18 +0100105const char *HTTP_407_fmt =
106 "HTTP/1.0 407 Unauthorized\r\n"
107 "Cache-Control: no-cache\r\n"
108 "Connection: close\r\n"
109 "Content-Type: text/html\r\n"
110 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
111 "\r\n"
112 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
113
Willy Tarreau0f772532006-12-23 20:51:41 +0100114
115const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200116 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100117 [HTTP_ERR_400] = 400,
118 [HTTP_ERR_403] = 403,
119 [HTTP_ERR_408] = 408,
120 [HTTP_ERR_500] = 500,
121 [HTTP_ERR_502] = 502,
122 [HTTP_ERR_503] = 503,
123 [HTTP_ERR_504] = 504,
124};
125
Willy Tarreau80587432006-12-24 17:47:20 +0100126static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200127 [HTTP_ERR_200] =
128 "HTTP/1.0 200 OK\r\n"
129 "Cache-Control: no-cache\r\n"
130 "Connection: close\r\n"
131 "Content-Type: text/html\r\n"
132 "\r\n"
133 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
134
Willy Tarreau0f772532006-12-23 20:51:41 +0100135 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100136 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100137 "Cache-Control: no-cache\r\n"
138 "Connection: close\r\n"
139 "Content-Type: text/html\r\n"
140 "\r\n"
141 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
142
143 [HTTP_ERR_403] =
144 "HTTP/1.0 403 Forbidden\r\n"
145 "Cache-Control: no-cache\r\n"
146 "Connection: close\r\n"
147 "Content-Type: text/html\r\n"
148 "\r\n"
149 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
150
151 [HTTP_ERR_408] =
152 "HTTP/1.0 408 Request Time-out\r\n"
153 "Cache-Control: no-cache\r\n"
154 "Connection: close\r\n"
155 "Content-Type: text/html\r\n"
156 "\r\n"
157 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
158
159 [HTTP_ERR_500] =
160 "HTTP/1.0 500 Server Error\r\n"
161 "Cache-Control: no-cache\r\n"
162 "Connection: close\r\n"
163 "Content-Type: text/html\r\n"
164 "\r\n"
165 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
166
167 [HTTP_ERR_502] =
168 "HTTP/1.0 502 Bad Gateway\r\n"
169 "Cache-Control: no-cache\r\n"
170 "Connection: close\r\n"
171 "Content-Type: text/html\r\n"
172 "\r\n"
173 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
174
175 [HTTP_ERR_503] =
176 "HTTP/1.0 503 Service Unavailable\r\n"
177 "Cache-Control: no-cache\r\n"
178 "Connection: close\r\n"
179 "Content-Type: text/html\r\n"
180 "\r\n"
181 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
182
183 [HTTP_ERR_504] =
184 "HTTP/1.0 504 Gateway Time-out\r\n"
185 "Cache-Control: no-cache\r\n"
186 "Connection: close\r\n"
187 "Content-Type: text/html\r\n"
188 "\r\n"
189 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
190
191};
192
Cyril Bonté19979e12012-04-04 12:57:21 +0200193/* status codes available for the stats admin page (strictly 4 chars length) */
194const char *stat_status_codes[STAT_STATUS_SIZE] = {
195 [STAT_STATUS_DENY] = "DENY",
196 [STAT_STATUS_DONE] = "DONE",
197 [STAT_STATUS_ERRP] = "ERRP",
198 [STAT_STATUS_EXCD] = "EXCD",
199 [STAT_STATUS_NONE] = "NONE",
200 [STAT_STATUS_PART] = "PART",
201 [STAT_STATUS_UNKN] = "UNKN",
202};
203
204
Willy Tarreau80587432006-12-24 17:47:20 +0100205/* We must put the messages here since GCC cannot initialize consts depending
206 * on strlen().
207 */
208struct chunk http_err_chunks[HTTP_ERR_SIZE];
209
Willy Tarreau42250582007-04-01 01:30:43 +0200210#define FD_SETS_ARE_BITFIELDS
211#ifdef FD_SETS_ARE_BITFIELDS
212/*
213 * This map is used with all the FD_* macros to check whether a particular bit
214 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
215 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
216 * byte should be encoded. Be careful to always pass bytes from 0 to 255
217 * exclusively to the macros.
218 */
219fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
220fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
221
222#else
223#error "Check if your OS uses bitfields for fd_sets"
224#endif
225
Willy Tarreau80587432006-12-24 17:47:20 +0100226void init_proto_http()
227{
Willy Tarreau42250582007-04-01 01:30:43 +0200228 int i;
229 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100230 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200231
Willy Tarreau80587432006-12-24 17:47:20 +0100232 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
233 if (!http_err_msgs[msg]) {
234 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
235 abort();
236 }
237
238 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
239 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
240 }
Willy Tarreau42250582007-04-01 01:30:43 +0200241
242 /* initialize the log header encoding map : '{|}"#' should be encoded with
243 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
244 * URL encoding only requires '"', '#' to be encoded as well as non-
245 * printable characters above.
246 */
247 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
248 memset(url_encode_map, 0, sizeof(url_encode_map));
249 for (i = 0; i < 32; i++) {
250 FD_SET(i, hdr_encode_map);
251 FD_SET(i, url_encode_map);
252 }
253 for (i = 127; i < 256; i++) {
254 FD_SET(i, hdr_encode_map);
255 FD_SET(i, url_encode_map);
256 }
257
258 tmp = "\"#{|}";
259 while (*tmp) {
260 FD_SET(*tmp, hdr_encode_map);
261 tmp++;
262 }
263
264 tmp = "\"#";
265 while (*tmp) {
266 FD_SET(*tmp, url_encode_map);
267 tmp++;
268 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200269
270 /* memory allocations */
271 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
Willy Tarreau086b3b42007-05-13 21:45:51 +0200272 pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100273 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100274}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275
Willy Tarreau53b6c742006-12-17 13:37:46 +0100276/*
277 * We have 26 list of methods (1 per first letter), each of which can have
278 * up to 3 entries (2 valid, 1 null).
279 */
280struct http_method_desc {
281 http_meth_t meth;
282 int len;
283 const char text[8];
284};
285
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100286const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100287 ['C' - 'A'] = {
288 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
289 },
290 ['D' - 'A'] = {
291 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
292 },
293 ['G' - 'A'] = {
294 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
295 },
296 ['H' - 'A'] = {
297 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
298 },
299 ['P' - 'A'] = {
300 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
301 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
302 },
303 ['T' - 'A'] = {
304 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
305 },
306 /* rest is empty like this :
307 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
308 */
309};
310
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100311/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200312 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100313 * RFC2616 for those chars.
314 */
315
316const char http_is_spht[256] = {
317 [' '] = 1, ['\t'] = 1,
318};
319
320const char http_is_crlf[256] = {
321 ['\r'] = 1, ['\n'] = 1,
322};
323
324const char http_is_lws[256] = {
325 [' '] = 1, ['\t'] = 1,
326 ['\r'] = 1, ['\n'] = 1,
327};
328
329const char http_is_sep[256] = {
330 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
331 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
332 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
333 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
334 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
335};
336
337const char http_is_ctl[256] = {
338 [0 ... 31] = 1,
339 [127] = 1,
340};
341
342/*
343 * A token is any ASCII char that is neither a separator nor a CTL char.
344 * Do not overwrite values in assignment since gcc-2.95 will not handle
345 * them correctly. Instead, define every non-CTL char's status.
346 */
347const char http_is_token[256] = {
348 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
349 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
350 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
351 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
352 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
353 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
354 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
355 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
356 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
357 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
358 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
359 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
360 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
361 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
362 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
363 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
364 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
365 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
366 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
367 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
368 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
369 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
370 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
371 ['|'] = 1, ['}'] = 0, ['~'] = 1,
372};
373
374
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100375/*
376 * An http ver_token is any ASCII which can be found in an HTTP version,
377 * which includes 'H', 'T', 'P', '/', '.' and any digit.
378 */
379const char http_is_ver_token[256] = {
380 ['.'] = 1, ['/'] = 1,
381 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
382 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
383 ['H'] = 1, ['P'] = 1, ['T'] = 1,
384};
385
386
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100387/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100388 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
389 */
390#if defined(DEBUG_FSM)
391static void http_silent_debug(int line, struct session *s)
392{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100393 chunk_printf(&trash,
394 "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld tf=%08x\n",
395 line,
396 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
397 s->req->buf->data, s->req->buf->size, s->req->l, s->req->w, s->req->r, s->req->buf->p, s->req->buf->o, s->req->to_forward, s->txn.flags);
398 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100399
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100400 chunk_printf(&trash,
401 " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p o=%p sm=%d fw=%ld\n",
402 line,
403 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
404 s->rep->buf->data, s->rep->buf->size, s->rep->l, s->rep->w, s->rep->r, s->rep->buf->p, s->rep->buf->o, s->rep->to_forward);
405 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100406}
407#else
408#define http_silent_debug(l,s) do { } while (0)
409#endif
410
411/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100412 * Adds a header and its CRLF at the tail of the message's buffer, just before
413 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100414 * The header is also automatically added to the index <hdr_idx>, and the end
415 * of headers is automatically adjusted. The number of bytes added is returned
416 * on success, otherwise <0 is returned indicating an error.
417 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100418int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100419{
420 int bytes, len;
421
422 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200423 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100424 if (!bytes)
425 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100426 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100427 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
428}
429
430/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100431 * Adds a header and its CRLF at the tail of the message's buffer, just before
432 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100433 * the buffer is only opened and the space reserved, but nothing is copied.
434 * The header is also automatically added to the index <hdr_idx>, and the end
435 * of headers is automatically adjusted. The number of bytes added is returned
436 * on success, otherwise <0 is returned indicating an error.
437 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100438int http_header_add_tail2(struct http_msg *msg,
439 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100440{
441 int bytes;
442
Willy Tarreau9b28e032012-10-12 23:49:43 +0200443 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100444 if (!bytes)
445 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100446 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100447 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
448}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200449
450/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100451 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
452 * If so, returns the position of the first non-space character relative to
453 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
454 * to return a pointer to the place after the first space. Returns 0 if the
455 * header name does not match. Checks are case-insensitive.
456 */
457int http_header_match2(const char *hdr, const char *end,
458 const char *name, int len)
459{
460 const char *val;
461
462 if (hdr + len >= end)
463 return 0;
464 if (hdr[len] != ':')
465 return 0;
466 if (strncasecmp(hdr, name, len) != 0)
467 return 0;
468 val = hdr + len + 1;
469 while (val < end && HTTP_IS_SPHT(*val))
470 val++;
471 if ((val >= end) && (len + 2 <= end - hdr))
472 return len + 2; /* we may replace starting from second space */
473 return val - hdr;
474}
475
Willy Tarreau68085d82010-01-18 14:54:04 +0100476/* Find the end of the header value contained between <s> and <e>. See RFC2616,
477 * par 2.2 for more information. Note that it requires a valid header to return
478 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200479 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100480char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200481{
482 int quoted, qdpair;
483
484 quoted = qdpair = 0;
485 for (; s < e; s++) {
486 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200487 else if (quoted) {
488 if (*s == '\\') qdpair = 1;
489 else if (*s == '"') quoted = 0;
490 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200491 else if (*s == '"') quoted = 1;
492 else if (*s == ',') return s;
493 }
494 return s;
495}
496
497/* Find the first or next occurrence of header <name> in message buffer <sol>
498 * using headers index <idx>, and return it in the <ctx> structure. This
499 * structure holds everything necessary to use the header and find next
500 * occurrence. If its <idx> member is 0, the header is searched from the
501 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100502 * 1 when it finds a value, and 0 when there is no more. It is designed to work
503 * with headers defined as comma-separated lists. As a special case, if ctx->val
504 * is NULL when searching for a new values of a header, the current header is
505 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200506 */
507int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100508 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200509 struct hdr_ctx *ctx)
510{
Willy Tarreau68085d82010-01-18 14:54:04 +0100511 char *eol, *sov;
512 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200513
Willy Tarreau68085d82010-01-18 14:54:04 +0100514 cur_idx = ctx->idx;
515 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200516 /* We have previously returned a value, let's search
517 * another one on the same line.
518 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200519 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200520 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100521 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200522 eol = sol + idx->v[cur_idx].len;
523
524 if (sov >= eol)
525 /* no more values in this header */
526 goto next_hdr;
527
Willy Tarreau68085d82010-01-18 14:54:04 +0100528 /* values remaining for this header, skip the comma but save it
529 * for later use (eg: for header deletion).
530 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 sov++;
532 while (sov < eol && http_is_lws[(unsigned char)*sov])
533 sov++;
534
535 goto return_hdr;
536 }
537
538 /* first request for this header */
539 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100540 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200542 while (cur_idx) {
543 eol = sol + idx->v[cur_idx].len;
544
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200545 if (len == 0) {
546 /* No argument was passed, we want any header.
547 * To achieve this, we simply build a fake request. */
548 while (sol + len < eol && sol[len] != ':')
549 len++;
550 name = sol;
551 }
552
Willy Tarreau33a7e692007-06-10 19:45:56 +0200553 if ((len < eol - sol) &&
554 (sol[len] == ':') &&
555 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100556 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200557 sov = sol + len + 1;
558 while (sov < eol && http_is_lws[(unsigned char)*sov])
559 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100560
Willy Tarreau33a7e692007-06-10 19:45:56 +0200561 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100562 ctx->prev = old_idx;
563 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200564 ctx->idx = cur_idx;
565 ctx->val = sov - sol;
566
567 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200568 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200569 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200570 eol--;
571 ctx->tws++;
572 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200573 ctx->vlen = eol - sov;
574 return 1;
575 }
576 next_hdr:
577 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100578 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200579 cur_idx = idx->v[cur_idx].next;
580 }
581 return 0;
582}
583
584int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100585 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200586 struct hdr_ctx *ctx)
587{
588 return http_find_header2(name, strlen(name), sol, idx, ctx);
589}
590
Willy Tarreau68085d82010-01-18 14:54:04 +0100591/* Remove one value of a header. This only works on a <ctx> returned by one of
592 * the http_find_header functions. The value is removed, as well as surrounding
593 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100594 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100595 * message <msg>. The new index is returned. If it is zero, it means there is
596 * no more header, so any processing may stop. The ctx is always left in a form
597 * that can be handled by http_find_header2() to find next occurrence.
598 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100599int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100600{
601 int cur_idx = ctx->idx;
602 char *sol = ctx->line;
603 struct hdr_idx_elem *hdr;
604 int delta, skip_comma;
605
606 if (!cur_idx)
607 return 0;
608
609 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200610 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100611 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200612 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100613 http_msg_move_end(msg, delta);
614 idx->used--;
615 hdr->len = 0; /* unused entry */
616 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100617 if (idx->tail == ctx->idx)
618 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100619 ctx->idx = ctx->prev; /* walk back to the end of previous header */
620 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
621 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200622 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100623 return ctx->idx;
624 }
625
626 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200627 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
628 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100629 */
630
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200631 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200632 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200633 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 NULL, 0);
635 hdr->len += delta;
636 http_msg_move_end(msg, delta);
637 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200638 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100639 return ctx->idx;
640}
641
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100642/* This function handles a server error at the stream interface level. The
643 * stream interface is assumed to be already in a closed state. An optional
644 * message is copied into the input buffer, and an HTTP status code stored.
645 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100646 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100648static void http_server_error(struct session *t, struct stream_interface *si,
649 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200650{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200651 channel_auto_read(si->ob);
652 channel_abort(si->ob);
653 channel_auto_close(si->ob);
654 channel_erase(si->ob);
655 channel_auto_close(si->ib);
656 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100657 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100658 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200659 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660 }
661 if (!(t->flags & SN_ERR_MASK))
662 t->flags |= err;
663 if (!(t->flags & SN_FINST_MASK))
664 t->flags |= finst;
665}
666
Willy Tarreau80587432006-12-24 17:47:20 +0100667/* This function returns the appropriate error location for the given session
668 * and message.
669 */
670
Willy Tarreau783f2582012-09-04 12:19:04 +0200671struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100672{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200673 if (s->be->errmsg[msgnum].str)
674 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100675 else if (s->fe->errmsg[msgnum].str)
676 return &s->fe->errmsg[msgnum];
677 else
678 return &http_err_chunks[msgnum];
679}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200680
Willy Tarreau53b6c742006-12-17 13:37:46 +0100681/*
682 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
683 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
684 */
685static http_meth_t find_http_meth(const char *str, const int len)
686{
687 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100688 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100689
690 m = ((unsigned)*str - 'A');
691
692 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100693 for (h = http_methods[m]; h->len > 0; h++) {
694 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100695 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100696 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100698 };
699 return HTTP_METH_OTHER;
700 }
701 return HTTP_METH_NONE;
702
703}
704
Willy Tarreau21d2af32008-02-14 20:25:24 +0100705/* Parse the URI from the given transaction (which is assumed to be in request
706 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
707 * It is returned otherwise.
708 */
709static char *
710http_get_path(struct http_txn *txn)
711{
712 char *ptr, *end;
713
Willy Tarreau9b28e032012-10-12 23:49:43 +0200714 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100715 end = ptr + txn->req.sl.rq.u_l;
716
717 if (ptr >= end)
718 return NULL;
719
720 /* RFC2616, par. 5.1.2 :
721 * Request-URI = "*" | absuri | abspath | authority
722 */
723
724 if (*ptr == '*')
725 return NULL;
726
727 if (isalpha((unsigned char)*ptr)) {
728 /* this is a scheme as described by RFC3986, par. 3.1 */
729 ptr++;
730 while (ptr < end &&
731 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
732 ptr++;
733 /* skip '://' */
734 if (ptr == end || *ptr++ != ':')
735 return NULL;
736 if (ptr == end || *ptr++ != '/')
737 return NULL;
738 if (ptr == end || *ptr++ != '/')
739 return NULL;
740 }
741 /* skip [user[:passwd]@]host[:[port]] */
742
743 while (ptr < end && *ptr != '/')
744 ptr++;
745
746 if (ptr == end)
747 return NULL;
748
749 /* OK, we got the '/' ! */
750 return ptr;
751}
752
Willy Tarreauefb453c2008-10-26 20:49:47 +0100753/* Returns a 302 for a redirectable request. This may only be called just after
754 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
755 * left unchanged and will follow normal proxy processing.
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200756 * NOTE: this function is designed to support being called once data are scheduled
757 * for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100758 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100760{
761 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100762 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100763 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200764 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100765
766 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100767 trash.len = strlen(HTTP_302);
768 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100769
Willy Tarreau827aee92011-03-10 16:55:02 +0100770 srv = target_srv(&s->target);
771
Willy Tarreauefb453c2008-10-26 20:49:47 +0100772 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100773 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100774 return;
775
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100776 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100778 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
779 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100780 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100781
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200782 /* 3: add the request URI. Since it was already forwarded, we need
783 * to temporarily rewind the buffer.
784 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100785 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200786 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200787
Willy Tarreauefb453c2008-10-26 20:49:47 +0100788 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200789 len = buffer_count(s->req->buf, path, b_ptr(s->req->buf, txn->req.sl.rq.u + txn->req.sl.rq.u_l));
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200790
Willy Tarreau9b28e032012-10-12 23:49:43 +0200791 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200792
Willy Tarreauefb453c2008-10-26 20:49:47 +0100793 if (!path)
794 return;
795
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100796 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100797 return;
798
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100799 memcpy(trash.str + trash.len, path, len);
800 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100801
802 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100803 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
804 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100805 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100806 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
807 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100808 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100809
810 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200811 si_shutr(si);
812 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100813 si->err_type = SI_ET_NONE;
814 si->err_loc = NULL;
815 si->state = SI_ST_CLO;
816
817 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100818 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100819
820 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100821 if (srv)
822 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100823}
824
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100825/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100826 * that the server side is closed. Note that err_type is actually a
827 * bitmask, where almost only aborts may be cumulated with other
828 * values. We consider that aborted operations are more important
829 * than timeouts or errors due to the fact that nobody else in the
830 * logs might explain incomplete retries. All others should avoid
831 * being cumulated. It should normally not be possible to have multiple
832 * aborts at once, but just in case, the first one in sequence is reported.
833 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100834void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100835{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100836 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837
838 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100839 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200840 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100841 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100842 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200843 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100844 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100845 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200846 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100847 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100848 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200849 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100850 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100851 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200852 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100853 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100854 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200855 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100856 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100857 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200858 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100859}
860
Willy Tarreau42250582007-04-01 01:30:43 +0200861extern const char sess_term_cond[8];
862extern const char sess_fin_state[8];
863extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200864struct pool_head *pool2_requri;
Willy Tarreau086b3b42007-05-13 21:45:51 +0200865struct pool_head *pool2_capture;
William Lallemanda73203e2012-03-12 12:48:57 +0100866struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100867
Willy Tarreau117f59e2007-03-04 18:17:17 +0100868/*
869 * Capture headers from message starting at <som> according to header list
870 * <cap_hdr>, and fill the <idx> structure appropriately.
871 */
872void capture_headers(char *som, struct hdr_idx *idx,
873 char **cap, struct cap_hdr *cap_hdr)
874{
875 char *eol, *sol, *col, *sov;
876 int cur_idx;
877 struct cap_hdr *h;
878 int len;
879
880 sol = som + hdr_idx_first_pos(idx);
881 cur_idx = hdr_idx_first_idx(idx);
882
883 while (cur_idx) {
884 eol = sol + idx->v[cur_idx].len;
885
886 col = sol;
887 while (col < eol && *col != ':')
888 col++;
889
890 sov = col + 1;
891 while (sov < eol && http_is_lws[(unsigned char)*sov])
892 sov++;
893
894 for (h = cap_hdr; h; h = h->next) {
895 if ((h->namelen == col - sol) &&
896 (strncasecmp(sol, h->name, h->namelen) == 0)) {
897 if (cap[h->index] == NULL)
898 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200899 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100900
901 if (cap[h->index] == NULL) {
902 Alert("HTTP capture : out of memory.\n");
903 continue;
904 }
905
906 len = eol - sov;
907 if (len > h->len)
908 len = h->len;
909
910 memcpy(cap[h->index], sov, len);
911 cap[h->index][len]=0;
912 }
913 }
914 sol = eol + idx->v[cur_idx].cr + 1;
915 cur_idx = idx->v[cur_idx].next;
916 }
917}
918
919
Willy Tarreau42250582007-04-01 01:30:43 +0200920/* either we find an LF at <ptr> or we jump to <bad>.
921 */
922#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
923
924/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
925 * otherwise to <http_msg_ood> with <state> set to <st>.
926 */
927#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
928 ptr++; \
929 if (likely(ptr < end)) \
930 goto good; \
931 else { \
932 state = (st); \
933 goto http_msg_ood; \
934 } \
935 } while (0)
936
937
Willy Tarreaubaaee002006-06-26 02:48:02 +0200938/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100939 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100940 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
941 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
942 * will give undefined results.
943 * Note that it is upon the caller's responsibility to ensure that ptr < end,
944 * and that msg->sol points to the beginning of the response.
945 * If a complete line is found (which implies that at least one CR or LF is
946 * found before <end>, the updated <ptr> is returned, otherwise NULL is
947 * returned indicating an incomplete line (which does not mean that parts have
948 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
949 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
950 * upon next call.
951 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200952 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100953 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
954 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200955 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100956 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200957const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100958 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100959 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100960{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200961 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100962
Willy Tarreau8973c702007-01-21 23:58:29 +0100963 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100964 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200965 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100966 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
968
969 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100970 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100971 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
972 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100973 state = HTTP_MSG_ERROR;
974 break;
975
Willy Tarreau8973c702007-01-21 23:58:29 +0100976 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200977 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100978 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100979 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100980 goto http_msg_rpcode;
981 }
982 if (likely(HTTP_IS_SPHT(*ptr)))
983 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
984 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100985 state = HTTP_MSG_ERROR;
986 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100987
Willy Tarreau8973c702007-01-21 23:58:29 +0100988 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200989 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 if (likely(!HTTP_IS_LWS(*ptr)))
991 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
992
993 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100994 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100995 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
996 }
997
998 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100999 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001000 http_msg_rsp_reason:
1001 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001002 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001003 msg->sl.st.r_l = 0;
1004 goto http_msg_rpline_eol;
1005
Willy Tarreau8973c702007-01-21 23:58:29 +01001006 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001007 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001008 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001009 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001010 goto http_msg_rpreason;
1011 }
1012 if (likely(HTTP_IS_SPHT(*ptr)))
1013 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1014 /* so it's a CR/LF, so there is no reason phrase */
1015 goto http_msg_rsp_reason;
1016
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001018 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001019 if (likely(!HTTP_IS_CRLF(*ptr)))
1020 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001021 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001022 http_msg_rpline_eol:
1023 /* We have seen the end of line. Note that we do not
1024 * necessarily have the \n yet, but at least we know that we
1025 * have EITHER \r OR \n, otherwise the response would not be
1026 * complete. We can then record the response length and return
1027 * to the caller which will be able to register it.
1028 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001029 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001030 return ptr;
1031
1032#ifdef DEBUG_FULL
1033 default:
1034 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1035 exit(1);
1036#endif
1037 }
1038
1039 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001040 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001041 if (ret_state)
1042 *ret_state = state;
1043 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001044 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001045 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001046}
1047
Willy Tarreau8973c702007-01-21 23:58:29 +01001048/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001049 * This function parses a request line between <ptr> and <end>, starting with
1050 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1051 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1052 * will give undefined results.
1053 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1054 * and that msg->sol points to the beginning of the request.
1055 * If a complete line is found (which implies that at least one CR or LF is
1056 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1057 * returned indicating an incomplete line (which does not mean that parts have
1058 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1059 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1060 * upon next call.
1061 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001062 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001063 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1064 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001065 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001066 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001067const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001068 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001069 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001070{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001071 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001072
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001074 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001075 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001076 if (likely(HTTP_IS_TOKEN(*ptr)))
1077 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001078
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001079 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001080 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001081 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1082 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001083
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001084 if (likely(HTTP_IS_CRLF(*ptr))) {
1085 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001086 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001088 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001089 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001090 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001091 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001092 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 msg->sl.rq.v_l = 0;
1094 goto http_msg_rqline_eol;
1095 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001096 state = HTTP_MSG_ERROR;
1097 break;
1098
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001099 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001100 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001102 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 goto http_msg_rquri;
1104 }
1105 if (likely(HTTP_IS_SPHT(*ptr)))
1106 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1107 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1108 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001109
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001110 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001111 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001112 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001114
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001116 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1118 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001119
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001120 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001121 /* non-ASCII chars are forbidden unless option
1122 * accept-invalid-http-request is enabled in the frontend.
1123 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001124 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001125 if (msg->err_pos < -1)
1126 goto invalid_char;
1127 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001128 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001129 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1130 }
1131
1132 if (likely(HTTP_IS_CRLF(*ptr))) {
1133 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1134 goto http_msg_req09_uri_e;
1135 }
1136
1137 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001138 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001139 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001140 state = HTTP_MSG_ERROR;
1141 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001142
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001143 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001144 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001145 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001146 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001147 goto http_msg_rqver;
1148 }
1149 if (likely(HTTP_IS_SPHT(*ptr)))
1150 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1151 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1152 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001153
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001154 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001155 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001156 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001158
1159 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001160 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001161 http_msg_rqline_eol:
1162 /* We have seen the end of line. Note that we do not
1163 * necessarily have the \n yet, but at least we know that we
1164 * have EITHER \r OR \n, otherwise the request would not be
1165 * complete. We can then record the request length and return
1166 * to the caller which will be able to register it.
1167 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001168 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001169 return ptr;
1170 }
1171
1172 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001173 state = HTTP_MSG_ERROR;
1174 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001176#ifdef DEBUG_FULL
1177 default:
1178 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1179 exit(1);
1180#endif
1181 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001182
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001183 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001184 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001185 if (ret_state)
1186 *ret_state = state;
1187 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001188 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001189 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001191
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001192/*
1193 * Returns the data from Authorization header. Function may be called more
1194 * than once so data is stored in txn->auth_data. When no header is found
1195 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1196 * searching again for something we are unable to find anyway.
1197 */
1198
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001199char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001200
1201int
1202get_http_auth(struct session *s)
1203{
1204
1205 struct http_txn *txn = &s->txn;
1206 struct chunk auth_method;
1207 struct hdr_ctx ctx;
1208 char *h, *p;
1209 int len;
1210
1211#ifdef DEBUG_AUTH
1212 printf("Auth for session %p: %d\n", s, txn->auth.method);
1213#endif
1214
1215 if (txn->auth.method == HTTP_AUTH_WRONG)
1216 return 0;
1217
1218 if (txn->auth.method)
1219 return 1;
1220
1221 txn->auth.method = HTTP_AUTH_WRONG;
1222
1223 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001224
1225 if (txn->flags & TX_USE_PX_CONN) {
1226 h = "Proxy-Authorization";
1227 len = strlen(h);
1228 } else {
1229 h = "Authorization";
1230 len = strlen(h);
1231 }
1232
Willy Tarreau9b28e032012-10-12 23:49:43 +02001233 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001234 return 0;
1235
1236 h = ctx.line + ctx.val;
1237
1238 p = memchr(h, ' ', ctx.vlen);
1239 if (!p || p == h)
1240 return 0;
1241
1242 chunk_initlen(&auth_method, h, 0, p-h);
1243 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1244
1245 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1246
1247 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001248 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001249
1250 if (len < 0)
1251 return 0;
1252
1253
1254 get_http_auth_buff[len] = '\0';
1255
1256 p = strchr(get_http_auth_buff, ':');
1257
1258 if (!p)
1259 return 0;
1260
1261 txn->auth.user = get_http_auth_buff;
1262 *p = '\0';
1263 txn->auth.pass = p+1;
1264
1265 txn->auth.method = HTTP_AUTH_BASIC;
1266 return 1;
1267 }
1268
1269 return 0;
1270}
1271
Willy Tarreau58f10d72006-12-04 02:26:12 +01001272
Willy Tarreau8973c702007-01-21 23:58:29 +01001273/*
1274 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001275 * depending on the initial msg->msg_state. The caller is responsible for
1276 * ensuring that the message does not wrap. The function can be preempted
1277 * everywhere when data are missing and recalled at the exact same location
1278 * with no information loss. The message may even be realigned between two
1279 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001280 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001281 * fields. Note that msg->sol will be initialized after completing the first
1282 * state, so that none of the msg pointers has to be initialized prior to the
1283 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001284 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001285void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001286{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001287 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001288 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001289 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001290
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001291 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001292 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001293 ptr = buf->p + msg->next;
1294 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001295
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001296 if (unlikely(ptr >= end))
1297 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001298
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001299 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001300 /*
1301 * First, states that are specific to the response only.
1302 * We check them first so that request and headers are
1303 * closer to each other (accessed more often).
1304 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001305 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001306 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001307 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001308 /* we have a start of message, but we have to check
1309 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001310 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001311 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001312 if (unlikely(ptr != buf->p)) {
1313 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001314 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001315 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001316 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001317 }
Willy Tarreau26927362012-05-18 23:22:52 +02001318 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001319 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001320 hdr_idx_init(idx);
1321 state = HTTP_MSG_RPVER;
1322 goto http_msg_rpver;
1323 }
1324
1325 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1326 goto http_msg_invalid;
1327
1328 if (unlikely(*ptr == '\n'))
1329 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1330 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1331 /* stop here */
1332
Willy Tarreau8973c702007-01-21 23:58:29 +01001333 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001334 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001335 EXPECT_LF_HERE(ptr, http_msg_invalid);
1336 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1337 /* stop here */
1338
Willy Tarreau8973c702007-01-21 23:58:29 +01001339 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001340 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001341 case HTTP_MSG_RPVER_SP:
1342 case HTTP_MSG_RPCODE:
1343 case HTTP_MSG_RPCODE_SP:
1344 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001345 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001346 state, ptr, end,
1347 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001348 if (unlikely(!ptr))
1349 return;
1350
1351 /* we have a full response and we know that we have either a CR
1352 * or an LF at <ptr>.
1353 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001354 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1355
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001356 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001357 if (likely(*ptr == '\r'))
1358 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1359 goto http_msg_rpline_end;
1360
Willy Tarreau8973c702007-01-21 23:58:29 +01001361 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001362 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001363 /* msg->sol must point to the first of CR or LF. */
1364 EXPECT_LF_HERE(ptr, http_msg_invalid);
1365 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1366 /* stop here */
1367
1368 /*
1369 * Second, states that are specific to the request only
1370 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001371 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001372 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001373 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001374 /* we have a start of message, but we have to check
1375 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001376 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001377 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001378 if (likely(ptr != buf->p)) {
1379 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001380 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001381 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001382 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001383 }
Willy Tarreau26927362012-05-18 23:22:52 +02001384 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001385 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001386 state = HTTP_MSG_RQMETH;
1387 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001388 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001389
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001390 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1391 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001392
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001393 if (unlikely(*ptr == '\n'))
1394 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1395 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001396 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001397
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001398 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001399 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001400 EXPECT_LF_HERE(ptr, http_msg_invalid);
1401 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001402 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001405 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001406 case HTTP_MSG_RQMETH_SP:
1407 case HTTP_MSG_RQURI:
1408 case HTTP_MSG_RQURI_SP:
1409 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001410 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001411 state, ptr, end,
1412 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001413 if (unlikely(!ptr))
1414 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001415
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001416 /* we have a full request and we know that we have either a CR
1417 * or an LF at <ptr>.
1418 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001419 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001420
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001421 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001422 if (likely(*ptr == '\r'))
1423 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001424 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001425
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001426 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001427 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001428 /* check for HTTP/0.9 request : no version information available.
1429 * msg->sol must point to the first of CR or LF.
1430 */
1431 if (unlikely(msg->sl.rq.v_l == 0))
1432 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001433
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001434 EXPECT_LF_HERE(ptr, http_msg_invalid);
1435 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001436 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001437
Willy Tarreau8973c702007-01-21 23:58:29 +01001438 /*
1439 * Common states below
1440 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001441 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001442 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001443 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001444 if (likely(!HTTP_IS_CRLF(*ptr))) {
1445 goto http_msg_hdr_name;
1446 }
1447
1448 if (likely(*ptr == '\r'))
1449 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1450 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001451
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001452 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001453 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001454 /* assumes msg->sol points to the first char */
1455 if (likely(HTTP_IS_TOKEN(*ptr)))
1456 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001457
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001458 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001459 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001460
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001461 if (likely(msg->err_pos < -1) || *ptr == '\n')
1462 goto http_msg_invalid;
1463
1464 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001465 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001466
1467 /* and we still accept this non-token character */
1468 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001469
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001470 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001471 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001472 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 if (likely(HTTP_IS_SPHT(*ptr)))
1474 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001475
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001476 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001477 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001478
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001479 if (likely(!HTTP_IS_CRLF(*ptr))) {
1480 goto http_msg_hdr_val;
1481 }
1482
1483 if (likely(*ptr == '\r'))
1484 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1485 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001486
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001488 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001489 EXPECT_LF_HERE(ptr, http_msg_invalid);
1490 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001491
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001492 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001493 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001494 if (likely(HTTP_IS_SPHT(*ptr))) {
1495 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001496 for (; buf->p + msg->sov < ptr; msg->sov++)
1497 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001498 goto http_msg_hdr_l1_sp;
1499 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001500 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001501 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001502 goto http_msg_complete_header;
1503
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001504 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001505 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001506 /* assumes msg->sol points to the first char, and msg->sov
1507 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 */
1509 if (likely(!HTTP_IS_CRLF(*ptr)))
1510 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001511
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001512 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001513 /* Note: we could also copy eol into ->eoh so that we have the
1514 * real header end in case it ends with lots of LWS, but is this
1515 * really needed ?
1516 */
1517 if (likely(*ptr == '\r'))
1518 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1519 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001520
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001521 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001522 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001523 EXPECT_LF_HERE(ptr, http_msg_invalid);
1524 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001526 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001527 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001528 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1529 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001530 for (; buf->p + msg->eol < ptr; msg->eol++)
1531 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001532 goto http_msg_hdr_val;
1533 }
1534 http_msg_complete_header:
1535 /*
1536 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001537 * Assumes msg->sol points to the first char, msg->sov points
1538 * to the first character of the value and msg->eol to the
1539 * first CR or LF so we know how the line ends. We insert last
1540 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001541 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001542 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001543 idx, idx->tail) < 0))
1544 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001545
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001546 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001547 if (likely(!HTTP_IS_CRLF(*ptr))) {
1548 goto http_msg_hdr_name;
1549 }
1550
1551 if (likely(*ptr == '\r'))
1552 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1553 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001554
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001556 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 /* Assumes msg->sol points to the first of either CR or LF */
1558 EXPECT_LF_HERE(ptr, http_msg_invalid);
1559 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001560 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001561 msg->eoh = msg->sol;
1562 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001563 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001564 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001565
1566 case HTTP_MSG_ERROR:
1567 /* this may only happen if we call http_msg_analyser() twice with an error */
1568 break;
1569
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001570#ifdef DEBUG_FULL
1571 default:
1572 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1573 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001574#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001575 }
1576 http_msg_ood:
1577 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001578 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001579 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001580 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001581
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001582 http_msg_invalid:
1583 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001584 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001585 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001586 return;
1587}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001588
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001589/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1590 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1591 * nothing is done and 1 is returned.
1592 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001593static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001594{
1595 int delta;
1596 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001597 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001598
1599 if (msg->sl.rq.v_l != 0)
1600 return 1;
1601
Willy Tarreau9b28e032012-10-12 23:49:43 +02001602 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001603 delta = 0;
1604
1605 if (msg->sl.rq.u_l == 0) {
1606 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001607 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001609 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001610 }
1611 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001612 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001613 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001614 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001615 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001616 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001617 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001618 NULL, NULL);
1619 if (unlikely(!cur_end))
1620 return 0;
1621
1622 /* we have a full HTTP/1.0 request now and we know that
1623 * we have either a CR or an LF at <ptr>.
1624 */
1625 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1626 return 1;
1627}
1628
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001629/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001630 * and "keep-alive" values. If we already know that some headers may safely
1631 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001632 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1633 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
1634 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1635 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1636 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001637 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001638 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001639void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001640{
Willy Tarreau5b154472009-12-21 20:11:07 +01001641 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001642 const char *hdr_val = "Connection";
1643 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001644
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001645 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001646 return;
1647
Willy Tarreau88d349d2010-01-25 12:15:43 +01001648 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1649 hdr_val = "Proxy-Connection";
1650 hdr_len = 16;
1651 }
1652
Willy Tarreau5b154472009-12-21 20:11:07 +01001653 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001654 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001655 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001656 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1657 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001658 if (to_del & 2)
1659 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001660 else
1661 txn->flags |= TX_CON_KAL_SET;
1662 }
1663 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1664 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001665 if (to_del & 1)
1666 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001667 else
1668 txn->flags |= TX_CON_CLO_SET;
1669 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001670 }
1671
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001672 txn->flags |= TX_HDR_CONN_PRS;
1673 return;
1674}
Willy Tarreau5b154472009-12-21 20:11:07 +01001675
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001676/* Apply desired changes on the Connection: header. Values may be removed and/or
1677 * added depending on the <wanted> flags, which are exclusively composed of
1678 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1679 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1680 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001681void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001682{
1683 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001684 const char *hdr_val = "Connection";
1685 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001686
1687 ctx.idx = 0;
1688
Willy Tarreau88d349d2010-01-25 12:15:43 +01001689
1690 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1691 hdr_val = "Proxy-Connection";
1692 hdr_len = 16;
1693 }
1694
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001695 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001696 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001697 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1698 if (wanted & TX_CON_KAL_SET)
1699 txn->flags |= TX_CON_KAL_SET;
1700 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001701 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001702 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001703 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1704 if (wanted & TX_CON_CLO_SET)
1705 txn->flags |= TX_CON_CLO_SET;
1706 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001707 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001708 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001709 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001710
1711 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1712 return;
1713
1714 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1715 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001716 hdr_val = "Connection: close";
1717 hdr_len = 17;
1718 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1719 hdr_val = "Proxy-Connection: close";
1720 hdr_len = 23;
1721 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001722 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001723 }
1724
1725 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1726 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001727 hdr_val = "Connection: keep-alive";
1728 hdr_len = 22;
1729 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1730 hdr_val = "Proxy-Connection: keep-alive";
1731 hdr_len = 28;
1732 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001733 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001734 }
1735 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001736}
1737
Willy Tarreaua458b672012-03-05 11:17:50 +01001738/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001739 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001740 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001741 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001742 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001743 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001744static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001745{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001746 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001747 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001748 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001749 const char *end = buf->data + buf->size;
1750 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001751 unsigned int chunk = 0;
1752
1753 /* The chunk size is in the following form, though we are only
1754 * interested in the size and CRLF :
1755 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1756 */
1757 while (1) {
1758 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001759 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001760 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001761 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001762 if (c < 0) /* not a hex digit anymore */
1763 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001765 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001766 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001767 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001768 chunk = (chunk << 4) + c;
1769 }
1770
Willy Tarreaud98cf932009-12-27 22:54:55 +01001771 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001772 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001773 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001774
1775 while (http_is_spht[(unsigned char)*ptr]) {
1776 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001777 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001778 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001779 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001780 }
1781
Willy Tarreaud98cf932009-12-27 22:54:55 +01001782 /* Up to there, we know that at least one byte is present at *ptr. Check
1783 * for the end of chunk size.
1784 */
1785 while (1) {
1786 if (likely(HTTP_IS_CRLF(*ptr))) {
1787 /* we now have a CR or an LF at ptr */
1788 if (likely(*ptr == '\r')) {
1789 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001790 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001791 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001792 return 0;
1793 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001794
Willy Tarreaud98cf932009-12-27 22:54:55 +01001795 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001796 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001797 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001798 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001799 /* done */
1800 break;
1801 }
1802 else if (*ptr == ';') {
1803 /* chunk extension, ends at next CRLF */
1804 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001805 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001806 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001807 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001808
1809 while (!HTTP_IS_CRLF(*ptr)) {
1810 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001811 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001812 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001813 return 0;
1814 }
1815 /* we have a CRLF now, loop above */
1816 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001817 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001818 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001819 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 }
1821
Willy Tarreaud98cf932009-12-27 22:54:55 +01001822 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001823 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001824 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001825 */
Willy Tarreaub8ffd372012-09-27 15:08:56 +02001826 if (ptr < ptr_old)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001827 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001828 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001829 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001830 msg->chunk_len = chunk;
1831 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001832 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001833 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001834 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001835 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001836 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001837}
1838
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001839/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001840 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001841 * the trailers is found, it is automatically scheduled to be forwarded,
1842 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1843 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001844 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001845 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001846 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001847 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1848 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001849 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001850 * matches the length of trailers already parsed and not forwarded. It is also
1851 * important to note that this function is designed to be able to parse wrapped
1852 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001853 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001854static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001856 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001857
Willy Tarreaua458b672012-03-05 11:17:50 +01001858 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001860 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001861 const char *ptr = b_ptr(buf, msg->next);
1862 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001863 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001864
1865 /* scan current line and stop at LF or CRLF */
1866 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001867 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001868 return 0;
1869
1870 if (*ptr == '\n') {
1871 if (!p1)
1872 p1 = ptr;
1873 p2 = ptr;
1874 break;
1875 }
1876
1877 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001878 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001879 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001880 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001881 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882 p1 = ptr;
1883 }
1884
1885 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001886 if (ptr >= buf->data + buf->size)
1887 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001888 }
1889
1890 /* after LF; point to beginning of next line */
1891 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001892 if (p2 >= buf->data + buf->size)
1893 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001894
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001895 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001896 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001897 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001898
1899 /* schedule this line for forwarding */
1900 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001901 if (msg->sov >= buf->size)
1902 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001903
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001905 /* LF/CRLF at beginning of line => end of trailers at p2.
1906 * Everything was scheduled for forwarding, there's nothing
1907 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001908 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001909 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001910 msg->msg_state = HTTP_MSG_DONE;
1911 return 1;
1912 }
1913 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001914 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001915 }
1916}
1917
Willy Tarreau54d23df2012-10-25 19:04:45 +02001918/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001919 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001920 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001921 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1923 * not enough data are available, the function does not change anything and
1924 * returns zero. If a parse error is encountered, the function returns < 0 and
1925 * does not change anything. Note: this function is designed to parse wrapped
1926 * CRLF at the end of the buffer.
1927 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001928static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001929{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001930 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001931 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001932 int bytes;
1933
1934 /* NB: we'll check data availabilty at the end. It's not a
1935 * problem because whatever we match first will be checked
1936 * against the correct length.
1937 */
1938 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001939 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 if (*ptr == '\r') {
1941 bytes++;
1942 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001943 if (ptr >= buf->data + buf->size)
1944 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001945 }
1946
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001947 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 return 0;
1949
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001950 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001951 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001952 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001953 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001954
1955 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001956 if (ptr >= buf->data + buf->size)
1957 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001958 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1959 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001960 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001961 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1962 return 1;
1963}
Willy Tarreau5b154472009-12-21 20:11:07 +01001964
William Lallemand82fe75c2012-10-23 10:25:10 +02001965
1966/*
1967 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001968 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001969int select_compression_request_header(struct session *s, struct buffer *req)
1970{
1971 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001972 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001973 struct hdr_ctx ctx;
1974 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001975 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001976
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001977 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1978 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001979 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1980 */
1981 ctx.idx = 0;
1982 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
1983 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001984 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
1985 (ctx.vlen < 31 ||
1986 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
1987 ctx.line[ctx.val + 30] < '6' ||
1988 (ctx.line[ctx.val + 30] == '6' &&
1989 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
1990 s->comp_algo = NULL;
1991 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02001992 }
1993
William Lallemand82fe75c2012-10-23 10:25:10 +02001994 ctx.idx = 0;
1995 /* no compression when Cache-Control: no-transform found */
1996 while (http_find_header2("Cache-Control", 13, req->p, &txn->hdr_idx, &ctx)) {
1997 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) {
1998 s->comp_algo = NULL;
1999 return 0;
2000 }
2001 }
2002
2003 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002004 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002005 ctx.idx = 0;
2006 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002007 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002008 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2009 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002010
2011 /* remove all occurrences of the header when "compression offload" is set */
2012
2013 if ((s->be->comp && s->be->comp->offload) ||
2014 (s->fe->comp && s->fe->comp->offload)) {
2015 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2016 ctx.idx = 0;
2017 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2018 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2019 }
2020 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002021 return 1;
2022 }
2023 }
2024 }
2025 }
2026
2027 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002028 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2029 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002030 if (comp_algo->add_data == identity_add_data) {
2031 s->comp_algo = comp_algo;
2032 return 1;
2033 }
2034 }
2035 }
2036
2037 s->comp_algo = NULL;
2038
2039 return 0;
2040}
2041
2042/*
2043 * Selects a comression algorithm depending of the server response.
2044 */
2045int select_compression_response_header(struct session *s, struct buffer *res)
2046{
2047 struct http_txn *txn = &s->txn;
2048 struct http_msg *msg = &txn->rsp;
2049 struct hdr_ctx ctx;
2050 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002051
2052 /* no common compression algorithm was found in request header */
2053 if (s->comp_algo == NULL)
2054 goto fail;
2055
2056 /* HTTP < 1.1 should not be compressed */
2057 if (!(msg->flags & HTTP_MSGF_VER_11))
2058 goto fail;
2059
William Lallemand82fe75c2012-10-23 10:25:10 +02002060 ctx.idx = 0;
2061
2062 /* Content-Length is null */
2063 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2064 goto fail;
2065
2066 /* content is already compressed */
2067 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2068 goto fail;
2069
2070 comp_type = NULL;
2071
2072 /* if there was a compression content-type option in the backend or the frontend
2073 * The backend have priority.
2074 */
2075 if ((s->be->comp && (comp_type = s->be->comp->types)) || (s->fe->comp && (comp_type = s->fe->comp->types))) {
2076 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2077 for (; comp_type; comp_type = comp_type->next) {
2078 if (strncmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
2079 /* this Content-Type should be compressed */
2080 break;
2081 }
2082 }
2083 /* this Content-Type should not be compressed */
2084 if (comp_type == NULL)
2085 goto fail;
2086 }
2087
2088 ctx.idx = 0;
2089
William Lallemandd85f9172012-11-09 17:05:39 +01002090 /* limit compression rate */
2091 if (global.comp_rate_lim > 0)
2092 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2093 goto fail;
2094
William Lallemand4c49fae2012-11-07 15:00:23 +01002095 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002096 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002097 goto fail;
2098
William Lallemandf3747832012-11-09 12:33:10 +01002099 s->comp_ctx.cur_lvl = global.tune.comp_maxlevel;
2100
William Lallemand82fe75c2012-10-23 10:25:10 +02002101 /* remove Content-Length header */
2102 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2103 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2104
2105 /* add Transfer-Encoding header */
2106 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2107 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2108
2109 /*
2110 * Add Content-Encoding header when it's not identity encoding.
2111 * RFC 2616 : Identity encoding: This content-coding is used only in the
2112 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2113 * header.
2114 */
2115 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002116 trash.len = 18;
2117 memcpy(trash.str, "Content-Encoding: ", trash.len);
2118 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2119 trash.len += s->comp_algo->name_len;
2120 trash.str[trash.len] = '\0';
2121 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002122 }
2123
William Lallemand82fe75c2012-10-23 10:25:10 +02002124 return 1;
2125
2126fail:
2127 if (s->comp_algo) {
William Lallemand1c2d6222012-10-30 15:52:53 +01002128 s->comp_algo->end(&s->comp_ctx);
William Lallemand82fe75c2012-10-23 10:25:10 +02002129 s->comp_algo = NULL;
2130 }
2131 return 0;
2132}
2133
2134
Willy Tarreaud787e662009-07-07 10:14:51 +02002135/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2136 * processing can continue on next analysers, or zero if it either needs more
2137 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2138 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2139 * when it has nothing left to do, and may remove any analyser when it wants to
2140 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002141 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002142int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002143{
Willy Tarreau59234e92008-11-30 23:51:27 +01002144 /*
2145 * We will parse the partial (or complete) lines.
2146 * We will check the request syntax, and also join multi-line
2147 * headers. An index of all the lines will be elaborated while
2148 * parsing.
2149 *
2150 * For the parsing, we use a 28 states FSM.
2151 *
2152 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002153 * req->buf->p = beginning of request
2154 * req->buf->p + msg->eoh = end of processed headers / start of current one
2155 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002156 * msg->eol = end of current header or line (LF or CRLF)
2157 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002158 *
2159 * At end of parsing, we may perform a capture of the error (if any), and
2160 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2161 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2162 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002163 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002164
Willy Tarreau59234e92008-11-30 23:51:27 +01002165 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002166 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002167 struct http_txn *txn = &s->txn;
2168 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002169 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002170
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002171 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 +01002172 now_ms, __FUNCTION__,
2173 s,
2174 req,
2175 req->rex, req->wex,
2176 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002177 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002178 req->analysers);
2179
Willy Tarreau52a0c602009-08-16 22:45:38 +02002180 /* we're speaking HTTP here, so let's speak HTTP to the client */
2181 s->srv_error = http_return_srv_error;
2182
Willy Tarreau83e3af02009-12-28 17:39:57 +01002183 /* There's a protected area at the end of the buffer for rewriting
2184 * purposes. We don't want to start to parse the request if the
2185 * protected area is affected, because we may have to move processed
2186 * data later, which is much more complicated.
2187 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002188 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002189 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002190 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002191 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2192 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2193 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002194 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002195 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002196 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002197 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002198 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002199 return 0;
2200 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002201 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2202 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2203 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002204 }
2205
Willy Tarreau065e8332010-01-08 00:30:20 +01002206 /* Note that we have the same problem with the response ; we
2207 * may want to send a redirect, error or anything which requires
2208 * some spare space. So we'll ensure that we have at least
2209 * maxrewrite bytes available in the response buffer before
2210 * processing that one. This will only affect pipelined
2211 * keep-alive requests.
2212 */
2213 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002214 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002215 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2216 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2217 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002218 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002219 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002220 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002221 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002222 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002223 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002224 return 0;
2225 }
2226 }
2227
Willy Tarreau9b28e032012-10-12 23:49:43 +02002228 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002229 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002230 }
2231
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 /* 1: we might have to print this header in debug mode */
2233 if (unlikely((global.mode & MODE_DEBUG) &&
2234 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002235 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002236 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002237
Willy Tarreau9b28e032012-10-12 23:49:43 +02002238 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002239 /* this is a bit complex : in case of error on the request line,
2240 * we know that rq.l is still zero, so we display only the part
2241 * up to the end of the line (truncated by debug_hdr).
2242 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002243 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002244 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002245
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 sol += hdr_idx_first_pos(&txn->hdr_idx);
2247 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002248
Willy Tarreau59234e92008-11-30 23:51:27 +01002249 while (cur_idx) {
2250 eol = sol + txn->hdr_idx.v[cur_idx].len;
2251 debug_hdr("clihdr", s, sol, eol);
2252 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2253 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002254 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002255 }
2256
Willy Tarreau58f10d72006-12-04 02:26:12 +01002257
Willy Tarreau59234e92008-11-30 23:51:27 +01002258 /*
2259 * Now we quickly check if we have found a full valid request.
2260 * If not so, we check the FD and buffer states before leaving.
2261 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002262 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002263 * requests are checked first. When waiting for a second request
2264 * on a keep-alive session, if we encounter and error, close, t/o,
2265 * we note the error in the session flags but don't set any state.
2266 * Since the error will be noted there, it will not be counted by
2267 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002268 * Last, we may increase some tracked counters' http request errors on
2269 * the cases that are deliberately the client's fault. For instance,
2270 * a timeout or connection reset is not counted as an error. However
2271 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002272 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002273
Willy Tarreau655dce92009-11-08 13:10:58 +01002274 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002275 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002276 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002277 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002278 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002279 session_inc_http_req_ctr(s);
2280 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002281 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002282 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002283 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002284
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 /* 1: Since we are in header mode, if there's no space
2286 * left for headers, we won't be able to free more
2287 * later, so the session will never terminate. We
2288 * must terminate it now.
2289 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002290 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002291 /* FIXME: check if URI is set and return Status
2292 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002293 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002294 session_inc_http_req_ctr(s);
2295 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002296 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002297 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002298 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002299 goto return_bad_req;
2300 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002301
Willy Tarreau59234e92008-11-30 23:51:27 +01002302 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002303 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002304 if (!(s->flags & SN_ERR_MASK))
2305 s->flags |= SN_ERR_CLICL;
2306
Willy Tarreaufcffa692010-01-10 14:21:19 +01002307 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002308 goto failed_keep_alive;
2309
Willy Tarreau59234e92008-11-30 23:51:27 +01002310 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002311 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002312 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002313 session_inc_http_err_ctr(s);
2314 }
2315
Willy Tarreau59234e92008-11-30 23:51:27 +01002316 msg->msg_state = HTTP_MSG_ERROR;
2317 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002318
Willy Tarreauda7ff642010-06-23 11:44:09 +02002319 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002320 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002321 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002322 if (s->listener->counters)
2323 s->listener->counters->failed_req++;
2324
Willy Tarreau59234e92008-11-30 23:51:27 +01002325 if (!(s->flags & SN_FINST_MASK))
2326 s->flags |= SN_FINST_R;
2327 return 0;
2328 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002329
Willy Tarreau59234e92008-11-30 23:51:27 +01002330 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002331 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002332 if (!(s->flags & SN_ERR_MASK))
2333 s->flags |= SN_ERR_CLITO;
2334
Willy Tarreaufcffa692010-01-10 14:21:19 +01002335 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002336 goto failed_keep_alive;
2337
Willy Tarreau59234e92008-11-30 23:51:27 +01002338 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002339 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002340 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002341 session_inc_http_err_ctr(s);
2342 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002343 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002344 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002345 msg->msg_state = HTTP_MSG_ERROR;
2346 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002347
Willy Tarreauda7ff642010-06-23 11:44:09 +02002348 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002349 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002350 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002351 if (s->listener->counters)
2352 s->listener->counters->failed_req++;
2353
Willy Tarreau59234e92008-11-30 23:51:27 +01002354 if (!(s->flags & SN_FINST_MASK))
2355 s->flags |= SN_FINST_R;
2356 return 0;
2357 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002358
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002360 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002361 if (!(s->flags & SN_ERR_MASK))
2362 s->flags |= SN_ERR_CLICL;
2363
Willy Tarreaufcffa692010-01-10 14:21:19 +01002364 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002365 goto failed_keep_alive;
2366
Willy Tarreau4076a152009-04-02 15:18:36 +02002367 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002368 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002370 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002371 msg->msg_state = HTTP_MSG_ERROR;
2372 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002373
Willy Tarreauda7ff642010-06-23 11:44:09 +02002374 session_inc_http_err_ctr(s);
2375 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002376 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002377 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002378 if (s->listener->counters)
2379 s->listener->counters->failed_req++;
2380
Willy Tarreau59234e92008-11-30 23:51:27 +01002381 if (!(s->flags & SN_FINST_MASK))
2382 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002383 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002384 }
2385
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002386 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002387 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2388 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002389#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002390 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002391 /* We need more data, we have to re-enable quick-ack in case we
2392 * previously disabled it, otherwise we might cause the client
2393 * to delay next data.
2394 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002395 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002396 }
2397#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002398
Willy Tarreaufcffa692010-01-10 14:21:19 +01002399 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2400 /* If the client starts to talk, let's fall back to
2401 * request timeout processing.
2402 */
2403 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002404 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002405 }
2406
Willy Tarreau59234e92008-11-30 23:51:27 +01002407 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002408 if (!tick_isset(req->analyse_exp)) {
2409 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2410 (txn->flags & TX_WAIT_NEXT_RQ) &&
2411 tick_isset(s->be->timeout.httpka))
2412 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2413 else
2414 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2415 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002416
Willy Tarreau59234e92008-11-30 23:51:27 +01002417 /* we're not ready yet */
2418 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002419
2420 failed_keep_alive:
2421 /* Here we process low-level errors for keep-alive requests. In
2422 * short, if the request is not the first one and it experiences
2423 * a timeout, read error or shutdown, we just silently close so
2424 * that the client can try again.
2425 */
2426 txn->status = 0;
2427 msg->msg_state = HTTP_MSG_RQBEFORE;
2428 req->analysers = 0;
2429 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002430 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002431 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002432 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002433 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002434
Willy Tarreaud787e662009-07-07 10:14:51 +02002435 /* OK now we have a complete HTTP request with indexed headers. Let's
2436 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002437 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002438 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002439 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002440 * byte after the last LF. msg->sov points to the first byte of data.
2441 * msg->eol cannot be trusted because it may have been left uninitialized
2442 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002443 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002444
Willy Tarreauda7ff642010-06-23 11:44:09 +02002445 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002446 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2447
Willy Tarreaub16a5742010-01-10 14:46:16 +01002448 if (txn->flags & TX_WAIT_NEXT_RQ) {
2449 /* kill the pending keep-alive timeout */
2450 txn->flags &= ~TX_WAIT_NEXT_RQ;
2451 req->analyse_exp = TICK_ETERNITY;
2452 }
2453
2454
Willy Tarreaud787e662009-07-07 10:14:51 +02002455 /* Maybe we found in invalid header name while we were configured not
2456 * to block on that, so we have to capture it now.
2457 */
2458 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002459 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002460
Willy Tarreau59234e92008-11-30 23:51:27 +01002461 /*
2462 * 1: identify the method
2463 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002464 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002465
2466 /* we can make use of server redirect on GET and HEAD */
2467 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2468 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002469
Willy Tarreau59234e92008-11-30 23:51:27 +01002470 /*
2471 * 2: check if the URI matches the monitor_uri.
2472 * We have to do this for every request which gets in, because
2473 * the monitor-uri is defined by the frontend.
2474 */
2475 if (unlikely((s->fe->monitor_uri_len != 0) &&
2476 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002477 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002478 s->fe->monitor_uri,
2479 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002480 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002481 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002482 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002483 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002484
Willy Tarreau59234e92008-11-30 23:51:27 +01002485 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002486 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002487
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002489 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002490 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002491
Willy Tarreau59234e92008-11-30 23:51:27 +01002492 ret = acl_pass(ret);
2493 if (cond->pol == ACL_COND_UNLESS)
2494 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002495
Willy Tarreau59234e92008-11-30 23:51:27 +01002496 if (ret) {
2497 /* we fail this request, let's return 503 service unavail */
2498 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002499 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002500 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002501 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002502 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002503
Willy Tarreau59234e92008-11-30 23:51:27 +01002504 /* nothing to fail, let's reply normaly */
2505 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002506 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002507 goto return_prx_cond;
2508 }
2509
2510 /*
2511 * 3: Maybe we have to copy the original REQURI for the logs ?
2512 * Note: we cannot log anymore if the request has been
2513 * classified as invalid.
2514 */
2515 if (unlikely(s->logs.logwait & LW_REQ)) {
2516 /* we have a complete HTTP request that we must log */
2517 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2518 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002519
Willy Tarreau59234e92008-11-30 23:51:27 +01002520 if (urilen >= REQURI_LEN)
2521 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002522 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002524
Willy Tarreau59234e92008-11-30 23:51:27 +01002525 if (!(s->logs.logwait &= ~LW_REQ))
2526 s->do_log(s);
2527 } else {
2528 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002529 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002530 }
Willy Tarreau06619262006-12-17 08:37:22 +01002531
William Lallemanda73203e2012-03-12 12:48:57 +01002532 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2533 s->unique_id = pool_alloc2(pool2_uniqueid);
2534 }
2535
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002537 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002538 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002539
Willy Tarreau5b154472009-12-21 20:11:07 +01002540 /* ... and check if the request is HTTP/1.1 or above */
2541 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002542 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2543 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2544 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002545 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002546
2547 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002548 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002549
Willy Tarreau88d349d2010-01-25 12:15:43 +01002550 /* if the frontend has "option http-use-proxy-header", we'll check if
2551 * we have what looks like a proxied connection instead of a connection,
2552 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2553 * Note that this is *not* RFC-compliant, however browsers and proxies
2554 * happen to do that despite being non-standard :-(
2555 * We consider that a request not beginning with either '/' or '*' is
2556 * a proxied connection, which covers both "scheme://location" and
2557 * CONNECT ip:port.
2558 */
2559 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002560 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002561 txn->flags |= TX_USE_PX_CONN;
2562
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002563 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002564 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002565
Willy Tarreau59234e92008-11-30 23:51:27 +01002566 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002567 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002568 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002569 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002570
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002571 /* 6: determine the transfer-length.
2572 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2573 * the presence of a message-body in a REQUEST and its transfer length
2574 * must be determined that way (in order of precedence) :
2575 * 1. The presence of a message-body in a request is signaled by the
2576 * inclusion of a Content-Length or Transfer-Encoding header field
2577 * in the request's header fields. When a request message contains
2578 * both a message-body of non-zero length and a method that does
2579 * not define any semantics for that request message-body, then an
2580 * origin server SHOULD either ignore the message-body or respond
2581 * with an appropriate error message (e.g., 413). A proxy or
2582 * gateway, when presented the same request, SHOULD either forward
2583 * the request inbound with the message- body or ignore the
2584 * message-body when determining a response.
2585 *
2586 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2587 * and the "chunked" transfer-coding (Section 6.2) is used, the
2588 * transfer-length is defined by the use of this transfer-coding.
2589 * If a Transfer-Encoding header field is present and the "chunked"
2590 * transfer-coding is not present, the transfer-length is defined
2591 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002592 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002593 * 3. If a Content-Length header field is present, its decimal value in
2594 * OCTETs represents both the entity-length and the transfer-length.
2595 * If a message is received with both a Transfer-Encoding header
2596 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002597 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002598 * 4. By the server closing the connection. (Closing the connection
2599 * cannot be used to indicate the end of a request body, since that
2600 * would leave no possibility for the server to send back a response.)
2601 *
2602 * Whenever a transfer-coding is applied to a message-body, the set of
2603 * transfer-codings MUST include "chunked", unless the message indicates
2604 * it is terminated by closing the connection. When the "chunked"
2605 * transfer-coding is used, it MUST be the last transfer-coding applied
2606 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002607 */
2608
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002609 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002610 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002611 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002612 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002613 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002614 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002615 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2616 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002617 /* bad transfer-encoding (chunked followed by something else) */
2618 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002619 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002620 break;
2621 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002622 }
2623
Willy Tarreau32b47f42009-10-18 20:55:02 +02002624 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002625 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002626 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002627 signed long long cl;
2628
Willy Tarreauad14f752011-09-02 20:33:27 +02002629 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002630 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002631 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002632 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002633
Willy Tarreauad14f752011-09-02 20:33:27 +02002634 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002635 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002636 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002637 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002638
Willy Tarreauad14f752011-09-02 20:33:27 +02002639 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002640 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002641 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002642 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002643
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002644 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002645 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002646 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002647 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002648
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002649 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002650 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002651 }
2652
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002653 /* bodyless requests have a known length */
2654 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002655 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002656
Willy Tarreaud787e662009-07-07 10:14:51 +02002657 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002658 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002659 req->analyse_exp = TICK_ETERNITY;
2660 return 1;
2661
2662 return_bad_req:
2663 /* We centralize bad requests processing here */
2664 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2665 /* we detected a parsing error. We want to archive this request
2666 * in the dedicated proxy area for later troubleshooting.
2667 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002668 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002669 }
2670
2671 txn->req.msg_state = HTTP_MSG_ERROR;
2672 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002673 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002674
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002675 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002676 if (s->listener->counters)
2677 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002678
2679 return_prx_cond:
2680 if (!(s->flags & SN_ERR_MASK))
2681 s->flags |= SN_ERR_PRXCOND;
2682 if (!(s->flags & SN_FINST_MASK))
2683 s->flags |= SN_FINST_R;
2684
2685 req->analysers = 0;
2686 req->analyse_exp = TICK_ETERNITY;
2687 return 0;
2688}
2689
Cyril Bonté70be45d2010-10-12 00:14:35 +02002690/* We reached the stats page through a POST request.
2691 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002692 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002693 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002694int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002695{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002696 struct proxy *px = NULL;
2697 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002698
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002699 char key[LINESIZE];
2700 int action = ST_ADM_ACTION_NONE;
2701 int reprocess = 0;
2702
2703 int total_servers = 0;
2704 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002705
2706 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002707 char *st_cur_param = NULL;
2708 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002709
Willy Tarreau9b28e032012-10-12 23:49:43 +02002710 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002711 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002712
2713 cur_param = next_param = end_params;
2714
Willy Tarreau9b28e032012-10-12 23:49:43 +02002715 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002716 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002717 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002718 return 1;
2719 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002720 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002721 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002722 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002723 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002724 }
2725
2726 *end_params = '\0';
2727
Willy Tarreau295a8372011-03-10 11:25:07 +01002728 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002729
2730 /*
2731 * Parse the parameters in reverse order to only store the last value.
2732 * From the html form, the backend and the action are at the end.
2733 */
2734 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002735 char *value;
2736 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002737
2738 cur_param--;
2739 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002740 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002741 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002742 poffset = (cur_param != first_param ? 1 : 0);
2743 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2744 if ((plen > 0) && (plen <= sizeof(key))) {
2745 strncpy(key, cur_param + poffset, plen);
2746 key[plen - 1] = '\0';
2747 } else {
2748 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2749 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002750 }
2751
2752 /* Parse the value */
2753 value = key;
2754 while (*value != '\0' && *value != '=') {
2755 value++;
2756 }
2757 if (*value == '=') {
2758 /* Ok, a value is found, we can mark the end of the key */
2759 *value++ = '\0';
2760 }
2761
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002762 if (!url_decode(key) || !url_decode(value))
2763 break;
2764
Cyril Bonté70be45d2010-10-12 00:14:35 +02002765 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002766 if (!px && (strcmp(key, "b") == 0)) {
2767 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2768 /* the backend name is unknown or ambiguous (duplicate names) */
2769 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2770 goto out;
2771 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002772 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002773 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002774 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002775 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002776 }
2777 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002778 action = ST_ADM_ACTION_ENABLE;
2779 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002780 else if (strcmp(value, "stop") == 0) {
2781 action = ST_ADM_ACTION_STOP;
2782 }
2783 else if (strcmp(value, "start") == 0) {
2784 action = ST_ADM_ACTION_START;
2785 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002786 else if (strcmp(value, "shutdown") == 0) {
2787 action = ST_ADM_ACTION_SHUTDOWN;
2788 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002789 else {
2790 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2791 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002792 }
2793 }
2794 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002795 if (!(px && action)) {
2796 /*
2797 * Indicates that we'll need to reprocess the parameters
2798 * as soon as backend and action are known
2799 */
2800 if (!reprocess) {
2801 st_cur_param = cur_param;
2802 st_next_param = next_param;
2803 }
2804 reprocess = 1;
2805 }
2806 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002807 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002808 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002809 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002810 /* Not already in maintenance, we can change the server state */
2811 sv->state |= SRV_MAINTAIN;
2812 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002813 altered_servers++;
2814 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002815 }
2816 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002817 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002818 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002819 /* Already in maintenance, we can change the server state */
2820 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002821 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002822 altered_servers++;
2823 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002824 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002825 break;
2826 case ST_ADM_ACTION_STOP:
2827 case ST_ADM_ACTION_START:
2828 if (action == ST_ADM_ACTION_START)
2829 sv->uweight = sv->iweight;
2830 else
2831 sv->uweight = 0;
2832
2833 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2834 /* we must take care of not pushing the server to full throttle during slow starts */
2835 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2836 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2837 else
2838 sv->eweight = BE_WEIGHT_SCALE;
2839 sv->eweight *= sv->uweight;
2840 } else {
2841 sv->eweight = sv->uweight;
2842 }
2843
2844 /* static LB algorithms are a bit harder to update */
2845 if (px->lbprm.update_server_eweight)
2846 px->lbprm.update_server_eweight(sv);
2847 else if (sv->eweight) {
2848 if (px->lbprm.set_server_status_up)
2849 px->lbprm.set_server_status_up(sv);
2850 }
2851 else {
2852 if (px->lbprm.set_server_status_down)
2853 px->lbprm.set_server_status_down(sv);
2854 }
2855 altered_servers++;
2856 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002857 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002858 case ST_ADM_ACTION_SHUTDOWN:
2859 if (px->state != PR_STSTOPPED) {
2860 struct session *sess, *sess_bck;
2861
2862 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2863 if (sess->srv_conn == sv)
2864 session_shutdown(sess, SN_ERR_KILLED);
2865
2866 altered_servers++;
2867 total_servers++;
2868 }
2869 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002870 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002871 } else {
2872 /* the server name is unknown or ambiguous (duplicate names) */
2873 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002874 }
2875 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002876 if (reprocess && px && action) {
2877 /* Now, we know the backend and the action chosen by the user.
2878 * We can safely restart from the first server parameter
2879 * to reprocess them
2880 */
2881 cur_param = st_cur_param;
2882 next_param = st_next_param;
2883 reprocess = 0;
2884 goto reprocess_servers;
2885 }
2886
Cyril Bonté70be45d2010-10-12 00:14:35 +02002887 next_param = cur_param;
2888 }
2889 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002890
2891 if (total_servers == 0) {
2892 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2893 }
2894 else if (altered_servers == 0) {
2895 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2896 }
2897 else if (altered_servers == total_servers) {
2898 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2899 }
2900 else {
2901 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2902 }
2903 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002904 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002905}
2906
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002907/* returns a pointer to the first rule which forbids access (deny or http_auth),
2908 * or NULL if everything's OK.
2909 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002910static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002911http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2912{
Willy Tarreauff011f22011-01-06 17:51:27 +01002913 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002914
Willy Tarreauff011f22011-01-06 17:51:27 +01002915 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002916 int ret = 1;
2917
Willy Tarreauff011f22011-01-06 17:51:27 +01002918 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002919 continue;
2920
2921 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002922 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002923 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002924 ret = acl_pass(ret);
2925
Willy Tarreauff011f22011-01-06 17:51:27 +01002926 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002927 ret = !ret;
2928 }
2929
2930 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002931 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002932 return NULL; /* no problem */
2933 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002934 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002935 }
2936 }
2937 return NULL;
2938}
2939
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002940/* This stream analyser runs all HTTP request processing which is common to
2941 * frontends and backends, which means blocking ACLs, filters, connection-close,
2942 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002943 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002944 * either needs more data or wants to immediately abort the request (eg: deny,
2945 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002946 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002947int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002948{
Willy Tarreaud787e662009-07-07 10:14:51 +02002949 struct http_txn *txn = &s->txn;
2950 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002951 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002952 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002953 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002954 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002955 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002956
Willy Tarreau655dce92009-11-08 13:10:58 +01002957 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002958 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002959 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002960 return 0;
2961 }
2962
Willy Tarreau3a816292009-07-07 10:55:49 +02002963 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002964 req->analyse_exp = TICK_ETERNITY;
2965
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002966 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 +02002967 now_ms, __FUNCTION__,
2968 s,
2969 req,
2970 req->rex, req->wex,
2971 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002972 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002973 req->analysers);
2974
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002975 /* first check whether we have some ACLs set to block this request */
2976 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002977 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002978
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002979 ret = acl_pass(ret);
2980 if (cond->pol == ACL_COND_UNLESS)
2981 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002982
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002983 if (ret) {
2984 txn->status = 403;
2985 /* let's log the request time */
2986 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002987 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002988 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002989 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002990 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002991 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002992
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002993 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002994 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002995
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002996 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002997 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002998 do_stats = stats_check_uri(s->rep->prod, txn, px);
2999 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003000 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 +01003001 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003002 else
3003 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003004
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003005 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01003006 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003007 txn->status = 403;
3008 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003009 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003010 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003011 s->fe->fe_counters.denied_req++;
3012 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3013 s->be->be_counters.denied_req++;
3014 if (s->listener->counters)
3015 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003016 goto return_prx_cond;
3017 }
3018
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003019 /* try headers filters */
3020 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003021 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003022 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003023
Willy Tarreau59234e92008-11-30 23:51:27 +01003024 /* has the request been denied ? */
3025 if (txn->flags & TX_CLDENY) {
3026 /* no need to go further */
3027 txn->status = 403;
3028 /* let's log the request time */
3029 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003030 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003031 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003032 goto return_prx_cond;
3033 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003034
3035 /* When a connection is tarpitted, we use the tarpit timeout,
3036 * which may be the same as the connect timeout if unspecified.
3037 * If unset, then set it to zero because we really want it to
3038 * eventually expire. We build the tarpit as an analyser.
3039 */
3040 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003041 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003042 /* wipe the request out so that we can drop the connection early
3043 * if the client closes first.
3044 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003045 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003046 req->analysers = 0; /* remove switching rules etc... */
3047 req->analysers |= AN_REQ_HTTP_TARPIT;
3048 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3049 if (!req->analyse_exp)
3050 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003051 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003052 return 1;
3053 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003054 }
Willy Tarreau06619262006-12-17 08:37:22 +01003055
Willy Tarreau5b154472009-12-21 20:11:07 +01003056 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3057 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003058 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3059 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003060 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3061 * avoid to redo the same work if FE and BE have the same settings (common).
3062 * The method consists in checking if options changed between the two calls
3063 * (implying that either one is non-null, or one of them is non-null and we
3064 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003065 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003066
Willy Tarreaudc008c52010-02-01 16:20:08 +01003067 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3068 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3069 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3070 (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 +01003071 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003072
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003073 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3074 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003075 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003076 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3077 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003078 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003079 tmp = TX_CON_WANT_CLO;
3080
Willy Tarreau5b154472009-12-21 20:11:07 +01003081 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3082 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003083
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003084 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3085 /* parse the Connection header and possibly clean it */
3086 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003087 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003088 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3089 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003090 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003091 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003092 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003093 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003094 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003095
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003096 /* check if client or config asks for explicit close in KAL/SCL */
3097 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3098 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3099 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003100 (!(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 +01003101 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003102 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003103 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003104 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3105 }
Willy Tarreau78599912009-10-17 20:12:21 +02003106
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003107 /* we can be blocked here because the request needs to be authenticated,
3108 * either to pass or to access stats.
3109 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003110 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003111 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003112
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003113 if (!realm)
3114 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3115
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003116 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003117 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003118 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003119 /* on 401 we still count one error, because normal browsing
3120 * won't significantly increase the counter but brute force
3121 * attempts will.
3122 */
3123 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003124 goto return_prx_cond;
3125 }
3126
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003127 /* add request headers from the rule sets in the same order */
3128 list_for_each_entry(wl, &px->req_add, list) {
3129 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003130 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003131 ret = acl_pass(ret);
3132 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3133 ret = !ret;
3134 if (!ret)
3135 continue;
3136 }
3137
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003138 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003139 goto return_bad_req;
3140 }
3141
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003142 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003143 struct stats_admin_rule *stats_admin_rule;
3144
3145 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003146 * FIXME!!! that one is rather dangerous, we want to
3147 * make it follow standard rules (eg: clear req->analysers).
3148 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003149
Cyril Bonté474be412010-10-12 00:14:36 +02003150 /* now check whether we have some admin rules for this request */
3151 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3152 int ret = 1;
3153
3154 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003155 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 +02003156 ret = acl_pass(ret);
3157 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3158 ret = !ret;
3159 }
3160
3161 if (ret) {
3162 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003163 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003164 break;
3165 }
3166 }
3167
Cyril Bonté70be45d2010-10-12 00:14:35 +02003168 /* Was the status page requested with a POST ? */
3169 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003170 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003171 if (msg->msg_state < HTTP_MSG_100_SENT) {
3172 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3173 * send an HTTP/1.1 100 Continue intermediate response.
3174 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003175 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003176 struct hdr_ctx ctx;
3177 ctx.idx = 0;
3178 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003179 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01003180 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003181 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003182 }
3183 }
3184 msg->msg_state = HTTP_MSG_100_SENT;
3185 s->logs.tv_request = now; /* update the request timer to reflect full request */
3186 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003187 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003188 /* we need more data */
3189 req->analysers |= an_bit;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003190 channel_dont_connect(req);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003191 return 0;
3192 }
Cyril Bonté474be412010-10-12 00:14:36 +02003193 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003194 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003195 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003196 }
3197
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003198 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003199 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003200 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003201 copy_target(&s->target, &s->rep->prod->conn->target); // for logging only
3202 s->rep->prod->conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01003203 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003204 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003205 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3206 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003207
3208 return 0;
3209
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003210 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003211
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003212 /* check whether we have some ACLs set to redirect this request */
3213 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003214 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003215
Willy Tarreauf285f542010-01-03 20:03:03 +01003216 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003217 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003218 ret = acl_pass(ret);
3219 if (rule->cond->pol == ACL_COND_UNLESS)
3220 ret = !ret;
3221 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003222
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003223 if (ret) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003224 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003225
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003226 /* build redirect message */
3227 switch(rule->code) {
3228 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003229 msg_fmt = HTTP_303;
3230 break;
3231 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003232 msg_fmt = HTTP_301;
3233 break;
3234 case 302:
3235 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003236 msg_fmt = HTTP_302;
3237 break;
3238 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003239
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003240 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003241 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003242
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003243 switch(rule->type) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003244 case REDIRECT_TYPE_SCHEME: {
3245 const char *path;
3246 const char *host;
3247 struct hdr_ctx ctx;
3248 int pathlen;
3249 int hostlen;
3250
3251 host = "";
3252 hostlen = 0;
3253 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003254 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 +02003255 host = ctx.line + ctx.val;
3256 hostlen = ctx.vlen;
3257 }
3258
3259 path = http_get_path(txn);
3260 /* build message using path */
3261 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003262 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003263 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3264 int qs = 0;
3265 while (qs < pathlen) {
3266 if (path[qs] == '?') {
3267 pathlen = qs;
3268 break;
3269 }
3270 qs++;
3271 }
3272 }
3273 } else {
3274 path = "/";
3275 pathlen = 1;
3276 }
3277
3278 /* check if we can add scheme + "://" + host + path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003279 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003280 goto return_bad_req;
3281
3282 /* add scheme */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003283 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3284 trash.len += rule->rdr_len;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003285
3286 /* add "://" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003287 memcpy(trash.str + trash.len, "://", 3);
3288 trash.len += 3;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003289
3290 /* add host */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003291 memcpy(trash.str + trash.len, host, hostlen);
3292 trash.len += hostlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003293
3294 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003295 memcpy(trash.str + trash.len, path, pathlen);
3296 trash.len += pathlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003297
3298 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003299 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003300 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003301 if (trash.len > trash.size - 5)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003302 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003303 trash.str[trash.len] = '/';
3304 trash.len++;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003305 }
3306
3307 break;
3308 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003309 case REDIRECT_TYPE_PREFIX: {
3310 const char *path;
3311 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003312
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003313 path = http_get_path(txn);
3314 /* build message using path */
3315 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003316 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003317 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3318 int qs = 0;
3319 while (qs < pathlen) {
3320 if (path[qs] == '?') {
3321 pathlen = qs;
3322 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003323 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003324 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003325 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003326 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003327 } else {
3328 path = "/";
3329 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003330 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003331
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003332 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003333 goto return_bad_req;
3334
3335 /* add prefix. Note that if prefix == "/", we don't want to
3336 * add anything, otherwise it makes it hard for the user to
3337 * configure a self-redirection.
3338 */
3339 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003340 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3341 trash.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003342 }
3343
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003344 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003345 memcpy(trash.str + trash.len, path, pathlen);
3346 trash.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003347
3348 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003349 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003350 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003351 if (trash.len > trash.size - 5)
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003352 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003353 trash.str[trash.len] = '/';
3354 trash.len++;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003355 }
3356
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003357 break;
3358 }
3359 case REDIRECT_TYPE_LOCATION:
3360 default:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003361 if (trash.len + rule->rdr_len > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003362 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003363
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003364 /* add location */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003365 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3366 trash.len += rule->rdr_len;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003367 break;
3368 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003369
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003370 if (rule->cookie_len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003371 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3372 trash.len += 14;
3373 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3374 trash.len += rule->cookie_len;
3375 memcpy(trash.str + trash.len, "\r\n", 2);
3376 trash.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003377 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003378
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003379 /* add end of headers and the keep-alive/close status.
3380 * We may choose to set keep-alive if the Location begins
3381 * with a slash, because the client will come back to the
3382 * same server.
3383 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003384 txn->status = rule->code;
3385 /* let's log the request time */
3386 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003387
3388 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003389 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3390 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003391 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3392 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3393 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003394 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003395 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003396 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3397 trash.len += 30;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003398 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003399 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3400 trash.len += 24;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003401 }
Willy Tarreau75661452010-01-10 10:35:01 +01003402 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003403 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3404 trash.len += 4;
3405 bo_inject(req->prod->ob, trash.str, trash.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003406 /* "eat" the request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003407 bi_fast_delete(req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003408 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003409 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003410 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3411 txn->req.msg_state = HTTP_MSG_CLOSED;
3412 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003413 break;
3414 } else {
3415 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003416 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003417 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3418 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003419 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003420 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3421 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003422 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003423 stream_int_retnclose(req->prod, &trash);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003424 goto return_prx_cond;
3425 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003426 }
3427 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003428
Willy Tarreau2be39392010-01-03 17:24:51 +01003429 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3430 * If this happens, then the data will not come immediately, so we must
3431 * send all what we have without waiting. Note that due to the small gain
3432 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003433 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003434 * itself once used.
3435 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003436 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003437
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003438 /* that's OK for us now, let's move on to next analysers */
3439 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003440
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003441 return_bad_req:
3442 /* We centralize bad requests processing here */
3443 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3444 /* we detected a parsing error. We want to archive this request
3445 * in the dedicated proxy area for later troubleshooting.
3446 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003447 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003448 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003449
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003450 txn->req.msg_state = HTTP_MSG_ERROR;
3451 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003452 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003453
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003454 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003455 if (s->listener->counters)
3456 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003457
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003458 return_prx_cond:
3459 if (!(s->flags & SN_ERR_MASK))
3460 s->flags |= SN_ERR_PRXCOND;
3461 if (!(s->flags & SN_FINST_MASK))
3462 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003463
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003464 req->analysers = 0;
3465 req->analyse_exp = TICK_ETERNITY;
3466 return 0;
3467}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003468
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003469/* This function performs all the processing enabled for the current request.
3470 * It returns 1 if the processing can continue on next analysers, or zero if it
3471 * needs more data, encounters an error, or wants to immediately abort the
3472 * request. It relies on buffers flags, and updates s->req->analysers.
3473 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003474int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003475{
3476 struct http_txn *txn = &s->txn;
3477 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003478
Willy Tarreau655dce92009-11-08 13:10:58 +01003479 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003480 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003481 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003482 return 0;
3483 }
3484
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003485 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 +02003486 now_ms, __FUNCTION__,
3487 s,
3488 req,
3489 req->rex, req->wex,
3490 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003491 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003492 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003493
William Lallemand82fe75c2012-10-23 10:25:10 +02003494 if (s->fe->comp || s->be->comp)
3495 select_compression_request_header(s, req->buf);
3496
Willy Tarreau59234e92008-11-30 23:51:27 +01003497 /*
3498 * Right now, we know that we have processed the entire headers
3499 * and that unwanted requests have been filtered out. We can do
3500 * whatever we want with the remaining request. Also, now we
3501 * may have separate values for ->fe, ->be.
3502 */
Willy Tarreau06619262006-12-17 08:37:22 +01003503
Willy Tarreau59234e92008-11-30 23:51:27 +01003504 /*
3505 * If HTTP PROXY is set we simply get remote server address
3506 * parsing incoming request.
3507 */
3508 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003509 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 +01003510 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003511
Willy Tarreau59234e92008-11-30 23:51:27 +01003512 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003513 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003514 * Note that doing so might move headers in the request, but
3515 * the fields will stay coherent and the URI will not move.
3516 * This should only be performed in the backend.
3517 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003518 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003519 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3520 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003521
Willy Tarreau59234e92008-11-30 23:51:27 +01003522 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003523 * 8: the appsession cookie was looked up very early in 1.2,
3524 * so let's do the same now.
3525 */
3526
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003527 /* It needs to look into the URI unless persistence must be ignored */
3528 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003529 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 +01003530 }
3531
William Lallemanda73203e2012-03-12 12:48:57 +01003532 /* add unique-id if "header-unique-id" is specified */
3533
3534 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3535 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3536
3537 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003538 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3539 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003540 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003541 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003542 goto return_bad_req;
3543 }
3544
Cyril Bontéb21570a2009-11-29 20:04:48 +01003545 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003546 * 9: add X-Forwarded-For if either the frontend or the backend
3547 * asks for it.
3548 */
3549 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003550 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003551 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003552 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3553 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003554 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003555 /* The header is set to be added only if none is present
3556 * and we found it, so don't do anything.
3557 */
3558 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003559 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003560 /* Add an X-Forwarded-For header unless the source IP is
3561 * in the 'except' network range.
3562 */
3563 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003564 (((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 +01003565 != s->fe->except_net.s_addr) &&
3566 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003567 (((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 +01003568 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003569 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003570 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003571 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003572
3573 /* Note: we rely on the backend to get the header name to be used for
3574 * x-forwarded-for, because the header is really meant for the backends.
3575 * However, if the backend did not specify any option, we have to rely
3576 * on the frontend's header name.
3577 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003578 if (s->be->fwdfor_hdr_len) {
3579 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003580 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003581 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003582 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003583 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003584 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003585 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003586
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003587 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003588 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003589 }
3590 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003591 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003592 /* FIXME: for the sake of completeness, we should also support
3593 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003594 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003595 int len;
3596 char pn[INET6_ADDRSTRLEN];
3597 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003598 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003599 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003600
Willy Tarreau59234e92008-11-30 23:51:27 +01003601 /* Note: we rely on the backend to get the header name to be used for
3602 * x-forwarded-for, because the header is really meant for the backends.
3603 * However, if the backend did not specify any option, we have to rely
3604 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003605 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003606 if (s->be->fwdfor_hdr_len) {
3607 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003608 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003609 } else {
3610 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003611 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003612 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003613 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003614
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003615 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003616 goto return_bad_req;
3617 }
3618 }
3619
3620 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003621 * 10: add X-Original-To if either the frontend or the backend
3622 * asks for it.
3623 */
3624 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3625
3626 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003627 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003628 /* Add an X-Original-To header unless the destination IP is
3629 * in the 'except' network range.
3630 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003631 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003632
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003633 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003634 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003635 (((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 +02003636 != s->fe->except_to.s_addr) &&
3637 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003638 (((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 +02003639 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003640 int len;
3641 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003642 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003643
3644 /* Note: we rely on the backend to get the header name to be used for
3645 * x-original-to, because the header is really meant for the backends.
3646 * However, if the backend did not specify any option, we have to rely
3647 * on the frontend's header name.
3648 */
3649 if (s->be->orgto_hdr_len) {
3650 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003651 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003652 } else {
3653 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003654 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003655 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003656 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003657
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003658 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003659 goto return_bad_req;
3660 }
3661 }
3662 }
3663
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003664 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3665 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003666 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003667 unsigned int want_flags = 0;
3668
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003669 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003670 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3671 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3672 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003673 want_flags |= TX_CON_CLO_SET;
3674 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003675 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3676 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3677 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003678 want_flags |= TX_CON_KAL_SET;
3679 }
3680
3681 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003682 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003683 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003684
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003685
Willy Tarreau522d6c02009-12-06 18:49:18 +01003686 /* If we have no server assigned yet and we're balancing on url_param
3687 * with a POST request, we may be interested in checking the body for
3688 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003689 */
3690 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3691 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003692 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003693 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003694 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003695 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003696 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003697
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003698 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003699 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003700#ifdef TCP_QUICKACK
3701 /* We expect some data from the client. Unless we know for sure
3702 * we already have a full request, we have to re-enable quick-ack
3703 * in case we previously disabled it, otherwise we might cause
3704 * the client to delay further data.
3705 */
3706 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003707 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003708 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003709 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003710#endif
3711 }
Willy Tarreau03945942009-12-22 16:50:27 +01003712
Willy Tarreau59234e92008-11-30 23:51:27 +01003713 /*************************************************************
3714 * OK, that's finished for the headers. We have done what we *
3715 * could. Let's switch to the DATA state. *
3716 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003717 req->analyse_exp = TICK_ETERNITY;
3718 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003719
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003720 /* if the server closes the connection, we want to immediately react
3721 * and close the socket to save packets and syscalls.
3722 */
3723 req->cons->flags |= SI_FL_NOHALF;
3724
Willy Tarreau59234e92008-11-30 23:51:27 +01003725 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003726 /* OK let's go on with the BODY now */
3727 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003728
Willy Tarreau59234e92008-11-30 23:51:27 +01003729 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003730 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003731 /* we detected a parsing error. We want to archive this request
3732 * in the dedicated proxy area for later troubleshooting.
3733 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003734 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003735 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003736
Willy Tarreau59234e92008-11-30 23:51:27 +01003737 txn->req.msg_state = HTTP_MSG_ERROR;
3738 txn->status = 400;
3739 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003740 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003741
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003742 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003743 if (s->listener->counters)
3744 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003745
Willy Tarreau59234e92008-11-30 23:51:27 +01003746 if (!(s->flags & SN_ERR_MASK))
3747 s->flags |= SN_ERR_PRXCOND;
3748 if (!(s->flags & SN_FINST_MASK))
3749 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003750 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003751}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003752
Willy Tarreau60b85b02008-11-30 23:28:40 +01003753/* This function is an analyser which processes the HTTP tarpit. It always
3754 * returns zero, at the beginning because it prevents any other processing
3755 * from occurring, and at the end because it terminates the request.
3756 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003757int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003758{
3759 struct http_txn *txn = &s->txn;
3760
3761 /* This connection is being tarpitted. The CLIENT side has
3762 * already set the connect expiration date to the right
3763 * timeout. We just have to check that the client is still
3764 * there and that the timeout has not expired.
3765 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003766 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003767 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003768 !tick_is_expired(req->analyse_exp, now_ms))
3769 return 0;
3770
3771 /* We will set the queue timer to the time spent, just for
3772 * logging purposes. We fake a 500 server error, so that the
3773 * attacker will not suspect his connection has been tarpitted.
3774 * It will not cause trouble to the logs because we can exclude
3775 * the tarpitted connections by filtering on the 'PT' status flags.
3776 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003777 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3778
3779 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003780 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003781 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003782
3783 req->analysers = 0;
3784 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003785
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003786 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003787 if (s->listener->counters)
3788 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003789
Willy Tarreau60b85b02008-11-30 23:28:40 +01003790 if (!(s->flags & SN_ERR_MASK))
3791 s->flags |= SN_ERR_PRXCOND;
3792 if (!(s->flags & SN_FINST_MASK))
3793 s->flags |= SN_FINST_T;
3794 return 0;
3795}
3796
Willy Tarreaud34af782008-11-30 23:36:37 +01003797/* This function is an analyser which processes the HTTP request body. It looks
3798 * for parameters to be used for the load balancing algorithm (url_param). It
3799 * must only be called after the standard HTTP request processing has occurred,
3800 * because it expects the request to be parsed. It returns zero if it needs to
3801 * read more data, or 1 once it has completed its analysis.
3802 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003803int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003804{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003805 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003806 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003807 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003808
3809 /* We have to parse the HTTP request body to find any required data.
3810 * "balance url_param check_post" should have been the only way to get
3811 * into this. We were brought here after HTTP header analysis, so all
3812 * related structures are ready.
3813 */
3814
Willy Tarreau522d6c02009-12-06 18:49:18 +01003815 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3816 goto missing_data;
3817
3818 if (msg->msg_state < HTTP_MSG_100_SENT) {
3819 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3820 * send an HTTP/1.1 100 Continue intermediate response.
3821 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003822 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003823 struct hdr_ctx ctx;
3824 ctx.idx = 0;
3825 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003826 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003827 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003828 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003829 }
3830 }
3831 msg->msg_state = HTTP_MSG_100_SENT;
3832 }
3833
3834 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003835 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02003836 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003837 * is still null. We must save the body in msg->next because it
3838 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003839 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003840 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003841
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003842 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003843 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3844 else
3845 msg->msg_state = HTTP_MSG_DATA;
3846 }
3847
3848 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003849 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003850 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003851 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003852 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003853 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003854
Willy Tarreau115acb92009-12-26 13:56:06 +01003855 if (!ret)
3856 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003857 else if (ret < 0) {
3858 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003859 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003860 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003861 }
3862
Willy Tarreaud98cf932009-12-27 22:54:55 +01003863 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003864 * We have the first data byte is in msg->sov. We're waiting for at
3865 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003866 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003867
Willy Tarreau124d9912011-03-01 20:30:48 +01003868 if (msg->body_len < limit)
3869 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003870
Willy Tarreau9b28e032012-10-12 23:49:43 +02003871 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003872 goto http_end;
3873
3874 missing_data:
3875 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003876 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02003877 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003878 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003879 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003880
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003881 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003882 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02003883 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003884
3885 if (!(s->flags & SN_ERR_MASK))
3886 s->flags |= SN_ERR_CLITO;
3887 if (!(s->flags & SN_FINST_MASK))
3888 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003889 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003890 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003891
3892 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003893 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003894 /* Not enough data. We'll re-use the http-request
3895 * timeout here. Ideally, we should set the timeout
3896 * relative to the accept() date. We just set the
3897 * request timeout once at the beginning of the
3898 * request.
3899 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003900 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003901 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003902 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003903 return 0;
3904 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003905
3906 http_end:
3907 /* The situation will not evolve, so let's give up on the analysis. */
3908 s->logs.tv_request = now; /* update the request timer to reflect full request */
3909 req->analysers &= ~an_bit;
3910 req->analyse_exp = TICK_ETERNITY;
3911 return 1;
3912
3913 return_bad_req: /* let's centralize all bad requests */
3914 txn->req.msg_state = HTTP_MSG_ERROR;
3915 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003916 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003917
Willy Tarreau79ebac62010-06-07 13:47:49 +02003918 if (!(s->flags & SN_ERR_MASK))
3919 s->flags |= SN_ERR_PRXCOND;
3920 if (!(s->flags & SN_FINST_MASK))
3921 s->flags |= SN_FINST_R;
3922
Willy Tarreau522d6c02009-12-06 18:49:18 +01003923 return_err_msg:
3924 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003925 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003926 if (s->listener->counters)
3927 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003928 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003929}
3930
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003931/* send a server's name with an outgoing request over an established connection.
3932 * Note: this function is designed to be called once the request has been scheduled
3933 * for being forwarded. This is the reason why it rewinds the buffer before
3934 * proceeding.
3935 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003936int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003937
3938 struct hdr_ctx ctx;
3939
Mark Lamourinec2247f02012-01-04 13:02:01 -05003940 char *hdr_name = be->server_id_hdr_name;
3941 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003942 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003943 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003944 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003945
William Lallemandd9e90662012-01-30 17:27:17 +01003946 ctx.idx = 0;
3947
Willy Tarreau9b28e032012-10-12 23:49:43 +02003948 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003949 if (old_o) {
3950 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003951 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003952 }
3953
Willy Tarreau9b28e032012-10-12 23:49:43 +02003954 old_i = chn->buf->i;
3955 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 -05003956 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003957 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003958 }
3959
3960 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003961 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003962 memcpy(hdr_val, hdr_name, hdr_name_len);
3963 hdr_val += hdr_name_len;
3964 *hdr_val++ = ':';
3965 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003966 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
3967 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003968
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003969 if (old_o) {
3970 /* If this was a forwarded request, we must readjust the amount of
3971 * data to be forwarded in order to take into account the size
3972 * variations.
3973 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003974 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003975 }
3976
Mark Lamourinec2247f02012-01-04 13:02:01 -05003977 return 0;
3978}
3979
Willy Tarreau610ecce2010-01-04 21:15:02 +01003980/* Terminate current transaction and prepare a new one. This is very tricky
3981 * right now but it works.
3982 */
3983void http_end_txn_clean_session(struct session *s)
3984{
3985 /* FIXME: We need a more portable way of releasing a backend's and a
3986 * server's connections. We need a safer way to reinitialize buffer
3987 * flags. We also need a more accurate method for computing per-request
3988 * data.
3989 */
3990 http_silent_debug(__LINE__, s);
3991
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003992 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02003993 si_shutr(s->req->cons);
3994 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003995
3996 http_silent_debug(__LINE__, s);
3997
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003998 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003999 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004000 if (unlikely(s->srv_conn))
4001 sess_change_server(s, NULL);
4002 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004003
4004 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4005 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004006 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007
4008 if (s->txn.status) {
4009 int n;
4010
4011 n = s->txn.status / 100;
4012 if (n < 1 || n > 5)
4013 n = 0;
4014
4015 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004016 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004017
Willy Tarreau24657792010-02-26 10:30:28 +01004018 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004019 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004020 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004021 }
4022
4023 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004024 s->logs.bytes_in -= s->req->buf->i;
4025 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004026
4027 /* let's do a final log if we need it */
4028 if (s->logs.logwait &&
4029 !(s->flags & SN_MONITOR) &&
4030 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4031 s->do_log(s);
4032 }
4033
4034 s->logs.accept_date = date; /* user-visible date for logging */
4035 s->logs.tv_accept = now; /* corrected date for internal use */
4036 tv_zero(&s->logs.tv_request);
4037 s->logs.t_queue = -1;
4038 s->logs.t_connect = -1;
4039 s->logs.t_data = -1;
4040 s->logs.t_close = 0;
4041 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4042 s->logs.srv_queue_size = 0; /* we will get this number soon */
4043
Willy Tarreau9b28e032012-10-12 23:49:43 +02004044 s->logs.bytes_in = s->req->total = s->req->buf->i;
4045 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004046
4047 if (s->pend_pos)
4048 pendconn_free(s->pend_pos);
4049
Willy Tarreau827aee92011-03-10 16:55:02 +01004050 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004051 if (s->flags & SN_CURR_SESS) {
4052 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01004053 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004054 }
Willy Tarreau827aee92011-03-10 16:55:02 +01004055 if (may_dequeue_tasks(target_srv(&s->target), s->be))
4056 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004057 }
4058
Willy Tarreau9e000c62011-03-10 14:03:36 +01004059 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004060
4061 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004062 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4063 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004064 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004065 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004066 s->req->cons->err_loc = NULL;
4067 s->req->cons->exp = TICK_ETERNITY;
4068 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004069 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4070 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 +02004071 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 +01004072 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
4073 s->txn.meth = 0;
4074 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004075 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004076 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004077 s->req->cons->flags |= SI_FL_INDEP_STR;
4078
Willy Tarreau96e31212011-05-30 18:10:30 +02004079 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004080 s->req->flags |= CF_NEVER_WAIT;
4081 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004082 }
4083
Willy Tarreau610ecce2010-01-04 21:15:02 +01004084 /* if the request buffer is not empty, it means we're
4085 * about to process another request, so send pending
4086 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004087 * Just don't do this if the buffer is close to be full,
4088 * because the request will wait for it to flush a little
4089 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004090 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004091 if (s->req->buf->i) {
4092 if (s->rep->buf->o &&
4093 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4094 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004095 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004096 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004097
4098 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004099 channel_auto_read(s->req);
4100 channel_auto_close(s->req);
4101 channel_auto_read(s->rep);
4102 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004103
Willy Tarreau342b11c2010-11-24 16:22:09 +01004104 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004105 s->rep->analysers = 0;
4106
4107 http_silent_debug(__LINE__, s);
4108}
4109
4110
4111/* This function updates the request state machine according to the response
4112 * state machine and buffer flags. It returns 1 if it changes anything (flag
4113 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4114 * it is only used to find when a request/response couple is complete. Both
4115 * this function and its equivalent should loop until both return zero. It
4116 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4117 */
4118int http_sync_req_state(struct session *s)
4119{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004120 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004121 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004122 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004123 unsigned int old_state = txn->req.msg_state;
4124
4125 http_silent_debug(__LINE__, s);
4126 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4127 return 0;
4128
4129 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004130 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004131 * We can shut the read side unless we want to abort_on_close,
4132 * or we have a POST request. The issue with POST requests is
4133 * that some browsers still send a CRLF after the request, and
4134 * this CRLF must be read so that it does not remain in the kernel
4135 * buffers, otherwise a close could cause an RST on some systems
4136 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004137 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004138 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004139 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004140
4141 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4142 goto wait_other_side;
4143
4144 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4145 /* The server has not finished to respond, so we
4146 * don't want to move in order not to upset it.
4147 */
4148 goto wait_other_side;
4149 }
4150
4151 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4152 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004153 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004154 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004155 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004156 goto wait_other_side;
4157 }
4158
4159 /* When we get here, it means that both the request and the
4160 * response have finished receiving. Depending on the connection
4161 * mode, we'll have to wait for the last bytes to leave in either
4162 * direction, and sometimes for a close to be effective.
4163 */
4164
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004165 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4166 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004167 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4168 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004169 }
4170 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4171 /* Option forceclose is set, or either side wants to close,
4172 * let's enforce it now that we're not expecting any new
4173 * data to come. The caller knows the session is complete
4174 * once both states are CLOSED.
4175 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004176 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4177 channel_shutr_now(chn);
4178 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004179 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004180 }
4181 else {
4182 /* The last possible modes are keep-alive and tunnel. Since tunnel
4183 * mode does not set the body analyser, we can't reach this place
4184 * in tunnel mode, so we're left with keep-alive only.
4185 * This mode is currently not implemented, we switch to tunnel mode.
4186 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004187 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004188 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004189 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004190 }
4191
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004192 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004193 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004194 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004195
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004196 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004197 txn->req.msg_state = HTTP_MSG_CLOSING;
4198 goto http_msg_closing;
4199 }
4200 else {
4201 txn->req.msg_state = HTTP_MSG_CLOSED;
4202 goto http_msg_closed;
4203 }
4204 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004205 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206 }
4207
4208 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4209 http_msg_closing:
4210 /* nothing else to forward, just waiting for the output buffer
4211 * to be empty and for the shutw_now to take effect.
4212 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004213 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004214 txn->req.msg_state = HTTP_MSG_CLOSED;
4215 goto http_msg_closed;
4216 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004217 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004218 txn->req.msg_state = HTTP_MSG_ERROR;
4219 goto wait_other_side;
4220 }
4221 }
4222
4223 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4224 http_msg_closed:
4225 goto wait_other_side;
4226 }
4227
4228 wait_other_side:
4229 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004230 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231}
4232
4233
4234/* This function updates the response state machine according to the request
4235 * state machine and buffer flags. It returns 1 if it changes anything (flag
4236 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4237 * it is only used to find when a request/response couple is complete. Both
4238 * this function and its equivalent should loop until both return zero. It
4239 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4240 */
4241int http_sync_res_state(struct session *s)
4242{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004243 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004244 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004245 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004246 unsigned int old_state = txn->rsp.msg_state;
4247
4248 http_silent_debug(__LINE__, s);
4249 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4250 return 0;
4251
4252 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4253 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004254 * still monitor the server connection for a possible close
4255 * while the request is being uploaded, so we don't disable
4256 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004257 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004258 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004259
4260 if (txn->req.msg_state == HTTP_MSG_ERROR)
4261 goto wait_other_side;
4262
4263 if (txn->req.msg_state < HTTP_MSG_DONE) {
4264 /* The client seems to still be sending data, probably
4265 * because we got an error response during an upload.
4266 * We have the choice of either breaking the connection
4267 * or letting it pass through. Let's do the later.
4268 */
4269 goto wait_other_side;
4270 }
4271
4272 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4273 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004274 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004275 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004276 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004277 goto wait_other_side;
4278 }
4279
4280 /* When we get here, it means that both the request and the
4281 * response have finished receiving. Depending on the connection
4282 * mode, we'll have to wait for the last bytes to leave in either
4283 * direction, and sometimes for a close to be effective.
4284 */
4285
4286 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4287 /* Server-close mode : shut read and wait for the request
4288 * side to close its output buffer. The caller will detect
4289 * when we're in DONE and the other is in CLOSED and will
4290 * catch that for the final cleanup.
4291 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004292 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4293 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004294 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004295 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4296 /* Option forceclose is set, or either side wants to close,
4297 * let's enforce it now that we're not expecting any new
4298 * data to come. The caller knows the session is complete
4299 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004300 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004301 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4302 channel_shutr_now(chn);
4303 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004304 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004305 }
4306 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004307 /* The last possible modes are keep-alive and tunnel. Since tunnel
4308 * mode does not set the body analyser, we can't reach this place
4309 * in tunnel mode, so we're left with keep-alive only.
4310 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004311 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004312 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004313 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004314 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004315 }
4316
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004317 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004318 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004319 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004320 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4321 goto http_msg_closing;
4322 }
4323 else {
4324 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4325 goto http_msg_closed;
4326 }
4327 }
4328 goto wait_other_side;
4329 }
4330
4331 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4332 http_msg_closing:
4333 /* nothing else to forward, just waiting for the output buffer
4334 * to be empty and for the shutw_now to take effect.
4335 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004336 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004337 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4338 goto http_msg_closed;
4339 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004340 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004341 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004342 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004343 if (target_srv(&s->target))
4344 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004345 goto wait_other_side;
4346 }
4347 }
4348
4349 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4350 http_msg_closed:
4351 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004352 bi_erase(chn);
4353 channel_auto_close(chn);
4354 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004355 goto wait_other_side;
4356 }
4357
4358 wait_other_side:
4359 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004360 /* We force the response to leave immediately if we're waiting for the
4361 * other side, since there is no pending shutdown to push it out.
4362 */
4363 if (!channel_is_empty(chn))
4364 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004365 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366}
4367
4368
4369/* Resync the request and response state machines. Return 1 if either state
4370 * changes.
4371 */
4372int http_resync_states(struct session *s)
4373{
4374 struct http_txn *txn = &s->txn;
4375 int old_req_state = txn->req.msg_state;
4376 int old_res_state = txn->rsp.msg_state;
4377
4378 http_silent_debug(__LINE__, s);
4379 http_sync_req_state(s);
4380 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004381 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004382 if (!http_sync_res_state(s))
4383 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004384 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004385 if (!http_sync_req_state(s))
4386 break;
4387 }
4388 http_silent_debug(__LINE__, s);
4389 /* OK, both state machines agree on a compatible state.
4390 * There are a few cases we're interested in :
4391 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4392 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4393 * directions, so let's simply disable both analysers.
4394 * - HTTP_MSG_CLOSED on the response only means we must abort the
4395 * request.
4396 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4397 * with server-close mode means we've completed one request and we
4398 * must re-initialize the server connection.
4399 */
4400
4401 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4402 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4403 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4404 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4405 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004406 channel_auto_close(s->req);
4407 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004408 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004409 channel_auto_close(s->rep);
4410 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004411 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004412 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4413 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004414 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004415 (s->rep->flags & CF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004416 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004417 channel_auto_close(s->rep);
4418 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004419 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004420 channel_abort(s->req);
4421 channel_auto_close(s->req);
4422 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004423 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004424 }
4425 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4426 txn->rsp.msg_state == HTTP_MSG_DONE &&
4427 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4428 /* server-close: terminate this server connection and
4429 * reinitialize a fresh-new transaction.
4430 */
4431 http_end_txn_clean_session(s);
4432 }
4433
4434 http_silent_debug(__LINE__, s);
4435 return txn->req.msg_state != old_req_state ||
4436 txn->rsp.msg_state != old_res_state;
4437}
4438
Willy Tarreaud98cf932009-12-27 22:54:55 +01004439/* This function is an analyser which forwards request body (including chunk
4440 * sizes if any). It is called as soon as we must forward, even if we forward
4441 * zero byte. The only situation where it must not be called is when we're in
4442 * tunnel mode and we want to forward till the close. It's used both to forward
4443 * remaining data and to resync after end of body. It expects the msg_state to
4444 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4445 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004446 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004447 * bytes of pending data + the headers if not already done (between sol and sov).
4448 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004449 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004450int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004451{
4452 struct http_txn *txn = &s->txn;
4453 struct http_msg *msg = &s->txn.req;
4454
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004455 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4456 return 0;
4457
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004458 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004459 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004460 /* Output closed while we were sending data. We must abort and
4461 * wake the other side up.
4462 */
4463 msg->msg_state = HTTP_MSG_ERROR;
4464 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004465 return 1;
4466 }
4467
Willy Tarreau4fe41902010-06-07 22:27:41 +02004468 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004469 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004470
4471 /* Note that we don't have to send 100-continue back because we don't
4472 * need the data to complete our job, and it's up to the server to
4473 * decide whether to return 100, 417 or anything else in return of
4474 * an "Expect: 100-continue" header.
4475 */
4476
4477 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004478 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004479 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004480 * is still null. We must save the body in msg->next because it
4481 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004482 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004483 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004484
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004485 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004486 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004487 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004488 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004489 }
4490
Willy Tarreaud98cf932009-12-27 22:54:55 +01004491 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004492 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004493
Willy Tarreau610ecce2010-01-04 21:15:02 +01004494 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004495 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004496 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004497 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004498 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004499 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004500 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004501 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004502 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004503
Willy Tarreaucaabe412010-01-03 23:08:28 +01004504 if (msg->msg_state == HTTP_MSG_DATA) {
4505 /* must still forward */
4506 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004507 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004508
4509 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004510 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004511 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004512 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004513 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004514 }
4515 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004516 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004517 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004518 * TRAILERS state.
4519 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004520 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004521
Willy Tarreau54d23df2012-10-25 19:04:45 +02004522 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004523 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 Tarreau8a0cef22012-03-09 13:39:23 +01004527 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, 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 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004531 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004532 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004533 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004534 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004535
4536 if (ret == 0)
4537 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004538 else if (ret < 0) {
4539 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004540 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004541 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004542 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004543 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004544 /* we're in MSG_CHUNK_SIZE now */
4545 }
4546 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004547 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004548
4549 if (ret == 0)
4550 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004551 else if (ret < 0) {
4552 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004553 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004554 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004555 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004556 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004557 /* we're in HTTP_MSG_DONE now */
4558 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004559 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004560 int old_state = msg->msg_state;
4561
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004563 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004564 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4565 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004566 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004567 if (http_resync_states(s)) {
4568 /* some state changes occurred, maybe the analyser
4569 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004570 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004571 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004572 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004573 /* request errors are most likely due to
4574 * the server aborting the transfer.
4575 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004576 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004577 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004578 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004579 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004580 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004581 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004582 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004583 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004584
4585 /* If "option abortonclose" is set on the backend, we
4586 * want to monitor the client's connection and forward
4587 * any shutdown notification to the server, which will
4588 * decide whether to close or to go on processing the
4589 * request.
4590 */
4591 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004592 channel_auto_read(req);
4593 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004594 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004595 else if (s->txn.meth == HTTP_METH_POST) {
4596 /* POST requests may require to read extra CRLF
4597 * sent by broken browsers and which could cause
4598 * an RST to be sent upon close on some systems
4599 * (eg: Linux).
4600 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004601 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004602 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004603
Willy Tarreau610ecce2010-01-04 21:15:02 +01004604 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004605 }
4606 }
4607
Willy Tarreaud98cf932009-12-27 22:54:55 +01004608 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004609 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004610 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004611 if (!(s->flags & SN_ERR_MASK))
4612 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004613 if (!(s->flags & SN_FINST_MASK)) {
4614 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4615 s->flags |= SN_FINST_H;
4616 else
4617 s->flags |= SN_FINST_D;
4618 }
4619
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004620 s->fe->fe_counters.cli_aborts++;
4621 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004622 if (target_srv(&s->target))
4623 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004624
4625 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004626 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004627
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004628 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004629 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004630 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004631
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004632 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004633 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004634 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004635 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004636 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004637
Willy Tarreau5c620922011-05-11 19:56:11 +02004638 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004639 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004640 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004641 * modes are already handled by the stream sock layer. We must not do
4642 * this in content-length mode because it could present the MSG_MORE
4643 * flag with the last block of forwarded data, which would cause an
4644 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004645 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004646 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004647 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004648
Willy Tarreau610ecce2010-01-04 21:15:02 +01004649 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004650 return 0;
4651
Willy Tarreaud98cf932009-12-27 22:54:55 +01004652 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004653 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004654 if (s->listener->counters)
4655 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004656 return_bad_req_stats_ok:
4657 txn->req.msg_state = HTTP_MSG_ERROR;
4658 if (txn->status) {
4659 /* Note: we don't send any error if some data were already sent */
4660 stream_int_retnclose(req->prod, NULL);
4661 } else {
4662 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004663 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004664 }
4665 req->analysers = 0;
4666 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004667
4668 if (!(s->flags & SN_ERR_MASK))
4669 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004670 if (!(s->flags & SN_FINST_MASK)) {
4671 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4672 s->flags |= SN_FINST_H;
4673 else
4674 s->flags |= SN_FINST_D;
4675 }
4676 return 0;
4677
4678 aborted_xfer:
4679 txn->req.msg_state = HTTP_MSG_ERROR;
4680 if (txn->status) {
4681 /* Note: we don't send any error if some data were already sent */
4682 stream_int_retnclose(req->prod, NULL);
4683 } else {
4684 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004685 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004686 }
4687 req->analysers = 0;
4688 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4689
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004690 s->fe->fe_counters.srv_aborts++;
4691 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004692 if (target_srv(&s->target))
4693 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004694
4695 if (!(s->flags & SN_ERR_MASK))
4696 s->flags |= SN_ERR_SRVCL;
4697 if (!(s->flags & SN_FINST_MASK)) {
4698 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4699 s->flags |= SN_FINST_H;
4700 else
4701 s->flags |= SN_FINST_D;
4702 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004703 return 0;
4704}
4705
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004706/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4707 * processing can continue on next analysers, or zero if it either needs more
4708 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4709 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4710 * when it has nothing left to do, and may remove any analyser when it wants to
4711 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004712 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004713int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004714{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004715 struct http_txn *txn = &s->txn;
4716 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004717 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004718 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004719 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004720 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004721
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004722 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 +02004723 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004724 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004725 rep,
4726 rep->rex, rep->wex,
4727 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004728 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004729 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004730
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004731 /*
4732 * Now parse the partial (or complete) lines.
4733 * We will check the response syntax, and also join multi-line
4734 * headers. An index of all the lines will be elaborated while
4735 * parsing.
4736 *
4737 * For the parsing, we use a 28 states FSM.
4738 *
4739 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004740 * rep->buf->p = beginning of response
4741 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4742 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004743 * msg->eol = end of current header or line (LF or CRLF)
4744 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004745 */
4746
Willy Tarreau83e3af02009-12-28 17:39:57 +01004747 /* There's a protected area at the end of the buffer for rewriting
4748 * purposes. We don't want to start to parse the request if the
4749 * protected area is affected, because we may have to move processed
4750 * data later, which is much more complicated.
4751 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004752 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004753 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004754 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4755 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4756 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004757 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004758 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004759 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004760 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004761 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004762 return 0;
4763 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004764 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4765 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004766 }
4767
Willy Tarreau9b28e032012-10-12 23:49:43 +02004768 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004769 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004770 }
4771
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004772 /* 1: we might have to print this header in debug mode */
4773 if (unlikely((global.mode & MODE_DEBUG) &&
4774 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004775 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004776 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004777
Willy Tarreau9b28e032012-10-12 23:49:43 +02004778 sol = rep->buf->p;
4779 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004780 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004781
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004782 sol += hdr_idx_first_pos(&txn->hdr_idx);
4783 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004784
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004785 while (cur_idx) {
4786 eol = sol + txn->hdr_idx.v[cur_idx].len;
4787 debug_hdr("srvhdr", s, sol, eol);
4788 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4789 cur_idx = txn->hdr_idx.v[cur_idx].next;
4790 }
4791 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004792
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004793 /*
4794 * Now we quickly check if we have found a full valid response.
4795 * If not so, we check the FD and buffer states before leaving.
4796 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004797 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004798 * responses are checked first.
4799 *
4800 * Depending on whether the client is still there or not, we
4801 * may send an error response back or not. Note that normally
4802 * we should only check for HTTP status there, and check I/O
4803 * errors somewhere else.
4804 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004805
Willy Tarreau655dce92009-11-08 13:10:58 +01004806 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004807 /* Invalid response */
4808 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4809 /* we detected a parsing error. We want to archive this response
4810 * in the dedicated proxy area for later troubleshooting.
4811 */
4812 hdr_response_bad:
4813 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004814 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004815
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004816 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004817 if (target_srv(&s->target)) {
4818 target_srv(&s->target)->counters.failed_resp++;
4819 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004820 }
Willy Tarreau64648412010-03-05 10:41:54 +01004821 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004822 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004823 rep->analysers = 0;
4824 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004825 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004826 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004827 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004828
4829 if (!(s->flags & SN_ERR_MASK))
4830 s->flags |= SN_ERR_PRXCOND;
4831 if (!(s->flags & SN_FINST_MASK))
4832 s->flags |= SN_FINST_H;
4833
4834 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004835 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004836
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004837 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004838 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004839 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02004840 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004841 goto hdr_response_bad;
4842 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004843
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004844 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004845 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004846 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004847 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004848
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004849 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004850 if (target_srv(&s->target)) {
4851 target_srv(&s->target)->counters.failed_resp++;
4852 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004853 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004854
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004855 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004856 rep->analysers = 0;
4857 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004858 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004859 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004860 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004861
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004862 if (!(s->flags & SN_ERR_MASK))
4863 s->flags |= SN_ERR_SRVCL;
4864 if (!(s->flags & SN_FINST_MASK))
4865 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004866 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004867 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004868
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004869 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004870 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004871 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004872 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004873
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004874 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004875 if (target_srv(&s->target)) {
4876 target_srv(&s->target)->counters.failed_resp++;
4877 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004878 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004879
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004880 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004881 rep->analysers = 0;
4882 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004883 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004884 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004885 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004886
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004887 if (!(s->flags & SN_ERR_MASK))
4888 s->flags |= SN_ERR_SRVTO;
4889 if (!(s->flags & SN_FINST_MASK))
4890 s->flags |= SN_FINST_H;
4891 return 0;
4892 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004893
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004894 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004895 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004896 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004897 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004898
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004899 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004900 if (target_srv(&s->target)) {
4901 target_srv(&s->target)->counters.failed_resp++;
4902 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004903 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004904
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004905 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004906 rep->analysers = 0;
4907 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004908 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004909 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004910 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004911
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004912 if (!(s->flags & SN_ERR_MASK))
4913 s->flags |= SN_ERR_SRVCL;
4914 if (!(s->flags & SN_FINST_MASK))
4915 s->flags |= SN_FINST_H;
4916 return 0;
4917 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004918
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004919 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004920 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004921 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004922 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004923
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004924 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004925 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004926 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004927
4928 if (!(s->flags & SN_ERR_MASK))
4929 s->flags |= SN_ERR_CLICL;
4930 if (!(s->flags & SN_FINST_MASK))
4931 s->flags |= SN_FINST_H;
4932
4933 /* process_session() will take care of the error */
4934 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004935 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004936
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004937 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004938 return 0;
4939 }
4940
4941 /* More interesting part now : we know that we have a complete
4942 * response which at least looks like HTTP. We have an indicator
4943 * of each header's length, so we can parse them quickly.
4944 */
4945
4946 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004947 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004948
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004949 /*
4950 * 1: get the status code
4951 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004952 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004953 if (n < 1 || n > 5)
4954 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004955 /* when the client triggers a 4xx from the server, it's most often due
4956 * to a missing object or permission. These events should be tracked
4957 * because if they happen often, it may indicate a brute force or a
4958 * vulnerability scan.
4959 */
4960 if (n == 4)
4961 session_inc_http_err_ctr(s);
4962
Willy Tarreau827aee92011-03-10 16:55:02 +01004963 if (target_srv(&s->target))
4964 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004965
Willy Tarreau5b154472009-12-21 20:11:07 +01004966 /* check if the response is HTTP/1.1 or above */
4967 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02004968 ((rep->buf->p[5] > '1') ||
4969 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004970 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004971
4972 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004973 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 +01004974
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004975 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004976 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004977
Willy Tarreau9b28e032012-10-12 23:49:43 +02004978 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004979
Willy Tarreau39650402010-03-15 19:44:39 +01004980 /* Adjust server's health based on status code. Note: status codes 501
4981 * and 505 are triggered on demand by client request, so we must not
4982 * count them as server failures.
4983 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004984 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004985 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004986 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004987 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004988 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004989 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004990
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004991 /*
4992 * 2: check for cacheability.
4993 */
4994
4995 switch (txn->status) {
4996 case 200:
4997 case 203:
4998 case 206:
4999 case 300:
5000 case 301:
5001 case 410:
5002 /* RFC2616 @13.4:
5003 * "A response received with a status code of
5004 * 200, 203, 206, 300, 301 or 410 MAY be stored
5005 * by a cache (...) unless a cache-control
5006 * directive prohibits caching."
5007 *
5008 * RFC2616 @9.5: POST method :
5009 * "Responses to this method are not cacheable,
5010 * unless the response includes appropriate
5011 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005012 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005013 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005014 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005015 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5016 break;
5017 default:
5018 break;
5019 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005021 /*
5022 * 3: we may need to capture headers
5023 */
5024 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005025 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005026 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005027 txn->rsp.cap, s->fe->rsp_cap);
5028
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005029 /* 4: determine the transfer-length.
5030 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5031 * the presence of a message-body in a RESPONSE and its transfer length
5032 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005033 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005034 * All responses to the HEAD request method MUST NOT include a
5035 * message-body, even though the presence of entity-header fields
5036 * might lead one to believe they do. All 1xx (informational), 204
5037 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5038 * message-body. All other responses do include a message-body,
5039 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005040 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005041 * 1. Any response which "MUST NOT" include a message-body (such as the
5042 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5043 * always terminated by the first empty line after the header fields,
5044 * regardless of the entity-header fields present in the message.
5045 *
5046 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5047 * the "chunked" transfer-coding (Section 6.2) is used, the
5048 * transfer-length is defined by the use of this transfer-coding.
5049 * If a Transfer-Encoding header field is present and the "chunked"
5050 * transfer-coding is not present, the transfer-length is defined by
5051 * the sender closing the connection.
5052 *
5053 * 3. If a Content-Length header field is present, its decimal value in
5054 * OCTETs represents both the entity-length and the transfer-length.
5055 * If a message is received with both a Transfer-Encoding header
5056 * field and a Content-Length header field, the latter MUST be ignored.
5057 *
5058 * 4. If the message uses the media type "multipart/byteranges", and
5059 * the transfer-length is not otherwise specified, then this self-
5060 * delimiting media type defines the transfer-length. This media
5061 * type MUST NOT be used unless the sender knows that the recipient
5062 * can parse it; the presence in a request of a Range header with
5063 * multiple byte-range specifiers from a 1.1 client implies that the
5064 * client can parse multipart/byteranges responses.
5065 *
5066 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005067 */
5068
5069 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005070 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005071 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005072 * FIXME: should we parse anyway and return an error on chunked encoding ?
5073 */
5074 if (txn->meth == HTTP_METH_HEAD ||
5075 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005076 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005077 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005078 goto skip_content_length;
5079 }
5080
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005081 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005082 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005083 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005084 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005085 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005086 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5087 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005088 /* bad transfer-encoding (chunked followed by something else) */
5089 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005090 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005091 break;
5092 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005093 }
5094
5095 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5096 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005097 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005098 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005099 signed long long cl;
5100
Willy Tarreauad14f752011-09-02 20:33:27 +02005101 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005102 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005103 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005104 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005105
Willy Tarreauad14f752011-09-02 20:33:27 +02005106 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005107 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005108 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005109 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005110
Willy Tarreauad14f752011-09-02 20:33:27 +02005111 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005112 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005113 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005114 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005115
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005116 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005117 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005118 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005119 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005120
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005121 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005122 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005123 }
5124
William Lallemand82fe75c2012-10-23 10:25:10 +02005125 if (s->fe->comp || s->be->comp)
5126 select_compression_response_header(s, rep->buf);
5127
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005128 /* FIXME: we should also implement the multipart/byterange method.
5129 * For now on, we resort to close mode in this case (unknown length).
5130 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005131skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005132
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005133 /* end of job, return OK */
5134 rep->analysers &= ~an_bit;
5135 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005136 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005137 return 1;
5138}
5139
5140/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005141 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5142 * and updates t->rep->analysers. It might make sense to explode it into several
5143 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005144 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005145int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005146{
5147 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005148 struct http_msg *msg = &txn->rsp;
5149 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005150 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005151
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005152 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 +02005153 now_ms, __FUNCTION__,
5154 t,
5155 rep,
5156 rep->rex, rep->wex,
5157 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005158 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005159 rep->analysers);
5160
Willy Tarreau655dce92009-11-08 13:10:58 +01005161 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005162 return 0;
5163
5164 rep->analysers &= ~an_bit;
5165 rep->analyse_exp = TICK_ETERNITY;
5166
Willy Tarreau5b154472009-12-21 20:11:07 +01005167 /* Now we have to check if we need to modify the Connection header.
5168 * This is more difficult on the response than it is on the request,
5169 * because we can have two different HTTP versions and we don't know
5170 * how the client will interprete a response. For instance, let's say
5171 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5172 * HTTP/1.1 response without any header. Maybe it will bound itself to
5173 * HTTP/1.0 because it only knows about it, and will consider the lack
5174 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5175 * the lack of header as a keep-alive. Thus we will use two flags
5176 * indicating how a request MAY be understood by the client. In case
5177 * of multiple possibilities, we'll fix the header to be explicit. If
5178 * ambiguous cases such as both close and keepalive are seen, then we
5179 * will fall back to explicit close. Note that we won't take risks with
5180 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005181 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005182 */
5183
Willy Tarreaudc008c52010-02-01 16:20:08 +01005184 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5185 txn->status == 101)) {
5186 /* Either we've established an explicit tunnel, or we're
5187 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005188 * to understand the next protocols. We have to switch to tunnel
5189 * mode, so that we transfer the request and responses then let
5190 * this protocol pass unmodified. When we later implement specific
5191 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005192 * header which contains information about that protocol for
5193 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005194 */
5195 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5196 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005197 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5198 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5199 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005200 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005201
Willy Tarreau60466522010-01-18 19:08:45 +01005202 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005203 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005204 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5205 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005206
Willy Tarreau60466522010-01-18 19:08:45 +01005207 /* now adjust header transformations depending on current state */
5208 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5209 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5210 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005211 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005212 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005213 }
Willy Tarreau60466522010-01-18 19:08:45 +01005214 else { /* SCL / KAL */
5215 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005216 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005217 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005218 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005219
Willy Tarreau60466522010-01-18 19:08:45 +01005220 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005221 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005222
Willy Tarreau60466522010-01-18 19:08:45 +01005223 /* Some keep-alive responses are converted to Server-close if
5224 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005225 */
Willy Tarreau60466522010-01-18 19:08:45 +01005226 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5227 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005228 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005229 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005230 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005231 }
5232
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005233 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005234 /*
5235 * 3: we will have to evaluate the filters.
5236 * As opposed to version 1.2, now they will be evaluated in the
5237 * filters order and not in the header order. This means that
5238 * each filter has to be validated among all headers.
5239 *
5240 * Filters are tried with ->be first, then with ->fe if it is
5241 * different from ->be.
5242 */
5243
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005244 cur_proxy = t->be;
5245 while (1) {
5246 struct proxy *rule_set = cur_proxy;
5247
5248 /* try headers filters */
5249 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005250 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005251 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005252 if (target_srv(&t->target)) {
5253 target_srv(&t->target)->counters.failed_resp++;
5254 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005255 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005256 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005257 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005258 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005259 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005260 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005261 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005262 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005263 if (!(t->flags & SN_ERR_MASK))
5264 t->flags |= SN_ERR_PRXCOND;
5265 if (!(t->flags & SN_FINST_MASK))
5266 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005267 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005268 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005269 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005270
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005271 /* has the response been denied ? */
5272 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005273 if (target_srv(&t->target))
5274 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005275
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005276 t->be->be_counters.denied_resp++;
5277 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005278 if (t->listener->counters)
5279 t->listener->counters->denied_resp++;
5280
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005281 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005282 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005283
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005284 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005285 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005286 if (txn->status < 200)
5287 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005288 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005289 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005290 ret = acl_pass(ret);
5291 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5292 ret = !ret;
5293 if (!ret)
5294 continue;
5295 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005296 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005297 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005298 }
5299
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005300 /* check whether we're already working on the frontend */
5301 if (cur_proxy == t->fe)
5302 break;
5303 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005304 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005305
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005306 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005307 * We may be facing a 100-continue response, in which case this
5308 * is not the right response, and we're waiting for the next one.
5309 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005310 * next one.
5311 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005312 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005313 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005314 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005315 msg->msg_state = HTTP_MSG_RPBEFORE;
5316 txn->status = 0;
5317 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5318 return 1;
5319 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005320 else if (unlikely(txn->status < 200))
5321 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005322
5323 /* we don't have any 1xx status code now */
5324
5325 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005326 * 4: check for server cookie.
5327 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005328 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5329 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005330 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005331
Willy Tarreaubaaee002006-06-26 02:48:02 +02005332
Willy Tarreaua15645d2007-03-18 16:22:39 +01005333 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005334 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005335 */
Willy Tarreau67402132012-05-31 20:40:20 +02005336 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005337 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005338
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005339 /*
5340 * 6: add server cookie in the response if needed
5341 */
Willy Tarreau67402132012-05-31 20:40:20 +02005342 if (target_srv(&t->target) && (t->be->ck_opts & PR_CK_INS) &&
5343 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005344 (!(t->flags & SN_DIRECT) ||
5345 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5346 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5347 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5348 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005349 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005350 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005351 /* the server is known, it's not the one the client requested, or the
5352 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005353 * insert a set-cookie here, except if we want to insert only on POST
5354 * requests and this one isn't. Note that servers which don't have cookies
5355 * (eg: some backup servers) will return a full cookie removal request.
5356 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005357 if (!target_srv(&t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005358 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005359 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5360 t->be->cookie_name);
5361 }
5362 else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005363 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005364
5365 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5366 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005367 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5368 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5369 trash.len += 5;
5370
Willy Tarreauef4f3912010-10-07 21:00:29 +02005371 if (t->be->cookie_maxlife) {
5372 /* emit first_date, which is either the original one or
5373 * the current date.
5374 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005375 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005376 s30tob64(txn->cookie_first_date ?
5377 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005378 (date.tv_sec+3) >> 2, trash.str + trash.len);
5379 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005380 }
5381 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005382 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005383 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005384
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005385 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005386 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005387
Willy Tarreau4992dd22012-05-31 21:02:17 +02005388 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005389 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005390
5391 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005392 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005393
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005394 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005395 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005396
Willy Tarreauf1348312010-10-07 15:54:11 +02005397 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005398 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005399 /* the server did not change, only the date was updated */
5400 txn->flags |= TX_SCK_UPDATED;
5401 else
5402 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005403
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005404 /* Here, we will tell an eventual cache on the client side that we don't
5405 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5406 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5407 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5408 */
Willy Tarreau67402132012-05-31 20:40:20 +02005409 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005410
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005411 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5412
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005413 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005414 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005415 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005416 }
5417 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005418
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005419 /*
5420 * 7: check if result will be cacheable with a cookie.
5421 * We'll block the response if security checks have caught
5422 * nasty things such as a cacheable cookie.
5423 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005424 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5425 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005426 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005427
5428 /* we're in presence of a cacheable response containing
5429 * a set-cookie header. We'll block it as requested by
5430 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005431 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005432 if (target_srv(&t->target))
5433 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005434
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005435 t->be->be_counters.denied_resp++;
5436 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005437 if (t->listener->counters)
5438 t->listener->counters->denied_resp++;
5439
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005440 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005441 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005442 send_log(t->be, LOG_ALERT,
5443 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005444 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005445 goto return_srv_prx_502;
5446 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005447
5448 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005449 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005450 */
Willy Tarreau60466522010-01-18 19:08:45 +01005451 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5452 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5453 unsigned int want_flags = 0;
5454
5455 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5456 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5457 /* we want a keep-alive response here. Keep-alive header
5458 * required if either side is not 1.1.
5459 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005460 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005461 want_flags |= TX_CON_KAL_SET;
5462 }
5463 else {
5464 /* we want a close response here. Close header required if
5465 * the server is 1.1, regardless of the client.
5466 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005467 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005468 want_flags |= TX_CON_CLO_SET;
5469 }
5470
5471 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005472 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005473 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005474
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005475 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005476 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005477 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005478 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005479
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005480 /*************************************************************
5481 * OK, that's finished for the headers. We have done what we *
5482 * could. Let's switch to the DATA state. *
5483 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005484
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005485 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005486
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005487 /* if the user wants to log as soon as possible, without counting
5488 * bytes from the server, then this is the right moment. We have
5489 * to temporarily assign bytes_out to log what we currently have.
5490 */
5491 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5492 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5493 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005494 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005495 t->logs.bytes_out = 0;
5496 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005497
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005498 /* Note: we must not try to cheat by jumping directly to DATA,
5499 * otherwise we would not let the client side wake up.
5500 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005501
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005502 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005503 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005504 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005505}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005506
Willy Tarreaud98cf932009-12-27 22:54:55 +01005507/* This function is an analyser which forwards response body (including chunk
5508 * sizes if any). It is called as soon as we must forward, even if we forward
5509 * zero byte. The only situation where it must not be called is when we're in
5510 * tunnel mode and we want to forward till the close. It's used both to forward
5511 * remaining data and to resync after end of body. It expects the msg_state to
5512 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5513 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005514 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005515 * bytes of pending data + the headers if not already done (between sol and sov).
5516 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005517 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005518int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005519{
5520 struct http_txn *txn = &s->txn;
5521 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005522 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005523 static struct buffer *tmpbuf = NULL;
5524 int compressing = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005525
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005526 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5527 return 0;
5528
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005529 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005530 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005531 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005532 /* Output closed while we were sending data. We must abort and
5533 * wake the other side up.
5534 */
5535 msg->msg_state = HTTP_MSG_ERROR;
5536 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005537 return 1;
5538 }
5539
Willy Tarreau4fe41902010-06-07 22:27:41 +02005540 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005541 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005542
William Lallemand82fe75c2012-10-23 10:25:10 +02005543 /* this is the first time we need the compression buffer */
5544 if (s->comp_algo != NULL && tmpbuf == NULL) {
5545 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5546 goto aborted_xfer; /* no memory */
5547 }
5548
Willy Tarreaud98cf932009-12-27 22:54:55 +01005549 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005550 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005551 * rep->buf.p still points to the beginning of the message and msg->sol
5552 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005553 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005554 channel_forward(res, msg->sov);
5555 msg->next = 0;
5556 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005557
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005558 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005559 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005560 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005561 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005562 }
5563
William Lallemand82fe75c2012-10-23 10:25:10 +02005564 if (s->comp_algo != NULL) {
5565 int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
5566 if (ret < 0)
5567 goto missing_data; /* not enough spaces in buffers */
5568 compressing = 1;
5569 }
5570
Willy Tarreaud98cf932009-12-27 22:54:55 +01005571 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005572 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005573 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005574 if (s->comp_algo == NULL) {
5575 bytes = msg->sov - msg->sol;
5576 if (msg->chunk_len || bytes) {
5577 msg->sol = msg->sov;
5578 msg->next -= bytes; /* will be forwarded */
5579 msg->chunk_len += bytes;
5580 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5581 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005582 }
5583
Willy Tarreaucaabe412010-01-03 23:08:28 +01005584 if (msg->msg_state == HTTP_MSG_DATA) {
5585 /* must still forward */
William Lallemand82fe75c2012-10-23 10:25:10 +02005586 if (compressing)
5587 http_compression_buffer_add_data(s, res->buf, tmpbuf);
5588
5589 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005590 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005591
5592 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005593 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005594 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005595 else
5596 msg->msg_state = HTTP_MSG_DONE;
5597 }
5598 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005599 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005600 * set ->sov and ->next to point to the body and switch to DATA or
5601 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005602 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005603 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005604
Willy Tarreau54d23df2012-10-25 19:04:45 +02005605 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005606 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005607 else if (ret < 0) {
5608 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005609 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005610 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005611 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005612 /* skipping data if we are in compression mode */
5613 if (compressing && msg->chunk_len > 0) {
5614 b_adv(res->buf, msg->next);
5615 msg->next = 0;
5616 msg->sov = 0;
5617 msg->sol = 0;
5618 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005619 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005620 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005621 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005622 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005623 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005624
Willy Tarreau54d23df2012-10-25 19:04:45 +02005625 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005626 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005627 else if (ret < 0) {
5628 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005629 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005630 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005631 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005632 /* skipping data in buffer for compression */
5633 if (compressing) {
5634 b_adv(res->buf, msg->next);
5635 msg->next = 0;
5636 msg->sov = 0;
5637 msg->sol = 0;
5638 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005639 /* we're in MSG_CHUNK_SIZE now */
5640 }
5641 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005642 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005643
Willy Tarreaud98cf932009-12-27 22:54:55 +01005644 if (ret == 0)
5645 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005646 else if (ret < 0) {
5647 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005648 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005649 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005650 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005651 if (compressing) {
5652 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5653 compressing = 0;
5654 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005655 /* we're in HTTP_MSG_DONE now */
5656 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005657 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005658 int old_state = msg->msg_state;
5659
William Lallemand82fe75c2012-10-23 10:25:10 +02005660 if (compressing) {
5661 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5662 compressing = 0;
5663 }
5664
Willy Tarreau610ecce2010-01-04 21:15:02 +01005665 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005666 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005667 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5668 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005669 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005670 if (http_resync_states(s)) {
5671 http_silent_debug(__LINE__, s);
5672 /* some state changes occurred, maybe the analyser
5673 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005674 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005675 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005676 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005677 /* response errors are most likely due to
5678 * the client aborting the transfer.
5679 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005680 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005681 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005682 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005683 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005684 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005685 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005686 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005687 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005688 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005689 }
5690 }
5691
Willy Tarreaud98cf932009-12-27 22:54:55 +01005692 missing_data:
William Lallemand82fe75c2012-10-23 10:25:10 +02005693 if (compressing) {
5694 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5695 compressing = 0;
5696 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005697 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005698 if (res->flags & CF_SHUTR) {
Willy Tarreau40dba092010-03-04 18:14:51 +01005699 if (!(s->flags & SN_ERR_MASK))
5700 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005701 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005702 if (target_srv(&s->target))
5703 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005704 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005705 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005706
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005707 if (res->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005708 goto aborted_xfer;
5709
Willy Tarreau40dba092010-03-04 18:14:51 +01005710 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005711 if (!s->req->analysers)
5712 goto return_bad_res;
5713
Willy Tarreauea953162012-05-18 23:41:28 +02005714 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005715 if (s->comp_algo == NULL) {
5716 bytes = msg->sov - msg->sol;
5717 if (msg->chunk_len || bytes) {
5718 msg->sol = msg->sov;
5719 msg->next -= bytes; /* will be forwarded */
5720 msg->chunk_len += bytes;
5721 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5722 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005723 }
5724
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005725 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005726 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005727 * Similarly, with keep-alive on the client side, we don't want to forward a
5728 * close.
5729 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005730 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005731 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5732 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005733 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005734
Willy Tarreau5c620922011-05-11 19:56:11 +02005735 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005736 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005737 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005738 * modes are already handled by the stream sock layer. We must not do
5739 * this in content-length mode because it could present the MSG_MORE
5740 * flag with the last block of forwarded data, which would cause an
5741 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005742 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005743 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005744 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005745
Willy Tarreaud98cf932009-12-27 22:54:55 +01005746 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005747 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005748 return 0;
5749
Willy Tarreau40dba092010-03-04 18:14:51 +01005750 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005751 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005752 if (target_srv(&s->target))
5753 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005754
5755 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005756 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005757 /* don't send any error message as we're in the body */
5758 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005759 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005760 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005761 if (target_srv(&s->target))
5762 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005763
5764 if (!(s->flags & SN_ERR_MASK))
5765 s->flags |= SN_ERR_PRXCOND;
5766 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005767 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005768 return 0;
5769
5770 aborted_xfer:
5771 txn->rsp.msg_state = HTTP_MSG_ERROR;
5772 /* don't send any error message as we're in the body */
5773 stream_int_retnclose(res->cons, NULL);
5774 res->analysers = 0;
5775 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5776
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005777 s->fe->fe_counters.cli_aborts++;
5778 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005779 if (target_srv(&s->target))
5780 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005781
5782 if (!(s->flags & SN_ERR_MASK))
5783 s->flags |= SN_ERR_CLICL;
5784 if (!(s->flags & SN_FINST_MASK))
5785 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005786 return 0;
5787}
5788
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005789/* Iterate the same filter through all request headers.
5790 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005791 * Since it can manage the switch to another backend, it updates the per-proxy
5792 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005793 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005794int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005795{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005796 char term;
5797 char *cur_ptr, *cur_end, *cur_next;
5798 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005799 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005800 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005801 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005802
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005803 last_hdr = 0;
5804
Willy Tarreau9b28e032012-10-12 23:49:43 +02005805 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005806 old_idx = 0;
5807
5808 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005809 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005810 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005811 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005812 (exp->action == ACT_ALLOW ||
5813 exp->action == ACT_DENY ||
5814 exp->action == ACT_TARPIT))
5815 return 0;
5816
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005817 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005818 if (!cur_idx)
5819 break;
5820
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005821 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005822 cur_ptr = cur_next;
5823 cur_end = cur_ptr + cur_hdr->len;
5824 cur_next = cur_end + cur_hdr->cr + 1;
5825
5826 /* Now we have one header between cur_ptr and cur_end,
5827 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005828 */
5829
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005830 /* The annoying part is that pattern matching needs
5831 * that we modify the contents to null-terminate all
5832 * strings before testing them.
5833 */
5834
5835 term = *cur_end;
5836 *cur_end = '\0';
5837
5838 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5839 switch (exp->action) {
5840 case ACT_SETBE:
5841 /* It is not possible to jump a second time.
5842 * FIXME: should we return an HTTP/500 here so that
5843 * the admin knows there's a problem ?
5844 */
5845 if (t->be != t->fe)
5846 break;
5847
5848 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005849 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005850 last_hdr = 1;
5851 break;
5852
5853 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005854 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005855 last_hdr = 1;
5856 break;
5857
5858 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005859 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005860 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005861
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005862 t->fe->fe_counters.denied_req++;
5863 if (t->fe != t->be)
5864 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005865 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005866 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005867
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005868 break;
5869
5870 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005871 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005872 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005873
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005874 t->fe->fe_counters.denied_req++;
5875 if (t->fe != t->be)
5876 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005877 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005878 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005879
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005880 break;
5881
5882 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005883 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
5884 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005885 /* FIXME: if the user adds a newline in the replacement, the
5886 * index will not be recalculated for now, and the new line
5887 * will not be counted as a new header.
5888 */
5889
5890 cur_end += delta;
5891 cur_next += delta;
5892 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005893 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005894 break;
5895
5896 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02005897 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005898 cur_next += delta;
5899
Willy Tarreaufa355d42009-11-29 18:12:29 +01005900 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005901 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5902 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005903 cur_hdr->len = 0;
5904 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005905 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005906 break;
5907
5908 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005909 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005910 if (cur_end)
5911 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005912
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005913 /* keep the link from this header to next one in case of later
5914 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005915 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005916 old_idx = cur_idx;
5917 }
5918 return 0;
5919}
5920
5921
5922/* Apply the filter to the request line.
5923 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5924 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005925 * Since it can manage the switch to another backend, it updates the per-proxy
5926 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005927 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005928int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005929{
5930 char term;
5931 char *cur_ptr, *cur_end;
5932 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005933 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005934 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005935
Willy Tarreau3d300592007-03-18 18:34:41 +01005936 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005937 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005938 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005939 (exp->action == ACT_ALLOW ||
5940 exp->action == ACT_DENY ||
5941 exp->action == ACT_TARPIT))
5942 return 0;
5943 else if (exp->action == ACT_REMOVE)
5944 return 0;
5945
5946 done = 0;
5947
Willy Tarreau9b28e032012-10-12 23:49:43 +02005948 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005949 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005950
5951 /* Now we have the request line between cur_ptr and cur_end */
5952
5953 /* The annoying part is that pattern matching needs
5954 * that we modify the contents to null-terminate all
5955 * strings before testing them.
5956 */
5957
5958 term = *cur_end;
5959 *cur_end = '\0';
5960
5961 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5962 switch (exp->action) {
5963 case ACT_SETBE:
5964 /* It is not possible to jump a second time.
5965 * FIXME: should we return an HTTP/500 here so that
5966 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005967 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968 if (t->be != t->fe)
5969 break;
5970
5971 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005972 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005973 done = 1;
5974 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005975
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005976 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005977 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005978 done = 1;
5979 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005980
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005981 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005982 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005983
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005984 t->fe->fe_counters.denied_req++;
5985 if (t->fe != t->be)
5986 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005987 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005988 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005989
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005990 done = 1;
5991 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005992
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005993 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005994 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005995
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005996 t->fe->fe_counters.denied_req++;
5997 if (t->fe != t->be)
5998 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005999 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006000 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006001
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006002 done = 1;
6003 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006004
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006005 case ACT_REPLACE:
6006 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006007 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6008 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006009 /* FIXME: if the user adds a newline in the replacement, the
6010 * index will not be recalculated for now, and the new line
6011 * will not be counted as a new header.
6012 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006013
Willy Tarreaufa355d42009-11-29 18:12:29 +01006014 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006015 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006016 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006017 HTTP_MSG_RQMETH,
6018 cur_ptr, cur_end + 1,
6019 NULL, NULL);
6020 if (unlikely(!cur_end))
6021 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006022
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006023 /* we have a full request and we know that we have either a CR
6024 * or an LF at <ptr>.
6025 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006026 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6027 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006028 /* there is no point trying this regex on headers */
6029 return 1;
6030 }
6031 }
6032 *cur_end = term; /* restore the string terminator */
6033 return done;
6034}
Willy Tarreau97de6242006-12-27 17:18:38 +01006035
Willy Tarreau58f10d72006-12-04 02:26:12 +01006036
Willy Tarreau58f10d72006-12-04 02:26:12 +01006037
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006038/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006039 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006040 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006041 * unparsable request. Since it can manage the switch to another backend, it
6042 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006043 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006044int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006045{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006046 struct http_txn *txn = &s->txn;
6047 struct hdr_exp *exp;
6048
6049 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006050 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006051
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006052 /*
6053 * The interleaving of transformations and verdicts
6054 * makes it difficult to decide to continue or stop
6055 * the evaluation.
6056 */
6057
Willy Tarreau6c123b12010-01-28 20:22:06 +01006058 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6059 break;
6060
Willy Tarreau3d300592007-03-18 18:34:41 +01006061 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006062 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006063 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006064 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006065
6066 /* if this filter had a condition, evaluate it now and skip to
6067 * next filter if the condition does not match.
6068 */
6069 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006070 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006071 ret = acl_pass(ret);
6072 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6073 ret = !ret;
6074
6075 if (!ret)
6076 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006077 }
6078
6079 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006080 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006081 if (unlikely(ret < 0))
6082 return -1;
6083
6084 if (likely(ret == 0)) {
6085 /* The filter did not match the request, it can be
6086 * iterated through all headers.
6087 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006088 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006089 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006090 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006092}
6093
6094
Willy Tarreaua15645d2007-03-18 16:22:39 +01006095
Willy Tarreau58f10d72006-12-04 02:26:12 +01006096/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006097 * Try to retrieve the server associated to the appsession.
6098 * If the server is found, it's assigned to the session.
6099 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006100void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006101 struct http_txn *txn = &t->txn;
6102 appsess *asession = NULL;
6103 char *sessid_temp = NULL;
6104
Cyril Bontéb21570a2009-11-29 20:04:48 +01006105 if (len > t->be->appsession_len) {
6106 len = t->be->appsession_len;
6107 }
6108
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006109 if (t->be->options2 & PR_O2_AS_REQL) {
6110 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006111 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006112 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006113 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006114 }
6115
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006116 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006117 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
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006122 memcpy(txn->sessid, buf, len);
6123 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006124 }
6125
6126 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6127 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6128 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6129 return;
6130 }
6131
Cyril Bontéb21570a2009-11-29 20:04:48 +01006132 memcpy(sessid_temp, buf, len);
6133 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006134
6135 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6136 /* free previously allocated memory */
6137 pool_free2(apools.sessid, sessid_temp);
6138
6139 if (asession != NULL) {
6140 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6141 if (!(t->be->options2 & PR_O2_AS_REQL))
6142 asession->request_count++;
6143
6144 if (asession->serverid != NULL) {
6145 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006146
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006147 while (srv) {
6148 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006149 if ((srv->state & SRV_RUNNING) ||
6150 (t->be->options & PR_O_PERSIST) ||
6151 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006152 /* we found the server and it's usable */
6153 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006154 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006155 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006156 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006157
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006158 break;
6159 } else {
6160 txn->flags &= ~TX_CK_MASK;
6161 txn->flags |= TX_CK_DOWN;
6162 }
6163 }
6164 srv = srv->next;
6165 }
6166 }
6167 }
6168}
6169
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006170/* Find the end of a cookie value contained between <s> and <e>. It works the
6171 * same way as with headers above except that the semi-colon also ends a token.
6172 * See RFC2965 for more information. Note that it requires a valid header to
6173 * return a valid result.
6174 */
6175char *find_cookie_value_end(char *s, const char *e)
6176{
6177 int quoted, qdpair;
6178
6179 quoted = qdpair = 0;
6180 for (; s < e; s++) {
6181 if (qdpair) qdpair = 0;
6182 else if (quoted) {
6183 if (*s == '\\') qdpair = 1;
6184 else if (*s == '"') quoted = 0;
6185 }
6186 else if (*s == '"') quoted = 1;
6187 else if (*s == ',' || *s == ';') return s;
6188 }
6189 return s;
6190}
6191
6192/* Delete a value in a header between delimiters <from> and <next> in buffer
6193 * <buf>. The number of characters displaced is returned, and the pointer to
6194 * the first delimiter is updated if required. The function tries as much as
6195 * possible to respect the following principles :
6196 * - replace <from> delimiter by the <next> one unless <from> points to a
6197 * colon, in which case <next> is simply removed
6198 * - set exactly one space character after the new first delimiter, unless
6199 * there are not enough characters in the block being moved to do so.
6200 * - remove unneeded spaces before the previous delimiter and after the new
6201 * one.
6202 *
6203 * It is the caller's responsibility to ensure that :
6204 * - <from> points to a valid delimiter or the colon ;
6205 * - <next> points to a valid delimiter or the final CR/LF ;
6206 * - there are non-space chars before <from> ;
6207 * - there is a CR/LF at or after <next>.
6208 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006209int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006210{
6211 char *prev = *from;
6212
6213 if (*prev == ':') {
6214 /* We're removing the first value, preserve the colon and add a
6215 * space if possible.
6216 */
6217 if (!http_is_crlf[(unsigned char)*next])
6218 next++;
6219 prev++;
6220 if (prev < next)
6221 *prev++ = ' ';
6222
6223 while (http_is_spht[(unsigned char)*next])
6224 next++;
6225 } else {
6226 /* Remove useless spaces before the old delimiter. */
6227 while (http_is_spht[(unsigned char)*(prev-1)])
6228 prev--;
6229 *from = prev;
6230
6231 /* copy the delimiter and if possible a space if we're
6232 * not at the end of the line.
6233 */
6234 if (!http_is_crlf[(unsigned char)*next]) {
6235 *prev++ = *next++;
6236 if (prev + 1 < next)
6237 *prev++ = ' ';
6238 while (http_is_spht[(unsigned char)*next])
6239 next++;
6240 }
6241 }
6242 return buffer_replace2(buf, prev, next, NULL, 0);
6243}
6244
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006245/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006246 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006247 * desirable to call it only when needed. This code is quite complex because
6248 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6249 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006251void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006252{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006253 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006254 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006255 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006256 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6257 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006259 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006260 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006261 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006262
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006263 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006264 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006265 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006266
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006267 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006268 hdr_beg = hdr_next;
6269 hdr_end = hdr_beg + cur_hdr->len;
6270 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006272 /* We have one full header between hdr_beg and hdr_end, and the
6273 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006274 * "Cookie:" headers.
6275 */
6276
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006277 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006278 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006279 old_idx = cur_idx;
6280 continue;
6281 }
6282
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006283 del_from = NULL; /* nothing to be deleted */
6284 preserve_hdr = 0; /* assume we may kill the whole header */
6285
Willy Tarreau58f10d72006-12-04 02:26:12 +01006286 /* Now look for cookies. Conforming to RFC2109, we have to support
6287 * attributes whose name begin with a '$', and associate them with
6288 * the right cookie, if we want to delete this cookie.
6289 * So there are 3 cases for each cookie read :
6290 * 1) it's a special attribute, beginning with a '$' : ignore it.
6291 * 2) it's a server id cookie that we *MAY* want to delete : save
6292 * some pointers on it (last semi-colon, beginning of cookie...)
6293 * 3) it's an application cookie : we *MAY* have to delete a previous
6294 * "special" cookie.
6295 * At the end of loop, if a "special" cookie remains, we may have to
6296 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006297 * *MUST* delete it.
6298 *
6299 * Note: RFC2965 is unclear about the processing of spaces around
6300 * the equal sign in the ATTR=VALUE form. A careful inspection of
6301 * the RFC explicitly allows spaces before it, and not within the
6302 * tokens (attrs or values). An inspection of RFC2109 allows that
6303 * too but section 10.1.3 lets one think that spaces may be allowed
6304 * after the equal sign too, resulting in some (rare) buggy
6305 * implementations trying to do that. So let's do what servers do.
6306 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6307 * allowed quoted strings in values, with any possible character
6308 * after a backslash, including control chars and delimitors, which
6309 * causes parsing to become ambiguous. Browsers also allow spaces
6310 * within values even without quotes.
6311 *
6312 * We have to keep multiple pointers in order to support cookie
6313 * removal at the beginning, middle or end of header without
6314 * corrupting the header. All of these headers are valid :
6315 *
6316 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6317 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6318 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6319 * | | | | | | | | |
6320 * | | | | | | | | hdr_end <--+
6321 * | | | | | | | +--> next
6322 * | | | | | | +----> val_end
6323 * | | | | | +-----------> val_beg
6324 * | | | | +--------------> equal
6325 * | | | +----------------> att_end
6326 * | | +---------------------> att_beg
6327 * | +--------------------------> prev
6328 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006329 */
6330
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006331 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6332 /* Iterate through all cookies on this line */
6333
6334 /* find att_beg */
6335 att_beg = prev + 1;
6336 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6337 att_beg++;
6338
6339 /* find att_end : this is the first character after the last non
6340 * space before the equal. It may be equal to hdr_end.
6341 */
6342 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006343
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 while (equal < hdr_end) {
6345 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006346 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006347 if (http_is_spht[(unsigned char)*equal++])
6348 continue;
6349 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006350 }
6351
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006352 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6353 * is between <att_beg> and <equal>, both may be identical.
6354 */
6355
6356 /* look for end of cookie if there is an equal sign */
6357 if (equal < hdr_end && *equal == '=') {
6358 /* look for the beginning of the value */
6359 val_beg = equal + 1;
6360 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6361 val_beg++;
6362
6363 /* find the end of the value, respecting quotes */
6364 next = find_cookie_value_end(val_beg, hdr_end);
6365
6366 /* make val_end point to the first white space or delimitor after the value */
6367 val_end = next;
6368 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6369 val_end--;
6370 } else {
6371 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006372 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006373
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006374 /* We have nothing to do with attributes beginning with '$'. However,
6375 * they will automatically be removed if a header before them is removed,
6376 * since they're supposed to be linked together.
6377 */
6378 if (*att_beg == '$')
6379 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006380
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006381 /* Ignore cookies with no equal sign */
6382 if (equal == next) {
6383 /* This is not our cookie, so we must preserve it. But if we already
6384 * scheduled another cookie for removal, we cannot remove the
6385 * complete header, but we can remove the previous block itself.
6386 */
6387 preserve_hdr = 1;
6388 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006389 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006390 val_end += delta;
6391 next += delta;
6392 hdr_end += delta;
6393 hdr_next += delta;
6394 cur_hdr->len += delta;
6395 http_msg_move_end(&txn->req, delta);
6396 prev = del_from;
6397 del_from = NULL;
6398 }
6399 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006400 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006401
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006402 /* if there are spaces around the equal sign, we need to
6403 * strip them otherwise we'll get trouble for cookie captures,
6404 * or even for rewrites. Since this happens extremely rarely,
6405 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006406 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006407 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6408 int stripped_before = 0;
6409 int stripped_after = 0;
6410
6411 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006412 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006413 equal += stripped_before;
6414 val_beg += stripped_before;
6415 }
6416
6417 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006418 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006419 val_beg += stripped_after;
6420 stripped_before += stripped_after;
6421 }
6422
6423 val_end += stripped_before;
6424 next += stripped_before;
6425 hdr_end += stripped_before;
6426 hdr_next += stripped_before;
6427 cur_hdr->len += stripped_before;
6428 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006429 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006430 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006431
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006432 /* First, let's see if we want to capture this cookie. We check
6433 * that we don't already have a client side cookie, because we
6434 * can only capture one. Also as an optimisation, we ignore
6435 * cookies shorter than the declared name.
6436 */
6437 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6438 (val_end - att_beg >= t->fe->capture_namelen) &&
6439 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6440 int log_len = val_end - att_beg;
6441
6442 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6443 Alert("HTTP logging : out of memory.\n");
6444 } else {
6445 if (log_len > t->fe->capture_len)
6446 log_len = t->fe->capture_len;
6447 memcpy(txn->cli_cookie, att_beg, log_len);
6448 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006449 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006450 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006451
Willy Tarreaubca99692010-10-06 19:25:55 +02006452 /* Persistence cookies in passive, rewrite or insert mode have the
6453 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006454 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006455 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006456 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006457 * For cookies in prefix mode, the form is :
6458 *
6459 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006460 */
6461 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6462 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6463 struct server *srv = t->be->srv;
6464 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006465
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006466 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6467 * have the server ID between val_beg and delim, and the original cookie between
6468 * delim+1 and val_end. Otherwise, delim==val_end :
6469 *
6470 * Cookie: NAME=SRV; # in all but prefix modes
6471 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6472 * | || || | |+-> next
6473 * | || || | +--> val_end
6474 * | || || +---------> delim
6475 * | || |+------------> val_beg
6476 * | || +-------------> att_end = equal
6477 * | |+-----------------> att_beg
6478 * | +------------------> prev
6479 * +-------------------------> hdr_beg
6480 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006481
Willy Tarreau67402132012-05-31 20:40:20 +02006482 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006483 for (delim = val_beg; delim < val_end; delim++)
6484 if (*delim == COOKIE_DELIM)
6485 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006486 } else {
6487 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006488 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006489 /* Now check if the cookie contains a date field, which would
6490 * appear after a vertical bar ('|') just after the server name
6491 * and before the delimiter.
6492 */
6493 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6494 if (vbar1) {
6495 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006496 * right is the last seen date. It is a base64 encoded
6497 * 30-bit value representing the UNIX date since the
6498 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006499 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006500 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006501 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006502 if (val_end - vbar1 >= 5) {
6503 val = b64tos30(vbar1);
6504 if (val > 0)
6505 txn->cookie_last_date = val << 2;
6506 }
6507 /* look for a second vertical bar */
6508 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6509 if (vbar1 && (val_end - vbar1 > 5)) {
6510 val = b64tos30(vbar1 + 1);
6511 if (val > 0)
6512 txn->cookie_first_date = val << 2;
6513 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006514 }
6515 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006516
Willy Tarreauf64d1412010-10-07 20:06:11 +02006517 /* if the cookie has an expiration date and the proxy wants to check
6518 * it, then we do that now. We first check if the cookie is too old,
6519 * then only if it has expired. We detect strict overflow because the
6520 * time resolution here is not great (4 seconds). Cookies with dates
6521 * in the future are ignored if their offset is beyond one day. This
6522 * allows an admin to fix timezone issues without expiring everyone
6523 * and at the same time avoids keeping unwanted side effects for too
6524 * long.
6525 */
6526 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006527 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6528 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006529 txn->flags &= ~TX_CK_MASK;
6530 txn->flags |= TX_CK_OLD;
6531 delim = val_beg; // let's pretend we have not found the cookie
6532 txn->cookie_first_date = 0;
6533 txn->cookie_last_date = 0;
6534 }
6535 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006536 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6537 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006538 txn->flags &= ~TX_CK_MASK;
6539 txn->flags |= TX_CK_EXPIRED;
6540 delim = val_beg; // let's pretend we have not found the cookie
6541 txn->cookie_first_date = 0;
6542 txn->cookie_last_date = 0;
6543 }
6544
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006545 /* Here, we'll look for the first running server which supports the cookie.
6546 * This allows to share a same cookie between several servers, for example
6547 * to dedicate backup servers to specific servers only.
6548 * However, to prevent clients from sticking to cookie-less backup server
6549 * when they have incidentely learned an empty cookie, we simply ignore
6550 * empty cookies and mark them as invalid.
6551 * The same behaviour is applied when persistence must be ignored.
6552 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006553 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006554 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006555
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006556 while (srv) {
6557 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6558 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6559 if ((srv->state & SRV_RUNNING) ||
6560 (t->be->options & PR_O_PERSIST) ||
6561 (t->flags & SN_FORCE_PRST)) {
6562 /* we found the server and we can use it */
6563 txn->flags &= ~TX_CK_MASK;
6564 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6565 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006566 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006567 break;
6568 } else {
6569 /* we found a server, but it's down,
6570 * mark it as such and go on in case
6571 * another one is available.
6572 */
6573 txn->flags &= ~TX_CK_MASK;
6574 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006575 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006576 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006577 srv = srv->next;
6578 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006579
Willy Tarreauf64d1412010-10-07 20:06:11 +02006580 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006581 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006582 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006583 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6584 txn->flags |= TX_CK_UNUSED;
6585 else
6586 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006587 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006589 /* depending on the cookie mode, we may have to either :
6590 * - delete the complete cookie if we're in insert+indirect mode, so that
6591 * the server never sees it ;
6592 * - remove the server id from the cookie value, and tag the cookie as an
6593 * application cookie so that it does not get accidentely removed later,
6594 * if we're in cookie prefix mode
6595 */
Willy Tarreau67402132012-05-31 20:40:20 +02006596 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006597 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006598
Willy Tarreau9b28e032012-10-12 23:49:43 +02006599 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006600 val_end += delta;
6601 next += delta;
6602 hdr_end += delta;
6603 hdr_next += delta;
6604 cur_hdr->len += delta;
6605 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006606
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006607 del_from = NULL;
6608 preserve_hdr = 1; /* we want to keep this cookie */
6609 }
6610 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006611 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006612 del_from = prev;
6613 }
6614 } else {
6615 /* This is not our cookie, so we must preserve it. But if we already
6616 * scheduled another cookie for removal, we cannot remove the
6617 * complete header, but we can remove the previous block itself.
6618 */
6619 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006620
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006621 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006622 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006623 if (att_beg >= del_from)
6624 att_beg += delta;
6625 if (att_end >= del_from)
6626 att_end += delta;
6627 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006628 val_end += delta;
6629 next += delta;
6630 hdr_end += delta;
6631 hdr_next += delta;
6632 cur_hdr->len += delta;
6633 http_msg_move_end(&txn->req, delta);
6634 prev = del_from;
6635 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006636 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006637 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006638
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006639 /* Look for the appsession cookie unless persistence must be ignored */
6640 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6641 int cmp_len, value_len;
6642 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006643
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006644 if (t->be->options2 & PR_O2_AS_PFX) {
6645 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6646 value_begin = att_beg + t->be->appsession_name_len;
6647 value_len = val_end - att_beg - t->be->appsession_name_len;
6648 } else {
6649 cmp_len = att_end - att_beg;
6650 value_begin = val_beg;
6651 value_len = val_end - val_beg;
6652 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006653
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006654 /* let's see if the cookie is our appcookie */
6655 if (cmp_len == t->be->appsession_name_len &&
6656 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6657 manage_client_side_appsession(t, value_begin, value_len);
6658 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006659 }
6660
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006661 /* continue with next cookie on this header line */
6662 att_beg = next;
6663 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006664
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006665 /* There are no more cookies on this line.
6666 * We may still have one (or several) marked for deletion at the
6667 * end of the line. We must do this now in two ways :
6668 * - if some cookies must be preserved, we only delete from the
6669 * mark to the end of line ;
6670 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006671 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006672 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006673 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006674 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006675 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006676 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006677 cur_hdr->len += delta;
6678 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006679 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006680
6681 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006682 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6683 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006684 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006685 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006686 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006687 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006688 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006689 }
6690
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006691 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006692 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006693 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006694}
6695
6696
Willy Tarreaua15645d2007-03-18 16:22:39 +01006697/* Iterate the same filter through all response headers contained in <rtr>.
6698 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6699 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006700int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006701{
6702 char term;
6703 char *cur_ptr, *cur_end, *cur_next;
6704 int cur_idx, old_idx, last_hdr;
6705 struct http_txn *txn = &t->txn;
6706 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006707 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708
6709 last_hdr = 0;
6710
Willy Tarreau9b28e032012-10-12 23:49:43 +02006711 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006712 old_idx = 0;
6713
6714 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006715 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006716 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006717 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006718 (exp->action == ACT_ALLOW ||
6719 exp->action == ACT_DENY))
6720 return 0;
6721
6722 cur_idx = txn->hdr_idx.v[old_idx].next;
6723 if (!cur_idx)
6724 break;
6725
6726 cur_hdr = &txn->hdr_idx.v[cur_idx];
6727 cur_ptr = cur_next;
6728 cur_end = cur_ptr + cur_hdr->len;
6729 cur_next = cur_end + cur_hdr->cr + 1;
6730
6731 /* Now we have one header between cur_ptr and cur_end,
6732 * and the next header starts at cur_next.
6733 */
6734
6735 /* The annoying part is that pattern matching needs
6736 * that we modify the contents to null-terminate all
6737 * strings before testing them.
6738 */
6739
6740 term = *cur_end;
6741 *cur_end = '\0';
6742
6743 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6744 switch (exp->action) {
6745 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006746 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006747 last_hdr = 1;
6748 break;
6749
6750 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006751 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006752 last_hdr = 1;
6753 break;
6754
6755 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006756 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6757 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006758 /* FIXME: if the user adds a newline in the replacement, the
6759 * index will not be recalculated for now, and the new line
6760 * will not be counted as a new header.
6761 */
6762
6763 cur_end += delta;
6764 cur_next += delta;
6765 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006766 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006767 break;
6768
6769 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006770 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771 cur_next += delta;
6772
Willy Tarreaufa355d42009-11-29 18:12:29 +01006773 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006774 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6775 txn->hdr_idx.used--;
6776 cur_hdr->len = 0;
6777 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006778 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006779 break;
6780
6781 }
6782 }
6783 if (cur_end)
6784 *cur_end = term; /* restore the string terminator */
6785
6786 /* keep the link from this header to next one in case of later
6787 * removal of next header.
6788 */
6789 old_idx = cur_idx;
6790 }
6791 return 0;
6792}
6793
6794
6795/* Apply the filter to the status line in the response buffer <rtr>.
6796 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6797 * or -1 if a replacement resulted in an invalid status line.
6798 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006799int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006800{
6801 char term;
6802 char *cur_ptr, *cur_end;
6803 int done;
6804 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006805 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006806
6807
Willy Tarreau3d300592007-03-18 18:34:41 +01006808 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006809 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006810 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006811 (exp->action == ACT_ALLOW ||
6812 exp->action == ACT_DENY))
6813 return 0;
6814 else if (exp->action == ACT_REMOVE)
6815 return 0;
6816
6817 done = 0;
6818
Willy Tarreau9b28e032012-10-12 23:49:43 +02006819 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006820 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006821
6822 /* Now we have the status line between cur_ptr and cur_end */
6823
6824 /* The annoying part is that pattern matching needs
6825 * that we modify the contents to null-terminate all
6826 * strings before testing them.
6827 */
6828
6829 term = *cur_end;
6830 *cur_end = '\0';
6831
6832 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6833 switch (exp->action) {
6834 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006835 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006836 done = 1;
6837 break;
6838
6839 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006840 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006841 done = 1;
6842 break;
6843
6844 case ACT_REPLACE:
6845 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006846 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6847 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006848 /* FIXME: if the user adds a newline in the replacement, the
6849 * index will not be recalculated for now, and the new line
6850 * will not be counted as a new header.
6851 */
6852
Willy Tarreaufa355d42009-11-29 18:12:29 +01006853 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006854 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006855 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006856 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006857 cur_ptr, cur_end + 1,
6858 NULL, NULL);
6859 if (unlikely(!cur_end))
6860 return -1;
6861
6862 /* we have a full respnse and we know that we have either a CR
6863 * or an LF at <ptr>.
6864 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006865 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006866 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006867 /* there is no point trying this regex on headers */
6868 return 1;
6869 }
6870 }
6871 *cur_end = term; /* restore the string terminator */
6872 return done;
6873}
6874
6875
6876
6877/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006878 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006879 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6880 * unparsable response.
6881 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006882int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006883{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006884 struct http_txn *txn = &s->txn;
6885 struct hdr_exp *exp;
6886
6887 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006888 int ret;
6889
6890 /*
6891 * The interleaving of transformations and verdicts
6892 * makes it difficult to decide to continue or stop
6893 * the evaluation.
6894 */
6895
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006896 if (txn->flags & TX_SVDENY)
6897 break;
6898
Willy Tarreau3d300592007-03-18 18:34:41 +01006899 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006900 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6901 exp->action == ACT_PASS)) {
6902 exp = exp->next;
6903 continue;
6904 }
6905
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006906 /* if this filter had a condition, evaluate it now and skip to
6907 * next filter if the condition does not match.
6908 */
6909 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006910 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006911 ret = acl_pass(ret);
6912 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6913 ret = !ret;
6914 if (!ret)
6915 continue;
6916 }
6917
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006919 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006920 if (unlikely(ret < 0))
6921 return -1;
6922
6923 if (likely(ret == 0)) {
6924 /* The filter did not match the response, it can be
6925 * iterated through all headers.
6926 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006927 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006928 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006929 }
6930 return 0;
6931}
6932
6933
Willy Tarreaua15645d2007-03-18 16:22:39 +01006934/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006935 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006936 * desirable to call it only when needed. This function is also used when we
6937 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006938 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006939void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006940{
6941 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006942 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006943 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006944 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006945 char *hdr_beg, *hdr_end, *hdr_next;
6946 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006947
Willy Tarreaua15645d2007-03-18 16:22:39 +01006948 /* Iterate through the headers.
6949 * we start with the start line.
6950 */
6951 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006952 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006953
6954 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6955 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006956 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006957
6958 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006959 hdr_beg = hdr_next;
6960 hdr_end = hdr_beg + cur_hdr->len;
6961 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006962
Willy Tarreau24581ba2010-08-31 22:39:35 +02006963 /* We have one full header between hdr_beg and hdr_end, and the
6964 * next header starts at hdr_next. We're only interested in
6965 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006966 */
6967
Willy Tarreau24581ba2010-08-31 22:39:35 +02006968 is_cookie2 = 0;
6969 prev = hdr_beg + 10;
6970 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006971 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006972 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6973 if (!val) {
6974 old_idx = cur_idx;
6975 continue;
6976 }
6977 is_cookie2 = 1;
6978 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979 }
6980
Willy Tarreau24581ba2010-08-31 22:39:35 +02006981 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6982 * <prev> points to the colon.
6983 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006984 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985
Willy Tarreau24581ba2010-08-31 22:39:35 +02006986 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6987 * check-cache is enabled) and we are not interested in checking
6988 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006989 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006990 if (t->be->cookie_name == NULL &&
6991 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006992 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006993 return;
6994
Willy Tarreau24581ba2010-08-31 22:39:35 +02006995 /* OK so now we know we have to process this response cookie.
6996 * The format of the Set-Cookie header is slightly different
6997 * from the format of the Cookie header in that it does not
6998 * support the comma as a cookie delimiter (thus the header
6999 * cannot be folded) because the Expires attribute described in
7000 * the original Netscape's spec may contain an unquoted date
7001 * with a comma inside. We have to live with this because
7002 * many browsers don't support Max-Age and some browsers don't
7003 * support quoted strings. However the Set-Cookie2 header is
7004 * clean.
7005 *
7006 * We have to keep multiple pointers in order to support cookie
7007 * removal at the beginning, middle or end of header without
7008 * corrupting the header (in case of set-cookie2). A special
7009 * pointer, <scav> points to the beginning of the set-cookie-av
7010 * fields after the first semi-colon. The <next> pointer points
7011 * either to the end of line (set-cookie) or next unquoted comma
7012 * (set-cookie2). All of these headers are valid :
7013 *
7014 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7015 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7016 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7017 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7018 * | | | | | | | | | |
7019 * | | | | | | | | +-> next hdr_end <--+
7020 * | | | | | | | +------------> scav
7021 * | | | | | | +--------------> val_end
7022 * | | | | | +--------------------> val_beg
7023 * | | | | +----------------------> equal
7024 * | | | +------------------------> att_end
7025 * | | +----------------------------> att_beg
7026 * | +------------------------------> prev
7027 * +-----------------------------------------> hdr_beg
7028 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029
Willy Tarreau24581ba2010-08-31 22:39:35 +02007030 for (; prev < hdr_end; prev = next) {
7031 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007032
Willy Tarreau24581ba2010-08-31 22:39:35 +02007033 /* find att_beg */
7034 att_beg = prev + 1;
7035 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7036 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007037
Willy Tarreau24581ba2010-08-31 22:39:35 +02007038 /* find att_end : this is the first character after the last non
7039 * space before the equal. It may be equal to hdr_end.
7040 */
7041 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042
Willy Tarreau24581ba2010-08-31 22:39:35 +02007043 while (equal < hdr_end) {
7044 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7045 break;
7046 if (http_is_spht[(unsigned char)*equal++])
7047 continue;
7048 att_end = equal;
7049 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007050
Willy Tarreau24581ba2010-08-31 22:39:35 +02007051 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7052 * is between <att_beg> and <equal>, both may be identical.
7053 */
7054
7055 /* look for end of cookie if there is an equal sign */
7056 if (equal < hdr_end && *equal == '=') {
7057 /* look for the beginning of the value */
7058 val_beg = equal + 1;
7059 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7060 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007061
Willy Tarreau24581ba2010-08-31 22:39:35 +02007062 /* find the end of the value, respecting quotes */
7063 next = find_cookie_value_end(val_beg, hdr_end);
7064
7065 /* make val_end point to the first white space or delimitor after the value */
7066 val_end = next;
7067 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7068 val_end--;
7069 } else {
7070 /* <equal> points to next comma, semi-colon or EOL */
7071 val_beg = val_end = next = equal;
7072 }
7073
7074 if (next < hdr_end) {
7075 /* Set-Cookie2 supports multiple cookies, and <next> points to
7076 * a colon or semi-colon before the end. So skip all attr-value
7077 * pairs and look for the next comma. For Set-Cookie, since
7078 * commas are permitted in values, skip to the end.
7079 */
7080 if (is_cookie2)
7081 next = find_hdr_value_end(next, hdr_end);
7082 else
7083 next = hdr_end;
7084 }
7085
7086 /* Now everything is as on the diagram above */
7087
7088 /* Ignore cookies with no equal sign */
7089 if (equal == val_end)
7090 continue;
7091
7092 /* If there are spaces around the equal sign, we need to
7093 * strip them otherwise we'll get trouble for cookie captures,
7094 * or even for rewrites. Since this happens extremely rarely,
7095 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007096 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007097 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7098 int stripped_before = 0;
7099 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100
Willy Tarreau24581ba2010-08-31 22:39:35 +02007101 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007102 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007103 equal += stripped_before;
7104 val_beg += stripped_before;
7105 }
7106
7107 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007108 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007109 val_beg += stripped_after;
7110 stripped_before += stripped_after;
7111 }
7112
7113 val_end += stripped_before;
7114 next += stripped_before;
7115 hdr_end += stripped_before;
7116 hdr_next += stripped_before;
7117 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007118 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007119 }
7120
7121 /* First, let's see if we want to capture this cookie. We check
7122 * that we don't already have a server side cookie, because we
7123 * can only capture one. Also as an optimisation, we ignore
7124 * cookies shorter than the declared name.
7125 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007126 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007127 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007128 (val_end - att_beg >= t->fe->capture_namelen) &&
7129 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7130 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007131 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 Alert("HTTP logging : out of memory.\n");
7133 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007134 else {
7135 if (log_len > t->fe->capture_len)
7136 log_len = t->fe->capture_len;
7137 memcpy(txn->srv_cookie, att_beg, log_len);
7138 txn->srv_cookie[log_len] = 0;
7139 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140 }
7141
Willy Tarreau827aee92011-03-10 16:55:02 +01007142 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007144 if (!(t->flags & SN_IGNORE_PRST) &&
7145 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7146 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007147 /* assume passive cookie by default */
7148 txn->flags &= ~TX_SCK_MASK;
7149 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150
7151 /* If the cookie is in insert mode on a known server, we'll delete
7152 * this occurrence because we'll insert another one later.
7153 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007154 * a direct access.
7155 */
Willy Tarreau67402132012-05-31 20:40:20 +02007156 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007157 /* The "preserve" flag was set, we don't want to touch the
7158 * server's cookie.
7159 */
7160 }
Willy Tarreau67402132012-05-31 20:40:20 +02007161 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7162 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007163 /* this cookie must be deleted */
7164 if (*prev == ':' && next == hdr_end) {
7165 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007166 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007167 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7168 txn->hdr_idx.used--;
7169 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007170 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007171 hdr_next += delta;
7172 http_msg_move_end(&txn->rsp, delta);
7173 /* note: while both invalid now, <next> and <hdr_end>
7174 * are still equal, so the for() will stop as expected.
7175 */
7176 } else {
7177 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007178 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007179 next = prev;
7180 hdr_end += delta;
7181 hdr_next += delta;
7182 cur_hdr->len += delta;
7183 http_msg_move_end(&txn->rsp, delta);
7184 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007185 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007186 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007187 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007188 }
Willy Tarreau67402132012-05-31 20:40:20 +02007189 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007190 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007191 * with this server since we know it.
7192 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007193 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007194 next += delta;
7195 hdr_end += delta;
7196 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007197 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007198 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007199
Willy Tarreauf1348312010-10-07 15:54:11 +02007200 txn->flags &= ~TX_SCK_MASK;
7201 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007202 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007203 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007204 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007205 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007206 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007207 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007208 next += delta;
7209 hdr_end += delta;
7210 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007211 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007212 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007213
Willy Tarreau827aee92011-03-10 16:55:02 +01007214 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007215 txn->flags &= ~TX_SCK_MASK;
7216 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007217 }
7218 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007219 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7220 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007221 int cmp_len, value_len;
7222 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007223
Cyril Bontéb21570a2009-11-29 20:04:48 +01007224 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007225 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7226 value_begin = att_beg + t->be->appsession_name_len;
7227 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007228 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007229 cmp_len = att_end - att_beg;
7230 value_begin = val_beg;
7231 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007232 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007233
Cyril Bonté17530c32010-04-06 21:11:10 +02007234 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007235 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7236 /* free a possibly previously allocated memory */
7237 pool_free2(apools.sessid, txn->sessid);
7238
Cyril Bontéb21570a2009-11-29 20:04:48 +01007239 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007240 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007241 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7242 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7243 return;
7244 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007245 memcpy(txn->sessid, value_begin, value_len);
7246 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007247 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007248 }
7249 /* that's done for this cookie, check the next one on the same
7250 * line when next != hdr_end (only if is_cookie2).
7251 */
7252 }
7253 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007254 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007255 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007256
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007257 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007258 appsess *asession = NULL;
7259 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007260 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007261 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007262 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007263 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7264 Alert("Not enough Memory process_srv():asession:calloc().\n");
7265 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7266 return;
7267 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007268 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7269
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007270 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7271 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7272 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 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007276 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007277 asession->sessid[t->be->appsession_len] = 0;
7278
Willy Tarreau827aee92011-03-10 16:55:02 +01007279 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007280 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007281 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007282 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007283 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007284 return;
7285 }
7286 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007287 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007288
7289 asession->request_count = 0;
7290 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7291 }
7292
7293 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7294 asession->request_count++;
7295 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296}
7297
7298
Willy Tarreaua15645d2007-03-18 16:22:39 +01007299/*
7300 * Check if response is cacheable or not. Updates t->flags.
7301 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007302void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007303{
7304 struct http_txn *txn = &t->txn;
7305 char *p1, *p2;
7306
7307 char *cur_ptr, *cur_end, *cur_next;
7308 int cur_idx;
7309
Willy Tarreau5df51872007-11-25 16:20:08 +01007310 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007311 return;
7312
7313 /* Iterate through the headers.
7314 * we start with the start line.
7315 */
7316 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007317 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007318
7319 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7320 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007321 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007322
7323 cur_hdr = &txn->hdr_idx.v[cur_idx];
7324 cur_ptr = cur_next;
7325 cur_end = cur_ptr + cur_hdr->len;
7326 cur_next = cur_end + cur_hdr->cr + 1;
7327
7328 /* We have one full header between cur_ptr and cur_end, and the
7329 * next header starts at cur_next. We're only interested in
7330 * "Cookie:" headers.
7331 */
7332
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007333 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7334 if (val) {
7335 if ((cur_end - (cur_ptr + val) >= 8) &&
7336 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7337 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7338 return;
7339 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007340 }
7341
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007342 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7343 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344 continue;
7345
7346 /* OK, right now we know we have a cache-control header at cur_ptr */
7347
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007348 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007349
7350 if (p1 >= cur_end) /* no more info */
7351 continue;
7352
7353 /* p1 is at the beginning of the value */
7354 p2 = p1;
7355
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007356 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007357 p2++;
7358
7359 /* we have a complete value between p1 and p2 */
7360 if (p2 < cur_end && *p2 == '=') {
7361 /* we have something of the form no-cache="set-cookie" */
7362 if ((cur_end - p1 >= 21) &&
7363 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7364 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007365 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007366 continue;
7367 }
7368
7369 /* OK, so we know that either p2 points to the end of string or to a comma */
7370 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7371 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7372 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7373 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007374 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007375 return;
7376 }
7377
7378 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007379 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007380 continue;
7381 }
7382 }
7383}
7384
7385
Willy Tarreau58f10d72006-12-04 02:26:12 +01007386/*
7387 * Try to retrieve a known appsession in the URI, then the associated server.
7388 * If the server is found, it's assigned to the session.
7389 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007390void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007391{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007392 char *end_params, *first_param, *cur_param, *next_param;
7393 char separator;
7394 int value_len;
7395
7396 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007397
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007398 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007399 (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 +01007400 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007401 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007402
Cyril Bontéb21570a2009-11-29 20:04:48 +01007403 first_param = NULL;
7404 switch (mode) {
7405 case PR_O2_AS_M_PP:
7406 first_param = memchr(begin, ';', len);
7407 break;
7408 case PR_O2_AS_M_QS:
7409 first_param = memchr(begin, '?', len);
7410 break;
7411 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007412
Cyril Bontéb21570a2009-11-29 20:04:48 +01007413 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007414 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007415 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007416
Cyril Bontéb21570a2009-11-29 20:04:48 +01007417 switch (mode) {
7418 case PR_O2_AS_M_PP:
7419 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7420 end_params = (char *) begin + len;
7421 }
7422 separator = ';';
7423 break;
7424 case PR_O2_AS_M_QS:
7425 end_params = (char *) begin + len;
7426 separator = '&';
7427 break;
7428 default:
7429 /* unknown mode, shouldn't happen */
7430 return;
7431 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007432
Cyril Bontéb21570a2009-11-29 20:04:48 +01007433 cur_param = next_param = end_params;
7434 while (cur_param > first_param) {
7435 cur_param--;
7436 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7437 /* let's see if this is the appsession parameter */
7438 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7439 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7440 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7441 /* Cool... it's the right one */
7442 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7443 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7444 if (value_len > 0) {
7445 manage_client_side_appsession(t, cur_param, value_len);
7446 }
7447 break;
7448 }
7449 next_param = cur_param;
7450 }
7451 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007452#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007453 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007454 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007455#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007456}
7457
Willy Tarreaub2513902006-12-17 14:52:38 +01007458/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007459 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007460 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007461 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007462 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007463 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007464 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007465 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007466 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007467int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007468{
7469 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007470 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007471 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007472 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007473
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007474 if (!uri_auth)
7475 return 0;
7476
Cyril Bonté70be45d2010-10-12 00:14:35 +02007477 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007478 return 0;
7479
Willy Tarreau295a8372011-03-10 11:25:07 +01007480 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007481 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007482
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007483 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007484 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007485 return 0;
7486
Willy Tarreau3a215be2012-03-09 21:39:51 +01007487 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007488 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007489 return 0;
7490
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007491 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007492 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007493 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007494 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007495 break;
7496 }
7497 h++;
7498 }
7499
7500 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007501 h = uri + uri_auth->uri_len;
7502 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007503 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007504 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007505 break;
7506 }
7507 h++;
7508 }
7509 }
7510
Willy Tarreau3a215be2012-03-09 21:39:51 +01007511 h = uri + uri_auth->uri_len;
7512 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007513 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007514 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007515 break;
7516 }
7517 h++;
7518 }
7519
Willy Tarreau3a215be2012-03-09 21:39:51 +01007520 h = uri + uri_auth->uri_len;
7521 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007522 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007523 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007524 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007525 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007526 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7527 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7528 si->applet.ctx.stats.st_code = i;
7529 break;
7530 }
7531 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007532 break;
7533 }
7534 h++;
7535 }
7536
Willy Tarreau295a8372011-03-10 11:25:07 +01007537 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007538
Willy Tarreaub2513902006-12-17 14:52:38 +01007539 return 1;
7540}
7541
Willy Tarreau4076a152009-04-02 15:18:36 +02007542/*
7543 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007544 * By default it tries to report the error position as msg->err_pos. However if
7545 * this one is not set, it will then report msg->next, which is the last known
7546 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007547 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007548 */
7549void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007550 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007551 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007552{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007553 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007554 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007555
Willy Tarreau9b28e032012-10-12 23:49:43 +02007556 es->len = MIN(chn->buf->i, sizeof(es->buf));
7557 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007558 len1 = MIN(len1, es->len);
7559 len2 = es->len - len1; /* remaining data if buffer wraps */
7560
Willy Tarreau9b28e032012-10-12 23:49:43 +02007561 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007562 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007563 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007564
Willy Tarreau4076a152009-04-02 15:18:36 +02007565 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007566 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007567 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007568 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007569
Willy Tarreau4076a152009-04-02 15:18:36 +02007570 es->when = date; // user-visible date
7571 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007572 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007573 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007574 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007575 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007576 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007577 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007578 es->s_flags = s->flags;
7579 es->t_flags = s->txn.flags;
7580 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007581 es->b_out = chn->buf->o;
7582 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007583 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007584 es->m_clen = msg->chunk_len;
7585 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007586}
Willy Tarreaub2513902006-12-17 14:52:38 +01007587
Willy Tarreau294c4732011-12-16 21:35:50 +01007588/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7589 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7590 * performed over the whole headers. Otherwise it must contain a valid header
7591 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7592 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7593 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7594 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7595 * -1.
7596 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007597 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007598unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007599 struct hdr_idx *idx, int occ,
7600 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007601{
Willy Tarreau294c4732011-12-16 21:35:50 +01007602 struct hdr_ctx local_ctx;
7603 char *ptr_hist[MAX_HDR_HISTORY];
7604 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007605 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007606 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007607
Willy Tarreau294c4732011-12-16 21:35:50 +01007608 if (!ctx) {
7609 local_ctx.idx = 0;
7610 ctx = &local_ctx;
7611 }
7612
Willy Tarreaubce70882009-09-07 11:51:47 +02007613 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007614 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007615 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007616 occ--;
7617 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007618 *vptr = ctx->line + ctx->val;
7619 *vlen = ctx->vlen;
7620 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007621 }
7622 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007623 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007624 }
7625
7626 /* negative occurrence, we scan all the list then walk back */
7627 if (-occ > MAX_HDR_HISTORY)
7628 return 0;
7629
Willy Tarreau294c4732011-12-16 21:35:50 +01007630 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007631 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007632 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7633 len_hist[hist_ptr] = ctx->vlen;
7634 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007635 hist_ptr = 0;
7636 found++;
7637 }
7638 if (-occ > found)
7639 return 0;
7640 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7641 * find occurrence -occ, so we have to check [hist_ptr+occ].
7642 */
7643 hist_ptr += occ;
7644 if (hist_ptr >= MAX_HDR_HISTORY)
7645 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007646 *vptr = ptr_hist[hist_ptr];
7647 *vlen = len_hist[hist_ptr];
7648 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007649}
7650
Willy Tarreaubaaee002006-06-26 02:48:02 +02007651/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007652 * Print a debug line with a header. Always stop at the first CR or LF char,
7653 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7654 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007655 */
7656void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7657{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007658 int max;
7659 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01007660 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
7661 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007662
7663 for (max = 0; start + max < end; max++)
7664 if (start[max] == '\r' || start[max] == '\n')
7665 break;
7666
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007667 UBOUND(max, trash.size - trash.len - 3);
7668 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7669 trash.str[trash.len++] = '\n';
7670 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007671}
7672
Willy Tarreau0937bc42009-12-22 15:03:09 +01007673/*
7674 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7675 * the required fields are properly allocated and that we only need to (re)init
7676 * them. This should be used before processing any new request.
7677 */
7678void http_init_txn(struct session *s)
7679{
7680 struct http_txn *txn = &s->txn;
7681 struct proxy *fe = s->fe;
7682
7683 txn->flags = 0;
7684 txn->status = -1;
7685
William Lallemand5f232402012-04-05 18:02:55 +02007686 global.req_count++;
7687
Willy Tarreauf64d1412010-10-07 20:06:11 +02007688 txn->cookie_first_date = 0;
7689 txn->cookie_last_date = 0;
7690
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007691 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007692 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007693 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007694 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007695 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007696 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007697 txn->req.chunk_len = 0LL;
7698 txn->req.body_len = 0LL;
7699 txn->rsp.chunk_len = 0LL;
7700 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007701 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7702 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007703 txn->req.chn = s->req;
7704 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007705
7706 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007707
7708 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7709 if (fe->options2 & PR_O2_REQBUG_OK)
7710 txn->req.err_pos = -1; /* let buggy requests pass */
7711
Willy Tarreau46023632010-01-07 22:51:47 +01007712 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007713 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7714
Willy Tarreau46023632010-01-07 22:51:47 +01007715 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007716 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7717
7718 if (txn->hdr_idx.v)
7719 hdr_idx_init(&txn->hdr_idx);
7720}
7721
7722/* to be used at the end of a transaction */
7723void http_end_txn(struct session *s)
7724{
7725 struct http_txn *txn = &s->txn;
7726
7727 /* these ones will have been dynamically allocated */
7728 pool_free2(pool2_requri, txn->uri);
7729 pool_free2(pool2_capture, txn->cli_cookie);
7730 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007731 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007732 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007733
William Lallemanda73203e2012-03-12 12:48:57 +01007734 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007735 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007736 txn->uri = NULL;
7737 txn->srv_cookie = NULL;
7738 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007739
7740 if (txn->req.cap) {
7741 struct cap_hdr *h;
7742 for (h = s->fe->req_cap; h; h = h->next)
7743 pool_free2(h->pool, txn->req.cap[h->index]);
7744 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7745 }
7746
7747 if (txn->rsp.cap) {
7748 struct cap_hdr *h;
7749 for (h = s->fe->rsp_cap; h; h = h->next)
7750 pool_free2(h->pool, txn->rsp.cap[h->index]);
7751 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7752 }
7753
Willy Tarreau0937bc42009-12-22 15:03:09 +01007754}
7755
7756/* to be used at the end of a transaction to prepare a new one */
7757void http_reset_txn(struct session *s)
7758{
7759 http_end_txn(s);
7760 http_init_txn(s);
7761
7762 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007763 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007764 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007765 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007766 /* re-init store persistence */
7767 s->store_count = 0;
7768
Willy Tarreau0937bc42009-12-22 15:03:09 +01007769 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007770
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007771 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007772
Willy Tarreau739cfba2010-01-25 23:11:14 +01007773 /* We must trim any excess data from the response buffer, because we
7774 * may have blocked an invalid response from a server that we don't
7775 * want to accidentely forward once we disable the analysers, nor do
7776 * we want those data to come along with next response. A typical
7777 * example of such data would be from a buggy server responding to
7778 * a HEAD with some data, or sending more than the advertised
7779 * content-length.
7780 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007781 if (unlikely(s->rep->buf->i))
7782 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007783
Willy Tarreau0937bc42009-12-22 15:03:09 +01007784 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007785 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007786
Willy Tarreaud04e8582010-05-31 12:31:35 +02007787 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007788 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007789
7790 s->req->rex = TICK_ETERNITY;
7791 s->req->wex = TICK_ETERNITY;
7792 s->req->analyse_exp = TICK_ETERNITY;
7793 s->rep->rex = TICK_ETERNITY;
7794 s->rep->wex = TICK_ETERNITY;
7795 s->rep->analyse_exp = TICK_ETERNITY;
7796}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007797
Willy Tarreauff011f22011-01-06 17:51:27 +01007798void free_http_req_rules(struct list *r) {
7799 struct http_req_rule *tr, *pr;
7800
7801 list_for_each_entry_safe(pr, tr, r, list) {
7802 LIST_DEL(&pr->list);
7803 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7804 free(pr->http_auth.realm);
7805
7806 free(pr);
7807 }
7808}
7809
7810struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7811{
7812 struct http_req_rule *rule;
7813 int cur_arg;
7814
7815 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7816 if (!rule) {
7817 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7818 return NULL;
7819 }
7820
7821 if (!*args[0]) {
7822 goto req_error_parsing;
7823 } else if (!strcmp(args[0], "allow")) {
7824 rule->action = HTTP_REQ_ACT_ALLOW;
7825 cur_arg = 1;
7826 } else if (!strcmp(args[0], "deny")) {
7827 rule->action = HTTP_REQ_ACT_DENY;
7828 cur_arg = 1;
7829 } else if (!strcmp(args[0], "auth")) {
7830 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7831 cur_arg = 1;
7832
7833 while(*args[cur_arg]) {
7834 if (!strcmp(args[cur_arg], "realm")) {
7835 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7836 cur_arg+=2;
7837 continue;
7838 } else
7839 break;
7840 }
7841 } else {
7842req_error_parsing:
7843 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7844 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7845 return NULL;
7846 }
7847
7848 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7849 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007850 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007851
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007852 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7853 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7854 file, linenum, args[0], errmsg);
7855 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007856 return NULL;
7857 }
7858 rule->cond = cond;
7859 }
7860 else if (*args[cur_arg]) {
7861 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7862 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7863 file, linenum, args[0], args[cur_arg]);
7864 return NULL;
7865 }
7866
7867 return rule;
7868}
7869
Willy Tarreau8797c062007-05-07 00:55:35 +02007870/************************************************************************/
7871/* The code below is dedicated to ACL parsing and matching */
7872/************************************************************************/
7873
7874
Willy Tarreau14174bc2012-04-16 14:34:04 +02007875/* This function ensures that the prerequisites for an L7 fetch are ready,
7876 * which means that a request or response is ready. If some data is missing,
7877 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007878 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7879 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007880 *
7881 * The function returns :
7882 * 0 if some data is missing or if the requested data cannot be fetched
7883 * -1 if it is certain that we'll never have any HTTP message there
7884 * 1 if an HTTP message is ready
7885 */
7886static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007887acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007888 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007889{
7890 struct http_txn *txn = l7;
7891 struct http_msg *msg = &txn->req;
7892
7893 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7894 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7895 */
7896
7897 if (unlikely(!s || !txn))
7898 return 0;
7899
7900 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007901 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007902
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007903 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007904 if (unlikely(!s->req))
7905 return 0;
7906
7907 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007908 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007909 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007910 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007911 return -1;
7912 }
7913
7914 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007915 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02007916 http_msg_analyzer(msg, &txn->hdr_idx);
7917
7918 /* Still no valid request ? */
7919 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007920 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007921 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007922 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007923 return -1;
7924 }
7925 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007926 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007927 return 0;
7928 }
7929
7930 /* OK we just got a valid HTTP request. We have some minor
7931 * preparation to perform so that further checks can rely
7932 * on HTTP tests.
7933 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007934 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007935 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7936 s->flags |= SN_REDIRECTABLE;
7937
7938 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007939 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007940 return -1;
7941 }
7942 }
7943
Willy Tarreau24e32d82012-04-23 23:55:44 +02007944 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007945 return 0; /* data might have moved and indexes changed */
7946
7947 /* otherwise everything's ready for the request */
7948 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007949 else {
7950 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007951 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7952 return 0;
7953 }
7954
7955 /* everything's OK */
7956 return 1;
7957}
Willy Tarreau8797c062007-05-07 00:55:35 +02007958
Willy Tarreauc0239e02012-04-16 14:42:55 +02007959#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007960 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 +02007961
Willy Tarreau24e32d82012-04-23 23:55:44 +02007962#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007963 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 +02007964
Willy Tarreau8797c062007-05-07 00:55:35 +02007965
7966/* 1. Check on METHOD
7967 * We use the pre-parsed method if it is known, and store its number as an
7968 * integer. If it is unknown, we use the pointer and the length.
7969 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007970static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007971{
7972 int len, meth;
7973
Willy Tarreauae8b7962007-06-09 23:10:04 +02007974 len = strlen(*text);
7975 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007976
7977 pattern->val.i = meth;
7978 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007979 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007980 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007981 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007982 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007983 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007984 pattern->len = len;
7985 }
7986 return 1;
7987}
7988
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007989/* This function fetches the method of current HTTP request and stores
7990 * it in the global pattern struct as a chunk. There are two possibilities :
7991 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7992 * in <len> and <ptr> is NULL ;
7993 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7994 * <len> to its length.
7995 * This is intended to be used with acl_match_meth() only.
7996 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007997static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007998acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007999 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008000{
8001 int meth;
8002 struct http_txn *txn = l7;
8003
Willy Tarreau24e32d82012-04-23 23:55:44 +02008004 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008005
Willy Tarreau8797c062007-05-07 00:55:35 +02008006 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008007 smp->type = SMP_T_UINT;
8008 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008009 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008010 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8011 /* ensure the indexes are not affected */
8012 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008013 smp->type = SMP_T_CSTR;
8014 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008015 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008016 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008017 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008018 return 1;
8019}
8020
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008021/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008022static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008023{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008024 int icase;
8025
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008026
Willy Tarreauf853c462012-04-23 18:53:56 +02008027 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008028 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008029 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008030 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008031 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008032 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008033
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008034 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8035 if (pattern->val.i != HTTP_METH_OTHER)
8036 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008037
8038 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008039 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008040 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008041
8042 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008043 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8044 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008045 return ACL_PAT_FAIL;
8046 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008047}
8048
8049/* 2. Check on Request/Status Version
8050 * We simply compare strings here.
8051 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008052static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008053{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008054 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008055 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008056 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008057 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008058 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008059 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008060 return 1;
8061}
8062
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008063static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008064acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008065 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008066{
8067 struct http_txn *txn = l7;
8068 char *ptr;
8069 int len;
8070
Willy Tarreauc0239e02012-04-16 14:42:55 +02008071 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008072
Willy Tarreau8797c062007-05-07 00:55:35 +02008073 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008074 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008075
8076 while ((len-- > 0) && (*ptr++ != '/'));
8077 if (len <= 0)
8078 return 0;
8079
Willy Tarreauf853c462012-04-23 18:53:56 +02008080 smp->type = SMP_T_CSTR;
8081 smp->data.str.str = ptr;
8082 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008083
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008084 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008085 return 1;
8086}
8087
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008088static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008089acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008090 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008091{
8092 struct http_txn *txn = l7;
8093 char *ptr;
8094 int len;
8095
Willy Tarreauc0239e02012-04-16 14:42:55 +02008096 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008097
Willy Tarreau8797c062007-05-07 00:55:35 +02008098 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008099 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008100
8101 while ((len-- > 0) && (*ptr++ != '/'));
8102 if (len <= 0)
8103 return 0;
8104
Willy Tarreauf853c462012-04-23 18:53:56 +02008105 smp->type = SMP_T_CSTR;
8106 smp->data.str.str = ptr;
8107 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008108
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008109 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008110 return 1;
8111}
8112
8113/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008114static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008115acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008116 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008117{
8118 struct http_txn *txn = l7;
8119 char *ptr;
8120 int len;
8121
Willy Tarreauc0239e02012-04-16 14:42:55 +02008122 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008123
Willy Tarreau8797c062007-05-07 00:55:35 +02008124 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008125 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008126
Willy Tarreauf853c462012-04-23 18:53:56 +02008127 smp->type = SMP_T_UINT;
8128 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008129 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008130 return 1;
8131}
8132
8133/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008134static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008135smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008136 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008137{
8138 struct http_txn *txn = l7;
8139
Willy Tarreauc0239e02012-04-16 14:42:55 +02008140 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008141
Willy Tarreauf853c462012-04-23 18:53:56 +02008142 smp->type = SMP_T_CSTR;
8143 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008144 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008145 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008146 return 1;
8147}
8148
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008149static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008150smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008151 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008152{
8153 struct http_txn *txn = l7;
8154
Willy Tarreauc0239e02012-04-16 14:42:55 +02008155 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008156
8157 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008158 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8159 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008160 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008161 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008162 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008163
8164 /*
8165 * If we are parsing url in frontend space, we prepare backend stage
8166 * to not parse again the same url ! optimization lazyness...
8167 */
8168 if (px->options & PR_O_HTTP_PROXY)
8169 l4->flags |= SN_ADDR_SET;
8170
Willy Tarreau37406352012-04-23 16:16:37 +02008171 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008172 return 1;
8173}
8174
8175static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008176smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008177 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008178{
8179 struct http_txn *txn = l7;
8180
Willy Tarreauc0239e02012-04-16 14:42:55 +02008181 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008182
8183 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008184 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 +02008185 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008186 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008187
8188 if (px->options & PR_O_HTTP_PROXY)
8189 l4->flags |= SN_ADDR_SET;
8190
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008191 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008192 return 1;
8193}
8194
Willy Tarreau185b5c42012-04-26 15:11:51 +02008195/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8196 * Accepts an optional argument of type string containing the header field name,
8197 * and an optional argument of type signed or unsigned integer to request an
8198 * explicit occurrence of the header. Note that in the event of a missing name,
8199 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008200 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008201static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008202smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008203 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008204{
8205 struct http_txn *txn = l7;
8206 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008207 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008208 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008209 int occ = 0;
8210 const char *name_str = NULL;
8211 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008212
Willy Tarreau185b5c42012-04-26 15:11:51 +02008213 if (args) {
8214 if (args[0].type != ARGT_STR)
8215 return 0;
8216 name_str = args[0].data.str.str;
8217 name_len = args[0].data.str.len;
8218
8219 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8220 occ = args[1].data.uint;
8221 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008222
Willy Tarreaue333ec92012-04-16 16:26:40 +02008223 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008224
Willy Tarreau185b5c42012-04-26 15:11:51 +02008225 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008226 /* search for header from the beginning */
8227 ctx->idx = 0;
8228
Willy Tarreau185b5c42012-04-26 15:11:51 +02008229 if (!occ && !(opt & SMP_OPT_ITERATE))
8230 /* no explicit occurrence and single fetch => last header by default */
8231 occ = -1;
8232
8233 if (!occ)
8234 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008235 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008236
Willy Tarreau185b5c42012-04-26 15:11:51 +02008237 smp->type = SMP_T_CSTR;
8238 smp->flags |= SMP_F_VOL_HDR;
8239 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 +02008240 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008241
Willy Tarreau37406352012-04-23 16:16:37 +02008242 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008243 return 0;
8244}
8245
Willy Tarreauc11416f2007-06-17 16:58:38 +02008246/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008247 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008248 */
8249static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008250smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008251 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008252{
8253 struct http_txn *txn = l7;
8254 struct hdr_idx *idx = &txn->hdr_idx;
8255 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008256 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008257 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008258
Willy Tarreau24e32d82012-04-23 23:55:44 +02008259 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008260 return 0;
8261
Willy Tarreaue333ec92012-04-16 16:26:40 +02008262 CHECK_HTTP_MESSAGE_FIRST();
8263
Willy Tarreau33a7e692007-06-10 19:45:56 +02008264 ctx.idx = 0;
8265 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008266 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 +02008267 cnt++;
8268
Willy Tarreauf853c462012-04-23 18:53:56 +02008269 smp->type = SMP_T_UINT;
8270 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008271 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008272 return 1;
8273}
8274
Willy Tarreau185b5c42012-04-26 15:11:51 +02008275/* Fetch an HTTP header's integer value. The integer value is returned. It
8276 * takes a mandatory argument of type string and an optional one of type int
8277 * to designate a specific occurrence. It returns an unsigned integer, which
8278 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008279 */
8280static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008281smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008282 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008283{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008284 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008285
Willy Tarreauf853c462012-04-23 18:53:56 +02008286 if (ret > 0) {
8287 smp->type = SMP_T_UINT;
8288 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8289 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008290
Willy Tarreaud53e2422012-04-16 17:21:11 +02008291 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008292}
8293
Cyril Bonté69fa9922012-10-25 00:01:06 +02008294/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8295 * and an optional one of type int to designate a specific occurrence.
8296 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008297 */
8298static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008299smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008300 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008301{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008302 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008303
Willy Tarreau185b5c42012-04-26 15:11:51 +02008304 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008305 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8306 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008307 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008308 } else {
8309 struct chunk *temp = sample_get_trash_chunk();
8310 if (smp->data.str.len < temp->size - 1) {
8311 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8312 temp->str[smp->data.str.len] = '\0';
8313 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8314 smp->type = SMP_T_IPV6;
8315 break;
8316 }
8317 }
8318 }
8319
Willy Tarreaud53e2422012-04-16 17:21:11 +02008320 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008321 if (!(smp->flags & SMP_F_NOT_LAST))
8322 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008323 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008324 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008325}
8326
Willy Tarreau737b0c12007-06-10 21:28:46 +02008327/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8328 * the first '/' after the possible hostname, and ends before the possible '?'.
8329 */
8330static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008331smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008332 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008333{
8334 struct http_txn *txn = l7;
8335 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008336
Willy Tarreauc0239e02012-04-16 14:42:55 +02008337 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008338
Willy Tarreau9b28e032012-10-12 23:49:43 +02008339 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008340 ptr = http_get_path(txn);
8341 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008342 return 0;
8343
8344 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008345 smp->type = SMP_T_CSTR;
8346 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008347
8348 while (ptr < end && *ptr != '?')
8349 ptr++;
8350
Willy Tarreauf853c462012-04-23 18:53:56 +02008351 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008352 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008353 return 1;
8354}
8355
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008356/* This produces a concatenation of the first occurrence of the Host header
8357 * followed by the path component if it begins with a slash ('/'). This means
8358 * that '*' will not be added, resulting in exactly the first Host entry.
8359 * If no Host header is found, then the path is returned as-is. The returned
8360 * value is stored in the trash so it does not need to be marked constant.
8361 */
8362static int
8363smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8364 const struct arg *args, struct sample *smp)
8365{
8366 struct http_txn *txn = l7;
8367 char *ptr, *end, *beg;
8368 struct hdr_ctx ctx;
8369
8370 CHECK_HTTP_MESSAGE_FIRST();
8371
8372 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008373 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 +02008374 !ctx.vlen)
8375 return smp_fetch_path(px, l4, l7, opt, args, smp);
8376
8377 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008378 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008379 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008380 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008381 smp->data.str.len = ctx.vlen;
8382
8383 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008384 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 +02008385 beg = http_get_path(txn);
8386 if (!beg)
8387 beg = end;
8388
8389 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8390
8391 if (beg < ptr && *beg == '/') {
8392 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8393 smp->data.str.len += ptr - beg;
8394 }
8395
8396 smp->flags = SMP_F_VOL_1ST;
8397 return 1;
8398}
8399
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008400static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008401acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008402 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008403{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008404 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8405 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8406 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008407
Willy Tarreau24e32d82012-04-23 23:55:44 +02008408 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008409
Willy Tarreauf853c462012-04-23 18:53:56 +02008410 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008411 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008412 return 1;
8413}
8414
Willy Tarreau7f18e522010-10-22 20:04:13 +02008415/* return a valid test if the current request is the first one on the connection */
8416static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008417acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008418 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008419{
8420 if (!s)
8421 return 0;
8422
Willy Tarreauf853c462012-04-23 18:53:56 +02008423 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008424 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008425 return 1;
8426}
8427
Willy Tarreau34db1082012-04-19 17:16:54 +02008428/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008429static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008430acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008431 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008432{
8433
Willy Tarreau24e32d82012-04-23 23:55:44 +02008434 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008435 return 0;
8436
Willy Tarreauc0239e02012-04-16 14:42:55 +02008437 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008438
Willy Tarreauc0239e02012-04-16 14:42:55 +02008439 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008440 return 0;
8441
Willy Tarreauf853c462012-04-23 18:53:56 +02008442 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008443 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 +01008444 return 1;
8445}
Willy Tarreau8797c062007-05-07 00:55:35 +02008446
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008447/* Accepts exactly 1 argument of type userlist */
8448static int
8449acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8450 const struct arg *args, struct sample *smp)
8451{
8452
8453 if (!args || args->type != ARGT_USR)
8454 return 0;
8455
8456 CHECK_HTTP_MESSAGE_FIRST();
8457
8458 if (!get_http_auth(l4))
8459 return 0;
8460
8461 /* acl_match_auth() will need several information at once */
8462 smp->ctx.a[0] = args->data.usr; /* user list */
8463 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8464 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8465
8466 /* if the user does not belong to the userlist or has a wrong password,
8467 * report that it unconditionally does not match. Otherwise we return
8468 * a non-zero integer which will be ignored anyway since all the params
8469 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8470 */
8471 smp->type = SMP_T_BOOL;
8472 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8473 if (smp->data.uint)
8474 smp->type = SMP_T_UINT;
8475
8476 return 1;
8477}
8478
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008479/* Try to find the next occurrence of a cookie name in a cookie header value.
8480 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8481 * the cookie value is returned into *value and *value_l, and the function
8482 * returns a pointer to the next pointer to search from if the value was found.
8483 * Otherwise if the cookie was not found, NULL is returned and neither value
8484 * nor value_l are touched. The input <hdr> string should first point to the
8485 * header's value, and the <hdr_end> pointer must point to the first character
8486 * not part of the value. <list> must be non-zero if value may represent a list
8487 * of values (cookie headers). This makes it faster to abort parsing when no
8488 * list is expected.
8489 */
8490static char *
8491extract_cookie_value(char *hdr, const char *hdr_end,
8492 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008493 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008494{
8495 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8496 char *next;
8497
8498 /* we search at least a cookie name followed by an equal, and more
8499 * generally something like this :
8500 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8501 */
8502 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8503 /* Iterate through all cookies on this line */
8504
8505 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8506 att_beg++;
8507
8508 /* find att_end : this is the first character after the last non
8509 * space before the equal. It may be equal to hdr_end.
8510 */
8511 equal = att_end = att_beg;
8512
8513 while (equal < hdr_end) {
8514 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8515 break;
8516 if (http_is_spht[(unsigned char)*equal++])
8517 continue;
8518 att_end = equal;
8519 }
8520
8521 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8522 * is between <att_beg> and <equal>, both may be identical.
8523 */
8524
8525 /* look for end of cookie if there is an equal sign */
8526 if (equal < hdr_end && *equal == '=') {
8527 /* look for the beginning of the value */
8528 val_beg = equal + 1;
8529 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8530 val_beg++;
8531
8532 /* find the end of the value, respecting quotes */
8533 next = find_cookie_value_end(val_beg, hdr_end);
8534
8535 /* make val_end point to the first white space or delimitor after the value */
8536 val_end = next;
8537 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8538 val_end--;
8539 } else {
8540 val_beg = val_end = next = equal;
8541 }
8542
8543 /* We have nothing to do with attributes beginning with '$'. However,
8544 * they will automatically be removed if a header before them is removed,
8545 * since they're supposed to be linked together.
8546 */
8547 if (*att_beg == '$')
8548 continue;
8549
8550 /* Ignore cookies with no equal sign */
8551 if (equal == next)
8552 continue;
8553
8554 /* Now we have the cookie name between att_beg and att_end, and
8555 * its value between val_beg and val_end.
8556 */
8557
8558 if (att_end - att_beg == cookie_name_l &&
8559 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8560 /* let's return this value and indicate where to go on from */
8561 *value = val_beg;
8562 *value_l = val_end - val_beg;
8563 return next + 1;
8564 }
8565
8566 /* Set-Cookie headers only have the name in the first attr=value part */
8567 if (!list)
8568 break;
8569 }
8570
8571 return NULL;
8572}
8573
Willy Tarreaue333ec92012-04-16 16:26:40 +02008574/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008575 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8576 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008577 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008578 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008579 * Accepts exactly 1 argument of type string. If the input options indicate
8580 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008581 */
8582static int
Willy Tarreau51539362012-05-08 12:46:28 +02008583smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8584 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008585{
8586 struct http_txn *txn = l7;
8587 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008588 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008589 const struct http_msg *msg;
8590 const char *hdr_name;
8591 int hdr_name_len;
8592 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008593 int occ = 0;
8594 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008595
Willy Tarreau24e32d82012-04-23 23:55:44 +02008596 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008597 return 0;
8598
Willy Tarreaue333ec92012-04-16 16:26:40 +02008599 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008600
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008601 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008602 msg = &txn->req;
8603 hdr_name = "Cookie";
8604 hdr_name_len = 6;
8605 } else {
8606 msg = &txn->rsp;
8607 hdr_name = "Set-Cookie";
8608 hdr_name_len = 10;
8609 }
8610
Willy Tarreau28376d62012-04-26 21:26:10 +02008611 if (!occ && !(opt & SMP_OPT_ITERATE))
8612 /* no explicit occurrence and single fetch => last cookie by default */
8613 occ = -1;
8614
8615 /* OK so basically here, either we want only one value and it's the
8616 * last one, or we want to iterate over all of them and we fetch the
8617 * next one.
8618 */
8619
Willy Tarreau9b28e032012-10-12 23:49:43 +02008620 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008621 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008622 /* search for the header from the beginning, we must first initialize
8623 * the search parameters.
8624 */
Willy Tarreau37406352012-04-23 16:16:37 +02008625 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008626 ctx->idx = 0;
8627 }
8628
Willy Tarreau28376d62012-04-26 21:26:10 +02008629 smp->flags |= SMP_F_VOL_HDR;
8630
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008631 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008632 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8633 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008634 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8635 goto out;
8636
Willy Tarreau24e32d82012-04-23 23:55:44 +02008637 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008638 continue;
8639
Willy Tarreau37406352012-04-23 16:16:37 +02008640 smp->ctx.a[0] = ctx->line + ctx->val;
8641 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008642 }
8643
Willy Tarreauf853c462012-04-23 18:53:56 +02008644 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008645 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008646 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008647 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008648 &smp->data.str.str,
8649 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008650 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008651 found = 1;
8652 if (occ >= 0) {
8653 /* one value was returned into smp->data.str.{str,len} */
8654 smp->flags |= SMP_F_NOT_LAST;
8655 return 1;
8656 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008657 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008658 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008659 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008660 /* all cookie headers and values were scanned. If we're looking for the
8661 * last occurrence, we may return it now.
8662 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008663 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008664 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008665 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008666}
8667
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008668/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008669 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008670 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008671 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008672 */
8673static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008674acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008675 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008676{
8677 struct http_txn *txn = l7;
8678 struct hdr_idx *idx = &txn->hdr_idx;
8679 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008680 const struct http_msg *msg;
8681 const char *hdr_name;
8682 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008683 int cnt;
8684 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008685 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008686
Willy Tarreau24e32d82012-04-23 23:55:44 +02008687 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008688 return 0;
8689
Willy Tarreaue333ec92012-04-16 16:26:40 +02008690 CHECK_HTTP_MESSAGE_FIRST();
8691
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008692 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008693 msg = &txn->req;
8694 hdr_name = "Cookie";
8695 hdr_name_len = 6;
8696 } else {
8697 msg = &txn->rsp;
8698 hdr_name = "Set-Cookie";
8699 hdr_name_len = 10;
8700 }
8701
Willy Tarreau9b28e032012-10-12 23:49:43 +02008702 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008703 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008704 ctx.idx = 0;
8705 cnt = 0;
8706
8707 while (1) {
8708 /* Note: val_beg == NULL every time we need to fetch a new header */
8709 if (!val_beg) {
8710 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8711 break;
8712
Willy Tarreau24e32d82012-04-23 23:55:44 +02008713 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008714 continue;
8715
8716 val_beg = ctx.line + ctx.val;
8717 val_end = val_beg + ctx.vlen;
8718 }
8719
Willy Tarreauf853c462012-04-23 18:53:56 +02008720 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008721 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008722 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008723 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008724 &smp->data.str.str,
8725 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008726 cnt++;
8727 }
8728 }
8729
Willy Tarreauf853c462012-04-23 18:53:56 +02008730 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008731 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008732 return 1;
8733}
8734
Willy Tarreau51539362012-05-08 12:46:28 +02008735/* Fetch an cookie's integer value. The integer value is returned. It
8736 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8737 */
8738static int
8739smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8740 const struct arg *args, struct sample *smp)
8741{
8742 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8743
8744 if (ret > 0) {
8745 smp->type = SMP_T_UINT;
8746 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8747 }
8748
8749 return ret;
8750}
8751
Willy Tarreau8797c062007-05-07 00:55:35 +02008752/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008753/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008754/************************************************************************/
8755
David Cournapeau16023ee2010-12-23 20:55:41 +09008756/*
8757 * Given a path string and its length, find the position of beginning of the
8758 * query string. Returns NULL if no query string is found in the path.
8759 *
8760 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8761 *
8762 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8763 */
bedis4c75cca2012-10-05 08:38:24 +02008764static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008765{
8766 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008767
bedis4c75cca2012-10-05 08:38:24 +02008768 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09008769 return p ? p + 1 : NULL;
8770}
8771
bedis4c75cca2012-10-05 08:38:24 +02008772static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008773{
bedis4c75cca2012-10-05 08:38:24 +02008774 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09008775}
8776
8777/*
8778 * Given a url parameter, find the starting position of the first occurence,
8779 * or NULL if the parameter is not found.
8780 *
8781 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8782 * the function will return query_string+8.
8783 */
8784static char*
8785find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02008786 char* url_param_name, size_t url_param_name_l,
8787 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008788{
8789 char *pos, *last;
8790
8791 pos = query_string;
8792 last = query_string + query_string_l - url_param_name_l - 1;
8793
8794 while (pos <= last) {
8795 if (pos[url_param_name_l] == '=') {
8796 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8797 return pos;
8798 pos += url_param_name_l + 1;
8799 }
bedis4c75cca2012-10-05 08:38:24 +02008800 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008801 pos++;
8802 pos++;
8803 }
8804 return NULL;
8805}
8806
8807/*
8808 * Given a url parameter name, returns its value and size into *value and
8809 * *value_l respectively, and returns non-zero. If the parameter is not found,
8810 * zero is returned and value/value_l are not touched.
8811 */
8812static int
8813find_url_param_value(char* path, size_t path_l,
8814 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02008815 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008816{
8817 char *query_string, *qs_end;
8818 char *arg_start;
8819 char *value_start, *value_end;
8820
bedis4c75cca2012-10-05 08:38:24 +02008821 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008822 if (!query_string)
8823 return 0;
8824
8825 qs_end = path + path_l;
8826 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02008827 url_param_name, url_param_name_l,
8828 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008829 if (!arg_start)
8830 return 0;
8831
8832 value_start = arg_start + url_param_name_l + 1;
8833 value_end = value_start;
8834
bedis4c75cca2012-10-05 08:38:24 +02008835 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008836 value_end++;
8837
8838 *value = value_start;
8839 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008840 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008841}
8842
8843static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008844smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8845 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008846{
bedis4c75cca2012-10-05 08:38:24 +02008847 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09008848 struct http_txn *txn = l7;
8849 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008850
bedis4c75cca2012-10-05 08:38:24 +02008851 if (!args || args[0].type != ARGT_STR ||
8852 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008853 return 0;
8854
8855 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008856
bedis4c75cca2012-10-05 08:38:24 +02008857 if (args[1].type)
8858 delim = *args[1].data.str.str;
8859
Willy Tarreau9b28e032012-10-12 23:49:43 +02008860 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02008861 args->data.str.str, args->data.str.len,
8862 &smp->data.str.str, &smp->data.str.len,
8863 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008864 return 0;
8865
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008866 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008867 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008868 return 1;
8869}
8870
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008871/* Return the signed integer value for the specified url parameter (see url_param
8872 * above).
8873 */
8874static int
8875smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8876 const struct arg *args, struct sample *smp)
8877{
8878 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
8879
8880 if (ret > 0) {
8881 smp->type = SMP_T_UINT;
8882 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8883 }
8884
8885 return ret;
8886}
8887
Willy Tarreau185b5c42012-04-26 15:11:51 +02008888/* This function is used to validate the arguments passed to any "hdr" fetch
8889 * keyword. These keywords support an optional positive or negative occurrence
8890 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8891 * is assumed that the types are already the correct ones. Returns 0 on error,
8892 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8893 * error message in case of error, that the caller is responsible for freeing.
8894 * The initial location must either be freeable or NULL.
8895 */
8896static int val_hdr(struct arg *arg, char **err_msg)
8897{
8898 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008899 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02008900 return 0;
8901 }
8902 return 1;
8903}
8904
Willy Tarreau4a568972010-05-12 08:08:50 +02008905/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008906/* All supported ACL keywords must be declared here. */
8907/************************************************************************/
8908
8909/* Note: must not be declared <const> as its list will be overwritten.
8910 * Please take care of keeping this list alphabetically sorted.
8911 */
8912static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008913 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8914 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8915 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8916 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8917 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8918 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8919 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8920 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8921
Willy Tarreau51539362012-05-08 12:46:28 +02008922 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8923 { "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 +02008924 { "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 +02008925 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8926 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8927 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8928 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8929 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8930 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8931 { "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 +02008932
Willy Tarreau185b5c42012-04-26 15:11:51 +02008933 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8934 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8935 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8936 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8937 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8938 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8939 { "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 },
8940 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8941 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8942 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8943 { "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 +02008944
8945 { "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 +02008946 { "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 +02008947 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8948
8949 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8950
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008951 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8952 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8953 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8954 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8955 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8956 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8957 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8958 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008959
8960 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8961 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8962 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8963
Willy Tarreau51539362012-05-08 12:46:28 +02008964 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8965 { "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 +02008966 { "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 +02008967 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8968 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8969 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8970 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8971 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8972 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8973 { "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 +02008974
Willy Tarreau185b5c42012-04-26 15:11:51 +02008975 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8976 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8977 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8978 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8979 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8980 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8981 { "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 },
8982 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8983 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8984 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8985 { "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 +02008986
8987 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8988
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008989 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8990 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8991 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8992 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8993 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8994 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8995 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8996 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8997 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8998 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008999
9000 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9001 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9002 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9003 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9004 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9005 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9006 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9007 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9008 { "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 +02009009 { "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 +02009010
9011 { NULL, NULL, NULL, NULL },
9012}};
9013
9014/************************************************************************/
9015/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009016/************************************************************************/
9017/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009018static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009019 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9020 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9021 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9022 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9023 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
9024 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
9025 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9026 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
9027 { "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 +02009028 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009029}};
9030
Willy Tarreau8797c062007-05-07 00:55:35 +02009031
9032__attribute__((constructor))
9033static void __http_protocol_init(void)
9034{
9035 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009036 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009037}
9038
9039
Willy Tarreau58f10d72006-12-04 02:26:12 +01009040/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009041 * Local variables:
9042 * c-indent-level: 8
9043 * c-basic-offset: 8
9044 * End:
9045 */