blob: 61730d753167eb85982d773fe2fc320a9a705ab0 [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
Willy Tarreau05d84602012-10-26 02:11:25 +02001977 /* Disable compression for older user agents announcing themselves as "Mozilla/4".
1978 * Note all of them are broken but they are very few and the broken ones are there.
1979 * 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 &&
1984 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0) {
1985 s->comp_algo = NULL;
1986 return 0;
1987 }
1988
William Lallemand82fe75c2012-10-23 10:25:10 +02001989 ctx.idx = 0;
1990 /* no compression when Cache-Control: no-transform found */
1991 while (http_find_header2("Cache-Control", 13, req->p, &txn->hdr_idx, &ctx)) {
1992 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) {
1993 s->comp_algo = NULL;
1994 return 0;
1995 }
1996 }
1997
1998 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001999 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 +02002000 ctx.idx = 0;
2001 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002002 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002003 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2004 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002005
2006 /* remove all occurrences of the header when "compression offload" is set */
2007
2008 if ((s->be->comp && s->be->comp->offload) ||
2009 (s->fe->comp && s->fe->comp->offload)) {
2010 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2011 ctx.idx = 0;
2012 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2013 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2014 }
2015 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002016 return 1;
2017 }
2018 }
2019 }
2020 }
2021
2022 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002023 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2024 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002025 if (comp_algo->add_data == identity_add_data) {
2026 s->comp_algo = comp_algo;
2027 return 1;
2028 }
2029 }
2030 }
2031
2032 s->comp_algo = NULL;
2033
2034 return 0;
2035}
2036
2037/*
2038 * Selects a comression algorithm depending of the server response.
2039 */
2040int select_compression_response_header(struct session *s, struct buffer *res)
2041{
2042 struct http_txn *txn = &s->txn;
2043 struct http_msg *msg = &txn->rsp;
2044 struct hdr_ctx ctx;
2045 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002046
2047 /* no common compression algorithm was found in request header */
2048 if (s->comp_algo == NULL)
2049 goto fail;
2050
2051 /* HTTP < 1.1 should not be compressed */
2052 if (!(msg->flags & HTTP_MSGF_VER_11))
2053 goto fail;
2054
William Lallemand82fe75c2012-10-23 10:25:10 +02002055 ctx.idx = 0;
2056
2057 /* Content-Length is null */
2058 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2059 goto fail;
2060
2061 /* content is already compressed */
2062 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2063 goto fail;
2064
2065 comp_type = NULL;
2066
2067 /* if there was a compression content-type option in the backend or the frontend
2068 * The backend have priority.
2069 */
2070 if ((s->be->comp && (comp_type = s->be->comp->types)) || (s->fe->comp && (comp_type = s->fe->comp->types))) {
2071 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2072 for (; comp_type; comp_type = comp_type->next) {
2073 if (strncmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
2074 /* this Content-Type should be compressed */
2075 break;
2076 }
2077 }
2078 /* this Content-Type should not be compressed */
2079 if (comp_type == NULL)
2080 goto fail;
2081 }
2082
2083 ctx.idx = 0;
2084
2085 /* remove Content-Length header */
2086 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2087 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2088
2089 /* add Transfer-Encoding header */
2090 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2091 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2092
2093 /*
2094 * Add Content-Encoding header when it's not identity encoding.
2095 * RFC 2616 : Identity encoding: This content-coding is used only in the
2096 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2097 * header.
2098 */
2099 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002100 trash.len = 18;
2101 memcpy(trash.str, "Content-Encoding: ", trash.len);
2102 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2103 trash.len += s->comp_algo->name_len;
2104 trash.str[trash.len] = '\0';
2105 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002106 }
2107
2108 /* initialize compression */
2109 if (s->comp_algo->init(&s->comp_ctx.strm, 1) < 0)
2110 goto fail;
2111
2112 return 1;
2113
2114fail:
2115 if (s->comp_algo) {
2116 s->comp_algo->end(&s->comp_ctx.strm);
2117 s->comp_algo = NULL;
2118 }
2119 return 0;
2120}
2121
2122
Willy Tarreaud787e662009-07-07 10:14:51 +02002123/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2124 * processing can continue on next analysers, or zero if it either needs more
2125 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2126 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2127 * when it has nothing left to do, and may remove any analyser when it wants to
2128 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002129 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002130int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002131{
Willy Tarreau59234e92008-11-30 23:51:27 +01002132 /*
2133 * We will parse the partial (or complete) lines.
2134 * We will check the request syntax, and also join multi-line
2135 * headers. An index of all the lines will be elaborated while
2136 * parsing.
2137 *
2138 * For the parsing, we use a 28 states FSM.
2139 *
2140 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002141 * req->buf->p = beginning of request
2142 * req->buf->p + msg->eoh = end of processed headers / start of current one
2143 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002144 * msg->eol = end of current header or line (LF or CRLF)
2145 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002146 *
2147 * At end of parsing, we may perform a capture of the error (if any), and
2148 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2149 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2150 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002151 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002152
Willy Tarreau59234e92008-11-30 23:51:27 +01002153 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002154 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002155 struct http_txn *txn = &s->txn;
2156 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002157 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002158
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002159 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 +01002160 now_ms, __FUNCTION__,
2161 s,
2162 req,
2163 req->rex, req->wex,
2164 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002165 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002166 req->analysers);
2167
Willy Tarreau52a0c602009-08-16 22:45:38 +02002168 /* we're speaking HTTP here, so let's speak HTTP to the client */
2169 s->srv_error = http_return_srv_error;
2170
Willy Tarreau83e3af02009-12-28 17:39:57 +01002171 /* There's a protected area at the end of the buffer for rewriting
2172 * purposes. We don't want to start to parse the request if the
2173 * protected area is affected, because we may have to move processed
2174 * data later, which is much more complicated.
2175 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002176 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002177 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002178 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002179 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2180 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2181 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002182 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002183 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002184 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002185 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002186 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002187 return 0;
2188 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002189 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2190 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2191 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002192 }
2193
Willy Tarreau065e8332010-01-08 00:30:20 +01002194 /* Note that we have the same problem with the response ; we
2195 * may want to send a redirect, error or anything which requires
2196 * some spare space. So we'll ensure that we have at least
2197 * maxrewrite bytes available in the response buffer before
2198 * processing that one. This will only affect pipelined
2199 * keep-alive requests.
2200 */
2201 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002202 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002203 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2204 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2205 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002206 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002207 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002208 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002209 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002210 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002211 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002212 return 0;
2213 }
2214 }
2215
Willy Tarreau9b28e032012-10-12 23:49:43 +02002216 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002217 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002218 }
2219
Willy Tarreau59234e92008-11-30 23:51:27 +01002220 /* 1: we might have to print this header in debug mode */
2221 if (unlikely((global.mode & MODE_DEBUG) &&
2222 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002223 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002224 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002225
Willy Tarreau9b28e032012-10-12 23:49:43 +02002226 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002227 /* this is a bit complex : in case of error on the request line,
2228 * we know that rq.l is still zero, so we display only the part
2229 * up to the end of the line (truncated by debug_hdr).
2230 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002231 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002232 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002233
Willy Tarreau59234e92008-11-30 23:51:27 +01002234 sol += hdr_idx_first_pos(&txn->hdr_idx);
2235 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002236
Willy Tarreau59234e92008-11-30 23:51:27 +01002237 while (cur_idx) {
2238 eol = sol + txn->hdr_idx.v[cur_idx].len;
2239 debug_hdr("clihdr", s, sol, eol);
2240 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2241 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002242 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002243 }
2244
Willy Tarreau58f10d72006-12-04 02:26:12 +01002245
Willy Tarreau59234e92008-11-30 23:51:27 +01002246 /*
2247 * Now we quickly check if we have found a full valid request.
2248 * If not so, we check the FD and buffer states before leaving.
2249 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002250 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002251 * requests are checked first. When waiting for a second request
2252 * on a keep-alive session, if we encounter and error, close, t/o,
2253 * we note the error in the session flags but don't set any state.
2254 * Since the error will be noted there, it will not be counted by
2255 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002256 * Last, we may increase some tracked counters' http request errors on
2257 * the cases that are deliberately the client's fault. For instance,
2258 * a timeout or connection reset is not counted as an error. However
2259 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002260 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002261
Willy Tarreau655dce92009-11-08 13:10:58 +01002262 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002263 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002264 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002265 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002266 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002267 session_inc_http_req_ctr(s);
2268 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002269 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002270 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002271 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002272
Willy Tarreau59234e92008-11-30 23:51:27 +01002273 /* 1: Since we are in header mode, if there's no space
2274 * left for headers, we won't be able to free more
2275 * later, so the session will never terminate. We
2276 * must terminate it now.
2277 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002278 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002279 /* FIXME: check if URI is set and return Status
2280 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002281 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002282 session_inc_http_req_ctr(s);
2283 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002284 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002285 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002286 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002287 goto return_bad_req;
2288 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002289
Willy Tarreau59234e92008-11-30 23:51:27 +01002290 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002291 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002292 if (!(s->flags & SN_ERR_MASK))
2293 s->flags |= SN_ERR_CLICL;
2294
Willy Tarreaufcffa692010-01-10 14:21:19 +01002295 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002296 goto failed_keep_alive;
2297
Willy Tarreau59234e92008-11-30 23:51:27 +01002298 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002299 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002300 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002301 session_inc_http_err_ctr(s);
2302 }
2303
Willy Tarreau59234e92008-11-30 23:51:27 +01002304 msg->msg_state = HTTP_MSG_ERROR;
2305 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002306
Willy Tarreauda7ff642010-06-23 11:44:09 +02002307 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002308 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002309 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002310 if (s->listener->counters)
2311 s->listener->counters->failed_req++;
2312
Willy Tarreau59234e92008-11-30 23:51:27 +01002313 if (!(s->flags & SN_FINST_MASK))
2314 s->flags |= SN_FINST_R;
2315 return 0;
2316 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002317
Willy Tarreau59234e92008-11-30 23:51:27 +01002318 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002319 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002320 if (!(s->flags & SN_ERR_MASK))
2321 s->flags |= SN_ERR_CLITO;
2322
Willy Tarreaufcffa692010-01-10 14:21:19 +01002323 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002324 goto failed_keep_alive;
2325
Willy Tarreau59234e92008-11-30 23:51:27 +01002326 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002327 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002328 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002329 session_inc_http_err_ctr(s);
2330 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002332 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002333 msg->msg_state = HTTP_MSG_ERROR;
2334 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002335
Willy Tarreauda7ff642010-06-23 11:44:09 +02002336 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002337 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002338 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002339 if (s->listener->counters)
2340 s->listener->counters->failed_req++;
2341
Willy Tarreau59234e92008-11-30 23:51:27 +01002342 if (!(s->flags & SN_FINST_MASK))
2343 s->flags |= SN_FINST_R;
2344 return 0;
2345 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002346
Willy Tarreau59234e92008-11-30 23:51:27 +01002347 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002348 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002349 if (!(s->flags & SN_ERR_MASK))
2350 s->flags |= SN_ERR_CLICL;
2351
Willy Tarreaufcffa692010-01-10 14:21:19 +01002352 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002353 goto failed_keep_alive;
2354
Willy Tarreau4076a152009-04-02 15:18:36 +02002355 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002356 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002358 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002359 msg->msg_state = HTTP_MSG_ERROR;
2360 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002361
Willy Tarreauda7ff642010-06-23 11:44:09 +02002362 session_inc_http_err_ctr(s);
2363 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002364 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002365 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002366 if (s->listener->counters)
2367 s->listener->counters->failed_req++;
2368
Willy Tarreau59234e92008-11-30 23:51:27 +01002369 if (!(s->flags & SN_FINST_MASK))
2370 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002371 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002372 }
2373
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002374 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002375 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2376 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002377#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002378 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002379 /* We need more data, we have to re-enable quick-ack in case we
2380 * previously disabled it, otherwise we might cause the client
2381 * to delay next data.
2382 */
Willy Tarreaufb7508a2012-05-21 16:47:54 +02002383 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002384 }
2385#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002386
Willy Tarreaufcffa692010-01-10 14:21:19 +01002387 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2388 /* If the client starts to talk, let's fall back to
2389 * request timeout processing.
2390 */
2391 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002392 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002393 }
2394
Willy Tarreau59234e92008-11-30 23:51:27 +01002395 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002396 if (!tick_isset(req->analyse_exp)) {
2397 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2398 (txn->flags & TX_WAIT_NEXT_RQ) &&
2399 tick_isset(s->be->timeout.httpka))
2400 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2401 else
2402 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2403 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002404
Willy Tarreau59234e92008-11-30 23:51:27 +01002405 /* we're not ready yet */
2406 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002407
2408 failed_keep_alive:
2409 /* Here we process low-level errors for keep-alive requests. In
2410 * short, if the request is not the first one and it experiences
2411 * a timeout, read error or shutdown, we just silently close so
2412 * that the client can try again.
2413 */
2414 txn->status = 0;
2415 msg->msg_state = HTTP_MSG_RQBEFORE;
2416 req->analysers = 0;
2417 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002418 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002419 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002420 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002421 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002422
Willy Tarreaud787e662009-07-07 10:14:51 +02002423 /* OK now we have a complete HTTP request with indexed headers. Let's
2424 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002425 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002426 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002427 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002428 * byte after the last LF. msg->sov points to the first byte of data.
2429 * msg->eol cannot be trusted because it may have been left uninitialized
2430 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002431 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002432
Willy Tarreauda7ff642010-06-23 11:44:09 +02002433 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002434 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2435
Willy Tarreaub16a5742010-01-10 14:46:16 +01002436 if (txn->flags & TX_WAIT_NEXT_RQ) {
2437 /* kill the pending keep-alive timeout */
2438 txn->flags &= ~TX_WAIT_NEXT_RQ;
2439 req->analyse_exp = TICK_ETERNITY;
2440 }
2441
2442
Willy Tarreaud787e662009-07-07 10:14:51 +02002443 /* Maybe we found in invalid header name while we were configured not
2444 * to block on that, so we have to capture it now.
2445 */
2446 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002447 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002448
Willy Tarreau59234e92008-11-30 23:51:27 +01002449 /*
2450 * 1: identify the method
2451 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002452 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002453
2454 /* we can make use of server redirect on GET and HEAD */
2455 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2456 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002457
Willy Tarreau59234e92008-11-30 23:51:27 +01002458 /*
2459 * 2: check if the URI matches the monitor_uri.
2460 * We have to do this for every request which gets in, because
2461 * the monitor-uri is defined by the frontend.
2462 */
2463 if (unlikely((s->fe->monitor_uri_len != 0) &&
2464 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002465 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002466 s->fe->monitor_uri,
2467 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002468 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002469 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002470 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002471 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002472
Willy Tarreau59234e92008-11-30 23:51:27 +01002473 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002474 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002475
Willy Tarreau59234e92008-11-30 23:51:27 +01002476 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002477 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002478 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002479
Willy Tarreau59234e92008-11-30 23:51:27 +01002480 ret = acl_pass(ret);
2481 if (cond->pol == ACL_COND_UNLESS)
2482 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002483
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 if (ret) {
2485 /* we fail this request, let's return 503 service unavail */
2486 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002487 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002488 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002489 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002490 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002491
Willy Tarreau59234e92008-11-30 23:51:27 +01002492 /* nothing to fail, let's reply normaly */
2493 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002494 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002495 goto return_prx_cond;
2496 }
2497
2498 /*
2499 * 3: Maybe we have to copy the original REQURI for the logs ?
2500 * Note: we cannot log anymore if the request has been
2501 * classified as invalid.
2502 */
2503 if (unlikely(s->logs.logwait & LW_REQ)) {
2504 /* we have a complete HTTP request that we must log */
2505 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2506 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 if (urilen >= REQURI_LEN)
2509 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002510 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002512
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 if (!(s->logs.logwait &= ~LW_REQ))
2514 s->do_log(s);
2515 } else {
2516 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002517 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002518 }
Willy Tarreau06619262006-12-17 08:37:22 +01002519
William Lallemanda73203e2012-03-12 12:48:57 +01002520 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2521 s->unique_id = pool_alloc2(pool2_uniqueid);
2522 }
2523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002525 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002526 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002527
Willy Tarreau5b154472009-12-21 20:11:07 +01002528 /* ... and check if the request is HTTP/1.1 or above */
2529 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002530 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2531 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2532 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002533 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002534
2535 /* "connection" has not been parsed yet */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01002536 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL);
Willy Tarreau5b154472009-12-21 20:11:07 +01002537
Willy Tarreau88d349d2010-01-25 12:15:43 +01002538 /* if the frontend has "option http-use-proxy-header", we'll check if
2539 * we have what looks like a proxied connection instead of a connection,
2540 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2541 * Note that this is *not* RFC-compliant, however browsers and proxies
2542 * happen to do that despite being non-standard :-(
2543 * We consider that a request not beginning with either '/' or '*' is
2544 * a proxied connection, which covers both "scheme://location" and
2545 * CONNECT ip:port.
2546 */
2547 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002548 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002549 txn->flags |= TX_USE_PX_CONN;
2550
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002551 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002552 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002553
Willy Tarreau59234e92008-11-30 23:51:27 +01002554 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002555 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002556 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002557 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002558
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002559 /* 6: determine the transfer-length.
2560 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2561 * the presence of a message-body in a REQUEST and its transfer length
2562 * must be determined that way (in order of precedence) :
2563 * 1. The presence of a message-body in a request is signaled by the
2564 * inclusion of a Content-Length or Transfer-Encoding header field
2565 * in the request's header fields. When a request message contains
2566 * both a message-body of non-zero length and a method that does
2567 * not define any semantics for that request message-body, then an
2568 * origin server SHOULD either ignore the message-body or respond
2569 * with an appropriate error message (e.g., 413). A proxy or
2570 * gateway, when presented the same request, SHOULD either forward
2571 * the request inbound with the message- body or ignore the
2572 * message-body when determining a response.
2573 *
2574 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2575 * and the "chunked" transfer-coding (Section 6.2) is used, the
2576 * transfer-length is defined by the use of this transfer-coding.
2577 * If a Transfer-Encoding header field is present and the "chunked"
2578 * transfer-coding is not present, the transfer-length is defined
2579 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002580 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002581 * 3. If a Content-Length header field is present, its decimal value in
2582 * OCTETs represents both the entity-length and the transfer-length.
2583 * If a message is received with both a Transfer-Encoding header
2584 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002585 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002586 * 4. By the server closing the connection. (Closing the connection
2587 * cannot be used to indicate the end of a request body, since that
2588 * would leave no possibility for the server to send back a response.)
2589 *
2590 * Whenever a transfer-coding is applied to a message-body, the set of
2591 * transfer-codings MUST include "chunked", unless the message indicates
2592 * it is terminated by closing the connection. When the "chunked"
2593 * transfer-coding is used, it MUST be the last transfer-coding applied
2594 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002595 */
2596
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002597 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002598 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002599 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002600 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002601 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002602 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002603 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2604 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002605 /* bad transfer-encoding (chunked followed by something else) */
2606 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002607 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002608 break;
2609 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002610 }
2611
Willy Tarreau32b47f42009-10-18 20:55:02 +02002612 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002613 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002614 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002615 signed long long cl;
2616
Willy Tarreauad14f752011-09-02 20:33:27 +02002617 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002618 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002619 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002620 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002621
Willy Tarreauad14f752011-09-02 20:33:27 +02002622 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002623 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002624 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002625 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002626
Willy Tarreauad14f752011-09-02 20:33:27 +02002627 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002628 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002629 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002630 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002631
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002632 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002633 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002634 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002635 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002636
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002637 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002638 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002639 }
2640
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002641 /* bodyless requests have a known length */
2642 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002643 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002644
Willy Tarreaud787e662009-07-07 10:14:51 +02002645 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002646 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002647 req->analyse_exp = TICK_ETERNITY;
2648 return 1;
2649
2650 return_bad_req:
2651 /* We centralize bad requests processing here */
2652 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2653 /* we detected a parsing error. We want to archive this request
2654 * in the dedicated proxy area for later troubleshooting.
2655 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002656 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002657 }
2658
2659 txn->req.msg_state = HTTP_MSG_ERROR;
2660 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002661 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002662
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002663 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002664 if (s->listener->counters)
2665 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002666
2667 return_prx_cond:
2668 if (!(s->flags & SN_ERR_MASK))
2669 s->flags |= SN_ERR_PRXCOND;
2670 if (!(s->flags & SN_FINST_MASK))
2671 s->flags |= SN_FINST_R;
2672
2673 req->analysers = 0;
2674 req->analyse_exp = TICK_ETERNITY;
2675 return 0;
2676}
2677
Cyril Bonté70be45d2010-10-12 00:14:35 +02002678/* We reached the stats page through a POST request.
2679 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002680 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002681 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002682int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002683{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002684 struct proxy *px = NULL;
2685 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002686
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002687 char key[LINESIZE];
2688 int action = ST_ADM_ACTION_NONE;
2689 int reprocess = 0;
2690
2691 int total_servers = 0;
2692 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002693
2694 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002695 char *st_cur_param = NULL;
2696 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002697
Willy Tarreau9b28e032012-10-12 23:49:43 +02002698 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002699 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002700
2701 cur_param = next_param = end_params;
2702
Willy Tarreau9b28e032012-10-12 23:49:43 +02002703 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002704 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002705 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002706 return 1;
2707 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002708 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002709 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002710 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002711 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002712 }
2713
2714 *end_params = '\0';
2715
Willy Tarreau295a8372011-03-10 11:25:07 +01002716 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002717
2718 /*
2719 * Parse the parameters in reverse order to only store the last value.
2720 * From the html form, the backend and the action are at the end.
2721 */
2722 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002723 char *value;
2724 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002725
2726 cur_param--;
2727 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002728 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002729 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002730 poffset = (cur_param != first_param ? 1 : 0);
2731 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2732 if ((plen > 0) && (plen <= sizeof(key))) {
2733 strncpy(key, cur_param + poffset, plen);
2734 key[plen - 1] = '\0';
2735 } else {
2736 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2737 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002738 }
2739
2740 /* Parse the value */
2741 value = key;
2742 while (*value != '\0' && *value != '=') {
2743 value++;
2744 }
2745 if (*value == '=') {
2746 /* Ok, a value is found, we can mark the end of the key */
2747 *value++ = '\0';
2748 }
2749
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002750 if (!url_decode(key) || !url_decode(value))
2751 break;
2752
Cyril Bonté70be45d2010-10-12 00:14:35 +02002753 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002754 if (!px && (strcmp(key, "b") == 0)) {
2755 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2756 /* the backend name is unknown or ambiguous (duplicate names) */
2757 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2758 goto out;
2759 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002760 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002761 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002762 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002763 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002764 }
2765 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002766 action = ST_ADM_ACTION_ENABLE;
2767 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002768 else if (strcmp(value, "stop") == 0) {
2769 action = ST_ADM_ACTION_STOP;
2770 }
2771 else if (strcmp(value, "start") == 0) {
2772 action = ST_ADM_ACTION_START;
2773 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002774 else if (strcmp(value, "shutdown") == 0) {
2775 action = ST_ADM_ACTION_SHUTDOWN;
2776 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002777 else {
2778 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2779 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002780 }
2781 }
2782 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002783 if (!(px && action)) {
2784 /*
2785 * Indicates that we'll need to reprocess the parameters
2786 * as soon as backend and action are known
2787 */
2788 if (!reprocess) {
2789 st_cur_param = cur_param;
2790 st_next_param = next_param;
2791 }
2792 reprocess = 1;
2793 }
2794 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002795 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002796 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002797 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002798 /* Not already in maintenance, we can change the server state */
2799 sv->state |= SRV_MAINTAIN;
2800 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002801 altered_servers++;
2802 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002803 }
2804 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002805 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002806 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002807 /* Already in maintenance, we can change the server state */
2808 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002809 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002810 altered_servers++;
2811 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002812 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002813 break;
2814 case ST_ADM_ACTION_STOP:
2815 case ST_ADM_ACTION_START:
2816 if (action == ST_ADM_ACTION_START)
2817 sv->uweight = sv->iweight;
2818 else
2819 sv->uweight = 0;
2820
2821 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2822 /* we must take care of not pushing the server to full throttle during slow starts */
2823 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2824 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2825 else
2826 sv->eweight = BE_WEIGHT_SCALE;
2827 sv->eweight *= sv->uweight;
2828 } else {
2829 sv->eweight = sv->uweight;
2830 }
2831
2832 /* static LB algorithms are a bit harder to update */
2833 if (px->lbprm.update_server_eweight)
2834 px->lbprm.update_server_eweight(sv);
2835 else if (sv->eweight) {
2836 if (px->lbprm.set_server_status_up)
2837 px->lbprm.set_server_status_up(sv);
2838 }
2839 else {
2840 if (px->lbprm.set_server_status_down)
2841 px->lbprm.set_server_status_down(sv);
2842 }
2843 altered_servers++;
2844 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002845 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002846 case ST_ADM_ACTION_SHUTDOWN:
2847 if (px->state != PR_STSTOPPED) {
2848 struct session *sess, *sess_bck;
2849
2850 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2851 if (sess->srv_conn == sv)
2852 session_shutdown(sess, SN_ERR_KILLED);
2853
2854 altered_servers++;
2855 total_servers++;
2856 }
2857 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002858 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002859 } else {
2860 /* the server name is unknown or ambiguous (duplicate names) */
2861 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002862 }
2863 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002864 if (reprocess && px && action) {
2865 /* Now, we know the backend and the action chosen by the user.
2866 * We can safely restart from the first server parameter
2867 * to reprocess them
2868 */
2869 cur_param = st_cur_param;
2870 next_param = st_next_param;
2871 reprocess = 0;
2872 goto reprocess_servers;
2873 }
2874
Cyril Bonté70be45d2010-10-12 00:14:35 +02002875 next_param = cur_param;
2876 }
2877 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002878
2879 if (total_servers == 0) {
2880 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2881 }
2882 else if (altered_servers == 0) {
2883 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2884 }
2885 else if (altered_servers == total_servers) {
2886 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2887 }
2888 else {
2889 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2890 }
2891 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002892 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002893}
2894
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002895/* returns a pointer to the first rule which forbids access (deny or http_auth),
2896 * or NULL if everything's OK.
2897 */
Willy Tarreauff011f22011-01-06 17:51:27 +01002898static inline struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002899http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
2900{
Willy Tarreauff011f22011-01-06 17:51:27 +01002901 struct http_req_rule *rule;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002902
Willy Tarreauff011f22011-01-06 17:51:27 +01002903 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002904 int ret = 1;
2905
Willy Tarreauff011f22011-01-06 17:51:27 +01002906 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002907 continue;
2908
2909 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01002910 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002911 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002912 ret = acl_pass(ret);
2913
Willy Tarreauff011f22011-01-06 17:51:27 +01002914 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002915 ret = !ret;
2916 }
2917
2918 if (ret) {
Willy Tarreauff011f22011-01-06 17:51:27 +01002919 if (rule->action == HTTP_REQ_ACT_ALLOW)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002920 return NULL; /* no problem */
2921 else
Willy Tarreauff011f22011-01-06 17:51:27 +01002922 return rule; /* most likely a deny or auth rule */
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002923 }
2924 }
2925 return NULL;
2926}
2927
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002928/* This stream analyser runs all HTTP request processing which is common to
2929 * frontends and backends, which means blocking ACLs, filters, connection-close,
2930 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02002931 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002932 * either needs more data or wants to immediately abort the request (eg: deny,
2933 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02002934 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002935int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02002936{
Willy Tarreaud787e662009-07-07 10:14:51 +02002937 struct http_txn *txn = &s->txn;
2938 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002939 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01002940 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002941 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01002942 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09002943 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02002944
Willy Tarreau655dce92009-11-08 13:10:58 +01002945 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02002946 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002947 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02002948 return 0;
2949 }
2950
Willy Tarreau3a816292009-07-07 10:55:49 +02002951 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002952 req->analyse_exp = TICK_ETERNITY;
2953
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002954 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 +02002955 now_ms, __FUNCTION__,
2956 s,
2957 req,
2958 req->rex, req->wex,
2959 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002960 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02002961 req->analysers);
2962
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002963 /* first check whether we have some ACLs set to block this request */
2964 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002965 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02002966
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002967 ret = acl_pass(ret);
2968 if (cond->pol == ACL_COND_UNLESS)
2969 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01002970
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002971 if (ret) {
2972 txn->status = 403;
2973 /* let's log the request time */
2974 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002975 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002976 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002977 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01002978 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02002979 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002980
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002981 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01002982 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01002983
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002984 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01002985 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002986 do_stats = stats_check_uri(s->rep->prod, txn, px);
2987 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01002988 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 +01002989 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002990 else
2991 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002992
Willy Tarreauf68a15a2011-01-06 16:53:21 +01002993 /* return a 403 if either rule has blocked */
Willy Tarreauff011f22011-01-06 17:51:27 +01002994 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01002995 txn->status = 403;
2996 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02002997 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02002998 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01002999 s->fe->fe_counters.denied_req++;
3000 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3001 s->be->be_counters.denied_req++;
3002 if (s->listener->counters)
3003 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003004 goto return_prx_cond;
3005 }
3006
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003007 /* try headers filters */
3008 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003009 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003010 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003011
Willy Tarreau59234e92008-11-30 23:51:27 +01003012 /* has the request been denied ? */
3013 if (txn->flags & TX_CLDENY) {
3014 /* no need to go further */
3015 txn->status = 403;
3016 /* let's log the request time */
3017 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003018 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003019 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003020 goto return_prx_cond;
3021 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003022
3023 /* When a connection is tarpitted, we use the tarpit timeout,
3024 * which may be the same as the connect timeout if unspecified.
3025 * If unset, then set it to zero because we really want it to
3026 * eventually expire. We build the tarpit as an analyser.
3027 */
3028 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003029 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003030 /* wipe the request out so that we can drop the connection early
3031 * if the client closes first.
3032 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003033 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003034 req->analysers = 0; /* remove switching rules etc... */
3035 req->analysers |= AN_REQ_HTTP_TARPIT;
3036 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3037 if (!req->analyse_exp)
3038 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003039 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003040 return 1;
3041 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003042 }
Willy Tarreau06619262006-12-17 08:37:22 +01003043
Willy Tarreau5b154472009-12-21 20:11:07 +01003044 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3045 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003046 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3047 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003048 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3049 * avoid to redo the same work if FE and BE have the same settings (common).
3050 * The method consists in checking if options changed between the two calls
3051 * (implying that either one is non-null, or one of them is non-null and we
3052 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003053 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003054
Willy Tarreaudc008c52010-02-01 16:20:08 +01003055 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3056 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3057 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3058 (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 +01003059 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003060
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003061 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3062 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003063 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003064 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3065 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003066 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003067 tmp = TX_CON_WANT_CLO;
3068
Willy Tarreau5b154472009-12-21 20:11:07 +01003069 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3070 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003071
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003072 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3073 /* parse the Connection header and possibly clean it */
3074 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003075 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003076 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3077 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003078 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003079 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003080 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003081 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003082 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003083
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003084 /* check if client or config asks for explicit close in KAL/SCL */
3085 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3086 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3087 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003088 (!(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 +01003089 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003090 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003091 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003092 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3093 }
Willy Tarreau78599912009-10-17 20:12:21 +02003094
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003095 /* we can be blocked here because the request needs to be authenticated,
3096 * either to pass or to access stats.
3097 */
Willy Tarreauff011f22011-01-06 17:51:27 +01003098 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003099 char *realm = http_req_last_rule->http_auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003100
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003101 if (!realm)
3102 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3103
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003104 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003105 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003106 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003107 /* on 401 we still count one error, because normal browsing
3108 * won't significantly increase the counter but brute force
3109 * attempts will.
3110 */
3111 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003112 goto return_prx_cond;
3113 }
3114
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003115 /* add request headers from the rule sets in the same order */
3116 list_for_each_entry(wl, &px->req_add, list) {
3117 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003118 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003119 ret = acl_pass(ret);
3120 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3121 ret = !ret;
3122 if (!ret)
3123 continue;
3124 }
3125
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003126 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003127 goto return_bad_req;
3128 }
3129
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003130 if (do_stats) {
Cyril Bonté474be412010-10-12 00:14:36 +02003131 struct stats_admin_rule *stats_admin_rule;
3132
3133 /* We need to provide stats for this request.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003134 * FIXME!!! that one is rather dangerous, we want to
3135 * make it follow standard rules (eg: clear req->analysers).
3136 */
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003137
Cyril Bonté474be412010-10-12 00:14:36 +02003138 /* now check whether we have some admin rules for this request */
3139 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
3140 int ret = 1;
3141
3142 if (stats_admin_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003143 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 +02003144 ret = acl_pass(ret);
3145 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
3146 ret = !ret;
3147 }
3148
3149 if (ret) {
3150 /* no rule, or the rule matches */
Willy Tarreau295a8372011-03-10 11:25:07 +01003151 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
Cyril Bonté474be412010-10-12 00:14:36 +02003152 break;
3153 }
3154 }
3155
Cyril Bonté70be45d2010-10-12 00:14:35 +02003156 /* Was the status page requested with a POST ? */
3157 if (txn->meth == HTTP_METH_POST) {
Willy Tarreau295a8372011-03-10 11:25:07 +01003158 if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003159 if (msg->msg_state < HTTP_MSG_100_SENT) {
3160 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3161 * send an HTTP/1.1 100 Continue intermediate response.
3162 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003163 if (msg->flags & HTTP_MSGF_VER_11) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003164 struct hdr_ctx ctx;
3165 ctx.idx = 0;
3166 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003167 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Cyril Bonté23b39d92011-02-10 22:54:44 +01003168 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003169 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003170 }
3171 }
3172 msg->msg_state = HTTP_MSG_100_SENT;
3173 s->logs.tv_request = now; /* update the request timer to reflect full request */
3174 }
Willy Tarreau295a8372011-03-10 11:25:07 +01003175 if (!http_process_req_stat_post(s->rep->prod, txn, req)) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01003176 /* we need more data */
3177 req->analysers |= an_bit;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003178 channel_dont_connect(req);
Cyril Bonté23b39d92011-02-10 22:54:44 +01003179 return 0;
3180 }
Cyril Bonté474be412010-10-12 00:14:36 +02003181 } else {
Willy Tarreau295a8372011-03-10 11:25:07 +01003182 s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY;
Cyril Bonté474be412010-10-12 00:14:36 +02003183 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02003184 }
3185
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003186 s->logs.tv_request = now;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003187 s->task->nice = -32; /* small boost for HTTP statistics */
Willy Tarreaub24281b2011-02-13 13:16:36 +01003188 stream_int_register_handler(s->rep->prod, &http_stats_applet);
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003189 copy_target(&s->target, &s->rep->prod->conn->target); // for logging only
3190 s->rep->prod->conn->xprt_ctx = s;
Willy Tarreaubc4af052011-02-13 13:25:14 +01003191 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003192 req->analysers = 0;
Willy Tarreaueabea072011-09-10 23:29:44 +02003193 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3194 s->fe->fe_counters.intercepted_req++;
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003195
3196 return 0;
3197
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003198 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003199
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003200 /* check whether we have some ACLs set to redirect this request */
3201 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003202 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003203
Willy Tarreauf285f542010-01-03 20:03:03 +01003204 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003205 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003206 ret = acl_pass(ret);
3207 if (rule->cond->pol == ACL_COND_UNLESS)
3208 ret = !ret;
3209 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003210
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003211 if (ret) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003212 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003213
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003214 /* build redirect message */
3215 switch(rule->code) {
3216 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003217 msg_fmt = HTTP_303;
3218 break;
3219 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003220 msg_fmt = HTTP_301;
3221 break;
3222 case 302:
3223 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003224 msg_fmt = HTTP_302;
3225 break;
3226 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003227
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003228 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003229 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003230
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003231 switch(rule->type) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003232 case REDIRECT_TYPE_SCHEME: {
3233 const char *path;
3234 const char *host;
3235 struct hdr_ctx ctx;
3236 int pathlen;
3237 int hostlen;
3238
3239 host = "";
3240 hostlen = 0;
3241 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003242 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 +02003243 host = ctx.line + ctx.val;
3244 hostlen = ctx.vlen;
3245 }
3246
3247 path = http_get_path(txn);
3248 /* build message using path */
3249 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003250 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003251 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3252 int qs = 0;
3253 while (qs < pathlen) {
3254 if (path[qs] == '?') {
3255 pathlen = qs;
3256 break;
3257 }
3258 qs++;
3259 }
3260 }
3261 } else {
3262 path = "/";
3263 pathlen = 1;
3264 }
3265
3266 /* check if we can add scheme + "://" + host + path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003267 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003268 goto return_bad_req;
3269
3270 /* add scheme */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003271 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3272 trash.len += rule->rdr_len;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003273
3274 /* add "://" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003275 memcpy(trash.str + trash.len, "://", 3);
3276 trash.len += 3;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003277
3278 /* add host */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003279 memcpy(trash.str + trash.len, host, hostlen);
3280 trash.len += hostlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003281
3282 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003283 memcpy(trash.str + trash.len, path, pathlen);
3284 trash.len += pathlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003285
3286 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003287 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003288 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003289 if (trash.len > trash.size - 5)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003290 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003291 trash.str[trash.len] = '/';
3292 trash.len++;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003293 }
3294
3295 break;
3296 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003297 case REDIRECT_TYPE_PREFIX: {
3298 const char *path;
3299 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003300
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003301 path = http_get_path(txn);
3302 /* build message using path */
3303 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003304 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003305 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3306 int qs = 0;
3307 while (qs < pathlen) {
3308 if (path[qs] == '?') {
3309 pathlen = qs;
3310 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003311 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003312 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003313 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003314 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003315 } else {
3316 path = "/";
3317 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003318 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003319
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003320 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003321 goto return_bad_req;
3322
3323 /* add prefix. Note that if prefix == "/", we don't want to
3324 * add anything, otherwise it makes it hard for the user to
3325 * configure a self-redirection.
3326 */
3327 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003328 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3329 trash.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003330 }
3331
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003332 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003333 memcpy(trash.str + trash.len, path, pathlen);
3334 trash.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003335
3336 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003337 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003338 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003339 if (trash.len > trash.size - 5)
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003340 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003341 trash.str[trash.len] = '/';
3342 trash.len++;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003343 }
3344
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003345 break;
3346 }
3347 case REDIRECT_TYPE_LOCATION:
3348 default:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003349 if (trash.len + rule->rdr_len > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003350 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003351
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003352 /* add location */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003353 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3354 trash.len += rule->rdr_len;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003355 break;
3356 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003357
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003358 if (rule->cookie_len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003359 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3360 trash.len += 14;
3361 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3362 trash.len += rule->cookie_len;
3363 memcpy(trash.str + trash.len, "\r\n", 2);
3364 trash.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003365 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003366
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003367 /* add end of headers and the keep-alive/close status.
3368 * We may choose to set keep-alive if the Location begins
3369 * with a slash, because the client will come back to the
3370 * same server.
3371 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003372 txn->status = rule->code;
3373 /* let's log the request time */
3374 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003375
3376 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003377 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3378 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003379 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3380 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3381 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003382 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003383 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003384 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3385 trash.len += 30;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003386 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003387 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3388 trash.len += 24;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003389 }
Willy Tarreau75661452010-01-10 10:35:01 +01003390 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003391 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3392 trash.len += 4;
3393 bo_inject(req->prod->ob, trash.str, trash.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003394 /* "eat" the request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003395 bi_fast_delete(req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003396 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003397 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003398 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3399 txn->req.msg_state = HTTP_MSG_CLOSED;
3400 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003401 break;
3402 } else {
3403 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003404 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003405 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3406 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003407 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003408 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3409 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003410 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003411 stream_int_retnclose(req->prod, &trash);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003412 goto return_prx_cond;
3413 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003414 }
3415 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003416
Willy Tarreau2be39392010-01-03 17:24:51 +01003417 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3418 * If this happens, then the data will not come immediately, so we must
3419 * send all what we have without waiting. Note that due to the small gain
3420 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003421 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003422 * itself once used.
3423 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003424 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003425
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003426 /* that's OK for us now, let's move on to next analysers */
3427 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003428
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003429 return_bad_req:
3430 /* We centralize bad requests processing here */
3431 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3432 /* we detected a parsing error. We want to archive this request
3433 * in the dedicated proxy area for later troubleshooting.
3434 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003435 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003436 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003437
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003438 txn->req.msg_state = HTTP_MSG_ERROR;
3439 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003440 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003441
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003442 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003443 if (s->listener->counters)
3444 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003445
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003446 return_prx_cond:
3447 if (!(s->flags & SN_ERR_MASK))
3448 s->flags |= SN_ERR_PRXCOND;
3449 if (!(s->flags & SN_FINST_MASK))
3450 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003451
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003452 req->analysers = 0;
3453 req->analyse_exp = TICK_ETERNITY;
3454 return 0;
3455}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003456
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003457/* This function performs all the processing enabled for the current request.
3458 * It returns 1 if the processing can continue on next analysers, or zero if it
3459 * needs more data, encounters an error, or wants to immediately abort the
3460 * request. It relies on buffers flags, and updates s->req->analysers.
3461 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003462int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003463{
3464 struct http_txn *txn = &s->txn;
3465 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003466
Willy Tarreau655dce92009-11-08 13:10:58 +01003467 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003468 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003469 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003470 return 0;
3471 }
3472
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003473 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 +02003474 now_ms, __FUNCTION__,
3475 s,
3476 req,
3477 req->rex, req->wex,
3478 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003479 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003480 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003481
William Lallemand82fe75c2012-10-23 10:25:10 +02003482 if (s->fe->comp || s->be->comp)
3483 select_compression_request_header(s, req->buf);
3484
Willy Tarreau59234e92008-11-30 23:51:27 +01003485 /*
3486 * Right now, we know that we have processed the entire headers
3487 * and that unwanted requests have been filtered out. We can do
3488 * whatever we want with the remaining request. Also, now we
3489 * may have separate values for ->fe, ->be.
3490 */
Willy Tarreau06619262006-12-17 08:37:22 +01003491
Willy Tarreau59234e92008-11-30 23:51:27 +01003492 /*
3493 * If HTTP PROXY is set we simply get remote server address
3494 * parsing incoming request.
3495 */
3496 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003497 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 +01003498 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003499
Willy Tarreau59234e92008-11-30 23:51:27 +01003500 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003501 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003502 * Note that doing so might move headers in the request, but
3503 * the fields will stay coherent and the URI will not move.
3504 * This should only be performed in the backend.
3505 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003506 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003507 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3508 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003509
Willy Tarreau59234e92008-11-30 23:51:27 +01003510 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003511 * 8: the appsession cookie was looked up very early in 1.2,
3512 * so let's do the same now.
3513 */
3514
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003515 /* It needs to look into the URI unless persistence must be ignored */
3516 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003517 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 +01003518 }
3519
William Lallemanda73203e2012-03-12 12:48:57 +01003520 /* add unique-id if "header-unique-id" is specified */
3521
3522 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3523 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3524
3525 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003526 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3527 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003528 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003529 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003530 goto return_bad_req;
3531 }
3532
Cyril Bontéb21570a2009-11-29 20:04:48 +01003533 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003534 * 9: add X-Forwarded-For if either the frontend or the backend
3535 * asks for it.
3536 */
3537 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003538 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003539 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003540 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3541 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003542 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003543 /* The header is set to be added only if none is present
3544 * and we found it, so don't do anything.
3545 */
3546 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003547 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003548 /* Add an X-Forwarded-For header unless the source IP is
3549 * in the 'except' network range.
3550 */
3551 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003552 (((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 +01003553 != s->fe->except_net.s_addr) &&
3554 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003555 (((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 +01003556 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003557 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003558 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003559 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003560
3561 /* Note: we rely on the backend to get the header name to be used for
3562 * x-forwarded-for, because the header is really meant for the backends.
3563 * However, if the backend did not specify any option, we have to rely
3564 * on the frontend's header name.
3565 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003566 if (s->be->fwdfor_hdr_len) {
3567 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003568 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003569 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003570 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003571 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003572 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003573 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003574
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003575 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003576 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003577 }
3578 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003579 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003580 /* FIXME: for the sake of completeness, we should also support
3581 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003582 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003583 int len;
3584 char pn[INET6_ADDRSTRLEN];
3585 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003586 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003587 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003588
Willy Tarreau59234e92008-11-30 23:51:27 +01003589 /* Note: we rely on the backend to get the header name to be used for
3590 * x-forwarded-for, because the header is really meant for the backends.
3591 * However, if the backend did not specify any option, we have to rely
3592 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003593 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003594 if (s->be->fwdfor_hdr_len) {
3595 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003596 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003597 } else {
3598 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003599 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003600 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003601 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003602
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003603 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003604 goto return_bad_req;
3605 }
3606 }
3607
3608 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003609 * 10: add X-Original-To if either the frontend or the backend
3610 * asks for it.
3611 */
3612 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3613
3614 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003615 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003616 /* Add an X-Original-To header unless the destination IP is
3617 * in the 'except' network range.
3618 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003619 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003620
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003621 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003622 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003623 (((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 +02003624 != s->fe->except_to.s_addr) &&
3625 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003626 (((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 +02003627 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003628 int len;
3629 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003630 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003631
3632 /* Note: we rely on the backend to get the header name to be used for
3633 * x-original-to, because the header is really meant for the backends.
3634 * However, if the backend did not specify any option, we have to rely
3635 * on the frontend's header name.
3636 */
3637 if (s->be->orgto_hdr_len) {
3638 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003639 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003640 } else {
3641 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003642 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003643 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003644 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003645
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003646 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003647 goto return_bad_req;
3648 }
3649 }
3650 }
3651
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003652 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */
3653 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003654 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003655 unsigned int want_flags = 0;
3656
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003657 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003658 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3659 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3660 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003661 want_flags |= TX_CON_CLO_SET;
3662 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003663 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3664 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3665 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003666 want_flags |= TX_CON_KAL_SET;
3667 }
3668
3669 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003670 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003671 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003672
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003673
Willy Tarreau522d6c02009-12-06 18:49:18 +01003674 /* If we have no server assigned yet and we're balancing on url_param
3675 * with a POST request, we may be interested in checking the body for
3676 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003677 */
3678 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3679 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003680 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003681 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003682 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003683 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003684 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003685
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003686 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003687 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003688#ifdef TCP_QUICKACK
3689 /* We expect some data from the client. Unless we know for sure
3690 * we already have a full request, we have to re-enable quick-ack
3691 * in case we previously disabled it, otherwise we might cause
3692 * the client to delay further data.
3693 */
3694 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003695 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003696 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreaufb7508a2012-05-21 16:47:54 +02003697 setsockopt(si_fd(&s->si[0]), IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003698#endif
3699 }
Willy Tarreau03945942009-12-22 16:50:27 +01003700
Willy Tarreau59234e92008-11-30 23:51:27 +01003701 /*************************************************************
3702 * OK, that's finished for the headers. We have done what we *
3703 * could. Let's switch to the DATA state. *
3704 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003705 req->analyse_exp = TICK_ETERNITY;
3706 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003707
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003708 /* if the server closes the connection, we want to immediately react
3709 * and close the socket to save packets and syscalls.
3710 */
3711 req->cons->flags |= SI_FL_NOHALF;
3712
Willy Tarreau59234e92008-11-30 23:51:27 +01003713 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003714 /* OK let's go on with the BODY now */
3715 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003716
Willy Tarreau59234e92008-11-30 23:51:27 +01003717 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003718 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003719 /* we detected a parsing error. We want to archive this request
3720 * in the dedicated proxy area for later troubleshooting.
3721 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003722 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003723 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003724
Willy Tarreau59234e92008-11-30 23:51:27 +01003725 txn->req.msg_state = HTTP_MSG_ERROR;
3726 txn->status = 400;
3727 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003728 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003729
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003730 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003731 if (s->listener->counters)
3732 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003733
Willy Tarreau59234e92008-11-30 23:51:27 +01003734 if (!(s->flags & SN_ERR_MASK))
3735 s->flags |= SN_ERR_PRXCOND;
3736 if (!(s->flags & SN_FINST_MASK))
3737 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003738 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003739}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003740
Willy Tarreau60b85b02008-11-30 23:28:40 +01003741/* This function is an analyser which processes the HTTP tarpit. It always
3742 * returns zero, at the beginning because it prevents any other processing
3743 * from occurring, and at the end because it terminates the request.
3744 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003745int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003746{
3747 struct http_txn *txn = &s->txn;
3748
3749 /* This connection is being tarpitted. The CLIENT side has
3750 * already set the connect expiration date to the right
3751 * timeout. We just have to check that the client is still
3752 * there and that the timeout has not expired.
3753 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003754 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003755 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003756 !tick_is_expired(req->analyse_exp, now_ms))
3757 return 0;
3758
3759 /* We will set the queue timer to the time spent, just for
3760 * logging purposes. We fake a 500 server error, so that the
3761 * attacker will not suspect his connection has been tarpitted.
3762 * It will not cause trouble to the logs because we can exclude
3763 * the tarpitted connections by filtering on the 'PT' status flags.
3764 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003765 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3766
3767 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003768 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003769 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003770
3771 req->analysers = 0;
3772 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003773
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003774 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003775 if (s->listener->counters)
3776 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003777
Willy Tarreau60b85b02008-11-30 23:28:40 +01003778 if (!(s->flags & SN_ERR_MASK))
3779 s->flags |= SN_ERR_PRXCOND;
3780 if (!(s->flags & SN_FINST_MASK))
3781 s->flags |= SN_FINST_T;
3782 return 0;
3783}
3784
Willy Tarreaud34af782008-11-30 23:36:37 +01003785/* This function is an analyser which processes the HTTP request body. It looks
3786 * for parameters to be used for the load balancing algorithm (url_param). It
3787 * must only be called after the standard HTTP request processing has occurred,
3788 * because it expects the request to be parsed. It returns zero if it needs to
3789 * read more data, or 1 once it has completed its analysis.
3790 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003791int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003792{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003793 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003794 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003795 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003796
3797 /* We have to parse the HTTP request body to find any required data.
3798 * "balance url_param check_post" should have been the only way to get
3799 * into this. We were brought here after HTTP header analysis, so all
3800 * related structures are ready.
3801 */
3802
Willy Tarreau522d6c02009-12-06 18:49:18 +01003803 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3804 goto missing_data;
3805
3806 if (msg->msg_state < HTTP_MSG_100_SENT) {
3807 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3808 * send an HTTP/1.1 100 Continue intermediate response.
3809 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003810 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003811 struct hdr_ctx ctx;
3812 ctx.idx = 0;
3813 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003814 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003815 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003816 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003817 }
3818 }
3819 msg->msg_state = HTTP_MSG_100_SENT;
3820 }
3821
3822 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003823 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02003824 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003825 * is still null. We must save the body in msg->next because it
3826 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003827 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003828 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003829
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003830 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003831 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3832 else
3833 msg->msg_state = HTTP_MSG_DATA;
3834 }
3835
3836 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003837 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003838 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003839 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003840 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003841 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003842
Willy Tarreau115acb92009-12-26 13:56:06 +01003843 if (!ret)
3844 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003845 else if (ret < 0) {
3846 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003847 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003848 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003849 }
3850
Willy Tarreaud98cf932009-12-27 22:54:55 +01003851 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003852 * We have the first data byte is in msg->sov. We're waiting for at
3853 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01003854 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003855
Willy Tarreau124d9912011-03-01 20:30:48 +01003856 if (msg->body_len < limit)
3857 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003858
Willy Tarreau9b28e032012-10-12 23:49:43 +02003859 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01003860 goto http_end;
3861
3862 missing_data:
3863 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003864 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02003865 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01003866 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003867 }
Willy Tarreau115acb92009-12-26 13:56:06 +01003868
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003869 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003870 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02003871 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02003872
3873 if (!(s->flags & SN_ERR_MASK))
3874 s->flags |= SN_ERR_CLITO;
3875 if (!(s->flags & SN_FINST_MASK))
3876 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003877 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01003878 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003879
3880 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003881 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01003882 /* Not enough data. We'll re-use the http-request
3883 * timeout here. Ideally, we should set the timeout
3884 * relative to the accept() date. We just set the
3885 * request timeout once at the beginning of the
3886 * request.
3887 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003888 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01003889 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02003890 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01003891 return 0;
3892 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003893
3894 http_end:
3895 /* The situation will not evolve, so let's give up on the analysis. */
3896 s->logs.tv_request = now; /* update the request timer to reflect full request */
3897 req->analysers &= ~an_bit;
3898 req->analyse_exp = TICK_ETERNITY;
3899 return 1;
3900
3901 return_bad_req: /* let's centralize all bad requests */
3902 txn->req.msg_state = HTTP_MSG_ERROR;
3903 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003904 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01003905
Willy Tarreau79ebac62010-06-07 13:47:49 +02003906 if (!(s->flags & SN_ERR_MASK))
3907 s->flags |= SN_ERR_PRXCOND;
3908 if (!(s->flags & SN_FINST_MASK))
3909 s->flags |= SN_FINST_R;
3910
Willy Tarreau522d6c02009-12-06 18:49:18 +01003911 return_err_msg:
3912 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003913 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003914 if (s->listener->counters)
3915 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01003916 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01003917}
3918
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003919/* send a server's name with an outgoing request over an established connection.
3920 * Note: this function is designed to be called once the request has been scheduled
3921 * for being forwarded. This is the reason why it rewinds the buffer before
3922 * proceeding.
3923 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01003924int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05003925
3926 struct hdr_ctx ctx;
3927
Mark Lamourinec2247f02012-01-04 13:02:01 -05003928 char *hdr_name = be->server_id_hdr_name;
3929 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02003930 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003931 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003932 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003933
William Lallemandd9e90662012-01-30 17:27:17 +01003934 ctx.idx = 0;
3935
Willy Tarreau9b28e032012-10-12 23:49:43 +02003936 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003937 if (old_o) {
3938 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003939 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003940 }
3941
Willy Tarreau9b28e032012-10-12 23:49:43 +02003942 old_i = chn->buf->i;
3943 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 -05003944 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003945 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003946 }
3947
3948 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003949 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05003950 memcpy(hdr_val, hdr_name, hdr_name_len);
3951 hdr_val += hdr_name_len;
3952 *hdr_val++ = ':';
3953 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003954 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
3955 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05003956
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003957 if (old_o) {
3958 /* If this was a forwarded request, we must readjust the amount of
3959 * data to be forwarded in order to take into account the size
3960 * variations.
3961 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003962 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02003963 }
3964
Mark Lamourinec2247f02012-01-04 13:02:01 -05003965 return 0;
3966}
3967
Willy Tarreau610ecce2010-01-04 21:15:02 +01003968/* Terminate current transaction and prepare a new one. This is very tricky
3969 * right now but it works.
3970 */
3971void http_end_txn_clean_session(struct session *s)
3972{
3973 /* FIXME: We need a more portable way of releasing a backend's and a
3974 * server's connections. We need a safer way to reinitialize buffer
3975 * flags. We also need a more accurate method for computing per-request
3976 * data.
3977 */
3978 http_silent_debug(__LINE__, s);
3979
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003980 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02003981 si_shutr(s->req->cons);
3982 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003983
3984 http_silent_debug(__LINE__, s);
3985
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003986 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01003987 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01003988 if (unlikely(s->srv_conn))
3989 sess_change_server(s, NULL);
3990 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01003991
3992 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
3993 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02003994 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01003995
3996 if (s->txn.status) {
3997 int n;
3998
3999 n = s->txn.status / 100;
4000 if (n < 1 || n > 5)
4001 n = 0;
4002
4003 if (s->fe->mode == PR_MODE_HTTP)
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004004 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004005
Willy Tarreau24657792010-02-26 10:30:28 +01004006 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004007 (s->be->mode == PR_MODE_HTTP))
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004008 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004009 }
4010
4011 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004012 s->logs.bytes_in -= s->req->buf->i;
4013 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004014
4015 /* let's do a final log if we need it */
4016 if (s->logs.logwait &&
4017 !(s->flags & SN_MONITOR) &&
4018 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4019 s->do_log(s);
4020 }
4021
4022 s->logs.accept_date = date; /* user-visible date for logging */
4023 s->logs.tv_accept = now; /* corrected date for internal use */
4024 tv_zero(&s->logs.tv_request);
4025 s->logs.t_queue = -1;
4026 s->logs.t_connect = -1;
4027 s->logs.t_data = -1;
4028 s->logs.t_close = 0;
4029 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4030 s->logs.srv_queue_size = 0; /* we will get this number soon */
4031
Willy Tarreau9b28e032012-10-12 23:49:43 +02004032 s->logs.bytes_in = s->req->total = s->req->buf->i;
4033 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004034
4035 if (s->pend_pos)
4036 pendconn_free(s->pend_pos);
4037
Willy Tarreau827aee92011-03-10 16:55:02 +01004038 if (target_srv(&s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004039 if (s->flags & SN_CURR_SESS) {
4040 s->flags &= ~SN_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01004041 target_srv(&s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004042 }
Willy Tarreau827aee92011-03-10 16:55:02 +01004043 if (may_dequeue_tasks(target_srv(&s->target), s->be))
4044 process_srv_queue(target_srv(&s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004045 }
4046
Willy Tarreau9e000c62011-03-10 14:03:36 +01004047 clear_target(&s->target);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004048
4049 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004050 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4051 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004052 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004053 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004054 s->req->cons->err_loc = NULL;
4055 s->req->cons->exp = TICK_ETERNITY;
4056 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004057 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4058 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 +02004059 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 +01004060 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
4061 s->txn.meth = 0;
4062 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004063 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004064 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004065 s->req->cons->flags |= SI_FL_INDEP_STR;
4066
Willy Tarreau96e31212011-05-30 18:10:30 +02004067 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004068 s->req->flags |= CF_NEVER_WAIT;
4069 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004070 }
4071
Willy Tarreau610ecce2010-01-04 21:15:02 +01004072 /* if the request buffer is not empty, it means we're
4073 * about to process another request, so send pending
4074 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004075 * Just don't do this if the buffer is close to be full,
4076 * because the request will wait for it to flush a little
4077 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004078 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004079 if (s->req->buf->i) {
4080 if (s->rep->buf->o &&
4081 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4082 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004083 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004084 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004085
4086 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004087 channel_auto_read(s->req);
4088 channel_auto_close(s->req);
4089 channel_auto_read(s->rep);
4090 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004091
Willy Tarreau342b11c2010-11-24 16:22:09 +01004092 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004093 s->rep->analysers = 0;
4094
4095 http_silent_debug(__LINE__, s);
4096}
4097
4098
4099/* This function updates the request state machine according to the response
4100 * state machine and buffer flags. It returns 1 if it changes anything (flag
4101 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4102 * it is only used to find when a request/response couple is complete. Both
4103 * this function and its equivalent should loop until both return zero. It
4104 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4105 */
4106int http_sync_req_state(struct session *s)
4107{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004108 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004109 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004110 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004111 unsigned int old_state = txn->req.msg_state;
4112
4113 http_silent_debug(__LINE__, s);
4114 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4115 return 0;
4116
4117 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004118 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004119 * We can shut the read side unless we want to abort_on_close,
4120 * or we have a POST request. The issue with POST requests is
4121 * that some browsers still send a CRLF after the request, and
4122 * this CRLF must be read so that it does not remain in the kernel
4123 * buffers, otherwise a close could cause an RST on some systems
4124 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004125 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004126 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004127 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004128
4129 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4130 goto wait_other_side;
4131
4132 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4133 /* The server has not finished to respond, so we
4134 * don't want to move in order not to upset it.
4135 */
4136 goto wait_other_side;
4137 }
4138
4139 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4140 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004141 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004142 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004143 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004144 goto wait_other_side;
4145 }
4146
4147 /* When we get here, it means that both the request and the
4148 * response have finished receiving. Depending on the connection
4149 * mode, we'll have to wait for the last bytes to leave in either
4150 * direction, and sometimes for a close to be effective.
4151 */
4152
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004153 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4154 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004155 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4156 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004157 }
4158 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4159 /* Option forceclose is set, or either side wants to close,
4160 * let's enforce it now that we're not expecting any new
4161 * data to come. The caller knows the session is complete
4162 * once both states are CLOSED.
4163 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004164 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4165 channel_shutr_now(chn);
4166 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004167 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004168 }
4169 else {
4170 /* The last possible modes are keep-alive and tunnel. Since tunnel
4171 * mode does not set the body analyser, we can't reach this place
4172 * in tunnel mode, so we're left with keep-alive only.
4173 * This mode is currently not implemented, we switch to tunnel mode.
4174 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004175 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004176 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004177 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004178 }
4179
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004180 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004181 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004182 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004183
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004184 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004185 txn->req.msg_state = HTTP_MSG_CLOSING;
4186 goto http_msg_closing;
4187 }
4188 else {
4189 txn->req.msg_state = HTTP_MSG_CLOSED;
4190 goto http_msg_closed;
4191 }
4192 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004193 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194 }
4195
4196 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4197 http_msg_closing:
4198 /* nothing else to forward, just waiting for the output buffer
4199 * to be empty and for the shutw_now to take effect.
4200 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004201 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004202 txn->req.msg_state = HTTP_MSG_CLOSED;
4203 goto http_msg_closed;
4204 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004205 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004206 txn->req.msg_state = HTTP_MSG_ERROR;
4207 goto wait_other_side;
4208 }
4209 }
4210
4211 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4212 http_msg_closed:
4213 goto wait_other_side;
4214 }
4215
4216 wait_other_side:
4217 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004218 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004219}
4220
4221
4222/* This function updates the response state machine according to the request
4223 * state machine and buffer flags. It returns 1 if it changes anything (flag
4224 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4225 * it is only used to find when a request/response couple is complete. Both
4226 * this function and its equivalent should loop until both return zero. It
4227 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4228 */
4229int http_sync_res_state(struct session *s)
4230{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004231 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004232 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004233 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004234 unsigned int old_state = txn->rsp.msg_state;
4235
4236 http_silent_debug(__LINE__, s);
4237 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4238 return 0;
4239
4240 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4241 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004242 * still monitor the server connection for a possible close
4243 * while the request is being uploaded, so we don't disable
4244 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004245 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004246 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004247
4248 if (txn->req.msg_state == HTTP_MSG_ERROR)
4249 goto wait_other_side;
4250
4251 if (txn->req.msg_state < HTTP_MSG_DONE) {
4252 /* The client seems to still be sending data, probably
4253 * because we got an error response during an upload.
4254 * We have the choice of either breaking the connection
4255 * or letting it pass through. Let's do the later.
4256 */
4257 goto wait_other_side;
4258 }
4259
4260 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4261 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004262 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004263 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004264 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004265 goto wait_other_side;
4266 }
4267
4268 /* When we get here, it means that both the request and the
4269 * response have finished receiving. Depending on the connection
4270 * mode, we'll have to wait for the last bytes to leave in either
4271 * direction, and sometimes for a close to be effective.
4272 */
4273
4274 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4275 /* Server-close mode : shut read and wait for the request
4276 * side to close its output buffer. The caller will detect
4277 * when we're in DONE and the other is in CLOSED and will
4278 * catch that for the final cleanup.
4279 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004280 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4281 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004282 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004283 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4284 /* Option forceclose is set, or either side wants to close,
4285 * let's enforce it now that we're not expecting any new
4286 * data to come. The caller knows the session is complete
4287 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004288 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004289 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4290 channel_shutr_now(chn);
4291 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004292 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 }
4294 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004295 /* The last possible modes are keep-alive and tunnel. Since tunnel
4296 * mode does not set the body analyser, we can't reach this place
4297 * in tunnel mode, so we're left with keep-alive only.
4298 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004300 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004301 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004302 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004303 }
4304
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004305 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004306 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004307 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004308 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4309 goto http_msg_closing;
4310 }
4311 else {
4312 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4313 goto http_msg_closed;
4314 }
4315 }
4316 goto wait_other_side;
4317 }
4318
4319 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4320 http_msg_closing:
4321 /* nothing else to forward, just waiting for the output buffer
4322 * to be empty and for the shutw_now to take effect.
4323 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004324 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004325 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4326 goto http_msg_closed;
4327 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004328 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004329 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004330 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004331 if (target_srv(&s->target))
4332 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004333 goto wait_other_side;
4334 }
4335 }
4336
4337 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4338 http_msg_closed:
4339 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004340 bi_erase(chn);
4341 channel_auto_close(chn);
4342 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004343 goto wait_other_side;
4344 }
4345
4346 wait_other_side:
4347 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004348 /* We force the response to leave immediately if we're waiting for the
4349 * other side, since there is no pending shutdown to push it out.
4350 */
4351 if (!channel_is_empty(chn))
4352 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004353 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004354}
4355
4356
4357/* Resync the request and response state machines. Return 1 if either state
4358 * changes.
4359 */
4360int http_resync_states(struct session *s)
4361{
4362 struct http_txn *txn = &s->txn;
4363 int old_req_state = txn->req.msg_state;
4364 int old_res_state = txn->rsp.msg_state;
4365
4366 http_silent_debug(__LINE__, s);
4367 http_sync_req_state(s);
4368 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004369 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004370 if (!http_sync_res_state(s))
4371 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004372 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004373 if (!http_sync_req_state(s))
4374 break;
4375 }
4376 http_silent_debug(__LINE__, s);
4377 /* OK, both state machines agree on a compatible state.
4378 * There are a few cases we're interested in :
4379 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4380 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4381 * directions, so let's simply disable both analysers.
4382 * - HTTP_MSG_CLOSED on the response only means we must abort the
4383 * request.
4384 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4385 * with server-close mode means we've completed one request and we
4386 * must re-initialize the server connection.
4387 */
4388
4389 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4390 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4391 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4392 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4393 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004394 channel_auto_close(s->req);
4395 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004396 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004397 channel_auto_close(s->rep);
4398 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004399 }
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004400 else if (txn->rsp.msg_state == HTTP_MSG_CLOSED ||
4401 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau4fe41902010-06-07 22:27:41 +02004402 txn->req.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004403 (s->rep->flags & CF_SHUTW)) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004404 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004405 channel_auto_close(s->rep);
4406 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004407 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004408 channel_abort(s->req);
4409 channel_auto_close(s->req);
4410 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004411 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004412 }
4413 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4414 txn->rsp.msg_state == HTTP_MSG_DONE &&
4415 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4416 /* server-close: terminate this server connection and
4417 * reinitialize a fresh-new transaction.
4418 */
4419 http_end_txn_clean_session(s);
4420 }
4421
4422 http_silent_debug(__LINE__, s);
4423 return txn->req.msg_state != old_req_state ||
4424 txn->rsp.msg_state != old_res_state;
4425}
4426
Willy Tarreaud98cf932009-12-27 22:54:55 +01004427/* This function is an analyser which forwards request body (including chunk
4428 * sizes if any). It is called as soon as we must forward, even if we forward
4429 * zero byte. The only situation where it must not be called is when we're in
4430 * tunnel mode and we want to forward till the close. It's used both to forward
4431 * remaining data and to resync after end of body. It expects the msg_state to
4432 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4433 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004434 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004435 * bytes of pending data + the headers if not already done (between sol and sov).
4436 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004437 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004438int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004439{
4440 struct http_txn *txn = &s->txn;
4441 struct http_msg *msg = &s->txn.req;
4442
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004443 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4444 return 0;
4445
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004446 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004447 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004448 /* Output closed while we were sending data. We must abort and
4449 * wake the other side up.
4450 */
4451 msg->msg_state = HTTP_MSG_ERROR;
4452 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004453 return 1;
4454 }
4455
Willy Tarreau4fe41902010-06-07 22:27:41 +02004456 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004457 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004458
4459 /* Note that we don't have to send 100-continue back because we don't
4460 * need the data to complete our job, and it's up to the server to
4461 * decide whether to return 100, 417 or anything else in return of
4462 * an "Expect: 100-continue" header.
4463 */
4464
4465 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004466 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004467 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004468 * is still null. We must save the body in msg->next because it
4469 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004470 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004471 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004472
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004473 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004474 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004475 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004476 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004477 }
4478
Willy Tarreaud98cf932009-12-27 22:54:55 +01004479 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004480 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004481
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004483 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004484 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004485 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004486 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004487 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004488 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004489 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004490 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004491
Willy Tarreaucaabe412010-01-03 23:08:28 +01004492 if (msg->msg_state == HTTP_MSG_DATA) {
4493 /* must still forward */
4494 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004495 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004496
4497 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004498 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004499 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004500 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004501 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004502 }
4503 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004504 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004505 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004506 * TRAILERS state.
4507 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004508 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004509
Willy Tarreau54d23df2012-10-25 19:04:45 +02004510 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004511 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004512 else if (ret < 0) {
4513 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004514 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004515 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004516 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004517 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004518 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004519 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004520 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004521 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004522 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004523
4524 if (ret == 0)
4525 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004526 else if (ret < 0) {
4527 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004528 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004529 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004530 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004531 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004532 /* we're in MSG_CHUNK_SIZE now */
4533 }
4534 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004535 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004536
4537 if (ret == 0)
4538 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004539 else if (ret < 0) {
4540 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004541 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004542 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004543 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004544 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004545 /* we're in HTTP_MSG_DONE now */
4546 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004547 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004548 int old_state = msg->msg_state;
4549
Willy Tarreau610ecce2010-01-04 21:15:02 +01004550 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004551 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004552 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4553 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004554 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004555 if (http_resync_states(s)) {
4556 /* some state changes occurred, maybe the analyser
4557 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004558 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004559 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004560 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004561 /* request errors are most likely due to
4562 * the server aborting the transfer.
4563 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004564 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004565 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004566 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004567 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004568 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004569 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004570 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004571 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004572
4573 /* If "option abortonclose" is set on the backend, we
4574 * want to monitor the client's connection and forward
4575 * any shutdown notification to the server, which will
4576 * decide whether to close or to go on processing the
4577 * request.
4578 */
4579 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004580 channel_auto_read(req);
4581 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004582 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004583 else if (s->txn.meth == HTTP_METH_POST) {
4584 /* POST requests may require to read extra CRLF
4585 * sent by broken browsers and which could cause
4586 * an RST to be sent upon close on some systems
4587 * (eg: Linux).
4588 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004589 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004590 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004591
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004593 }
4594 }
4595
Willy Tarreaud98cf932009-12-27 22:54:55 +01004596 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004597 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004598 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004599 if (!(s->flags & SN_ERR_MASK))
4600 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004601 if (!(s->flags & SN_FINST_MASK)) {
4602 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4603 s->flags |= SN_FINST_H;
4604 else
4605 s->flags |= SN_FINST_D;
4606 }
4607
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004608 s->fe->fe_counters.cli_aborts++;
4609 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004610 if (target_srv(&s->target))
4611 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004612
4613 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004614 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004615
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004616 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004617 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004618 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004619
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004620 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004621 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004622 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004623 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004624 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004625
Willy Tarreau5c620922011-05-11 19:56:11 +02004626 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004627 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004628 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004629 * modes are already handled by the stream sock layer. We must not do
4630 * this in content-length mode because it could present the MSG_MORE
4631 * flag with the last block of forwarded data, which would cause an
4632 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004633 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004634 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004635 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004636
Willy Tarreau610ecce2010-01-04 21:15:02 +01004637 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004638 return 0;
4639
Willy Tarreaud98cf932009-12-27 22:54:55 +01004640 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004641 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004642 if (s->listener->counters)
4643 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004644 return_bad_req_stats_ok:
4645 txn->req.msg_state = HTTP_MSG_ERROR;
4646 if (txn->status) {
4647 /* Note: we don't send any error if some data were already sent */
4648 stream_int_retnclose(req->prod, NULL);
4649 } else {
4650 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004651 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004652 }
4653 req->analysers = 0;
4654 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004655
4656 if (!(s->flags & SN_ERR_MASK))
4657 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004658 if (!(s->flags & SN_FINST_MASK)) {
4659 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4660 s->flags |= SN_FINST_H;
4661 else
4662 s->flags |= SN_FINST_D;
4663 }
4664 return 0;
4665
4666 aborted_xfer:
4667 txn->req.msg_state = HTTP_MSG_ERROR;
4668 if (txn->status) {
4669 /* Note: we don't send any error if some data were already sent */
4670 stream_int_retnclose(req->prod, NULL);
4671 } else {
4672 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004673 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004674 }
4675 req->analysers = 0;
4676 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4677
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004678 s->fe->fe_counters.srv_aborts++;
4679 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004680 if (target_srv(&s->target))
4681 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004682
4683 if (!(s->flags & SN_ERR_MASK))
4684 s->flags |= SN_ERR_SRVCL;
4685 if (!(s->flags & SN_FINST_MASK)) {
4686 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4687 s->flags |= SN_FINST_H;
4688 else
4689 s->flags |= SN_FINST_D;
4690 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004691 return 0;
4692}
4693
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004694/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4695 * processing can continue on next analysers, or zero if it either needs more
4696 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4697 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4698 * when it has nothing left to do, and may remove any analyser when it wants to
4699 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004700 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004701int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004702{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004703 struct http_txn *txn = &s->txn;
4704 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004705 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004706 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004707 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004708 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004709
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004710 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 +02004711 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004712 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004713 rep,
4714 rep->rex, rep->wex,
4715 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004716 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004717 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004718
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004719 /*
4720 * Now parse the partial (or complete) lines.
4721 * We will check the response syntax, and also join multi-line
4722 * headers. An index of all the lines will be elaborated while
4723 * parsing.
4724 *
4725 * For the parsing, we use a 28 states FSM.
4726 *
4727 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004728 * rep->buf->p = beginning of response
4729 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4730 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004731 * msg->eol = end of current header or line (LF or CRLF)
4732 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004733 */
4734
Willy Tarreau83e3af02009-12-28 17:39:57 +01004735 /* There's a protected area at the end of the buffer for rewriting
4736 * purposes. We don't want to start to parse the request if the
4737 * protected area is affected, because we may have to move processed
4738 * data later, which is much more complicated.
4739 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004740 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004741 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004742 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4743 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4744 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004745 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004746 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004747 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004748 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004749 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004750 return 0;
4751 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004752 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4753 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004754 }
4755
Willy Tarreau9b28e032012-10-12 23:49:43 +02004756 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004757 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004758 }
4759
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004760 /* 1: we might have to print this header in debug mode */
4761 if (unlikely((global.mode & MODE_DEBUG) &&
4762 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004763 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004764 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004765
Willy Tarreau9b28e032012-10-12 23:49:43 +02004766 sol = rep->buf->p;
4767 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004768 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004769
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004770 sol += hdr_idx_first_pos(&txn->hdr_idx);
4771 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004772
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004773 while (cur_idx) {
4774 eol = sol + txn->hdr_idx.v[cur_idx].len;
4775 debug_hdr("srvhdr", s, sol, eol);
4776 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4777 cur_idx = txn->hdr_idx.v[cur_idx].next;
4778 }
4779 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004780
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004781 /*
4782 * Now we quickly check if we have found a full valid response.
4783 * If not so, we check the FD and buffer states before leaving.
4784 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004785 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004786 * responses are checked first.
4787 *
4788 * Depending on whether the client is still there or not, we
4789 * may send an error response back or not. Note that normally
4790 * we should only check for HTTP status there, and check I/O
4791 * errors somewhere else.
4792 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004793
Willy Tarreau655dce92009-11-08 13:10:58 +01004794 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004795 /* Invalid response */
4796 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4797 /* we detected a parsing error. We want to archive this response
4798 * in the dedicated proxy area for later troubleshooting.
4799 */
4800 hdr_response_bad:
4801 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004802 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004803
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004804 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004805 if (target_srv(&s->target)) {
4806 target_srv(&s->target)->counters.failed_resp++;
4807 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004808 }
Willy Tarreau64648412010-03-05 10:41:54 +01004809 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004810 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004811 rep->analysers = 0;
4812 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004813 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004814 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004815 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004816
4817 if (!(s->flags & SN_ERR_MASK))
4818 s->flags |= SN_ERR_PRXCOND;
4819 if (!(s->flags & SN_FINST_MASK))
4820 s->flags |= SN_FINST_H;
4821
4822 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004823 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004824
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004825 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004826 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004827 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02004828 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004829 goto hdr_response_bad;
4830 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004831
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004832 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004833 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004834 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004835 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02004836
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004837 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004838 if (target_srv(&s->target)) {
4839 target_srv(&s->target)->counters.failed_resp++;
4840 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004841 }
Willy Tarreau461f6622008-08-15 23:43:19 +02004842
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004843 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004844 rep->analysers = 0;
4845 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004846 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004847 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004848 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02004849
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004850 if (!(s->flags & SN_ERR_MASK))
4851 s->flags |= SN_ERR_SRVCL;
4852 if (!(s->flags & SN_FINST_MASK))
4853 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02004854 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004855 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004856
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004857 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004858 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004859 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004860 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004861
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004862 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004863 if (target_srv(&s->target)) {
4864 target_srv(&s->target)->counters.failed_resp++;
4865 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004866 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004867
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004868 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004869 rep->analysers = 0;
4870 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004871 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004872 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004873 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02004874
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004875 if (!(s->flags & SN_ERR_MASK))
4876 s->flags |= SN_ERR_SRVTO;
4877 if (!(s->flags & SN_FINST_MASK))
4878 s->flags |= SN_FINST_H;
4879 return 0;
4880 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02004881
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004882 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004883 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02004884 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004885 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01004886
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004887 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01004888 if (target_srv(&s->target)) {
4889 target_srv(&s->target)->counters.failed_resp++;
4890 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004891 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004892
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004893 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004894 rep->analysers = 0;
4895 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004896 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004897 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004898 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01004899
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004900 if (!(s->flags & SN_ERR_MASK))
4901 s->flags |= SN_ERR_SRVCL;
4902 if (!(s->flags & SN_FINST_MASK))
4903 s->flags |= SN_FINST_H;
4904 return 0;
4905 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004906
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004907 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004908 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004909 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004910 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004911
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004912 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004913 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004914 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004915
4916 if (!(s->flags & SN_ERR_MASK))
4917 s->flags |= SN_ERR_CLICL;
4918 if (!(s->flags & SN_FINST_MASK))
4919 s->flags |= SN_FINST_H;
4920
4921 /* process_session() will take care of the error */
4922 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004923 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01004924
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004925 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004926 return 0;
4927 }
4928
4929 /* More interesting part now : we know that we have a complete
4930 * response which at least looks like HTTP. We have an indicator
4931 * of each header's length, so we can parse them quickly.
4932 */
4933
4934 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004935 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004936
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004937 /*
4938 * 1: get the status code
4939 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004940 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004941 if (n < 1 || n > 5)
4942 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004943 /* when the client triggers a 4xx from the server, it's most often due
4944 * to a missing object or permission. These events should be tracked
4945 * because if they happen often, it may indicate a brute force or a
4946 * vulnerability scan.
4947 */
4948 if (n == 4)
4949 session_inc_http_err_ctr(s);
4950
Willy Tarreau827aee92011-03-10 16:55:02 +01004951 if (target_srv(&s->target))
4952 target_srv(&s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004953
Willy Tarreau5b154472009-12-21 20:11:07 +01004954 /* check if the response is HTTP/1.1 or above */
4955 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02004956 ((rep->buf->p[5] > '1') ||
4957 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004958 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01004959
4960 /* "connection" has not been parsed yet */
Willy Tarreau60466522010-01-18 19:08:45 +01004961 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 +01004962
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004963 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004964 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004965
Willy Tarreau9b28e032012-10-12 23:49:43 +02004966 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004967
Willy Tarreau39650402010-03-15 19:44:39 +01004968 /* Adjust server's health based on status code. Note: status codes 501
4969 * and 505 are triggered on demand by client request, so we must not
4970 * count them as server failures.
4971 */
Willy Tarreau827aee92011-03-10 16:55:02 +01004972 if (target_srv(&s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004973 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau827aee92011-03-10 16:55:02 +01004974 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004975 else
Willy Tarreau827aee92011-03-10 16:55:02 +01004976 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02004977 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004978
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004979 /*
4980 * 2: check for cacheability.
4981 */
4982
4983 switch (txn->status) {
4984 case 200:
4985 case 203:
4986 case 206:
4987 case 300:
4988 case 301:
4989 case 410:
4990 /* RFC2616 @13.4:
4991 * "A response received with a status code of
4992 * 200, 203, 206, 300, 301 or 410 MAY be stored
4993 * by a cache (...) unless a cache-control
4994 * directive prohibits caching."
4995 *
4996 * RFC2616 @9.5: POST method :
4997 * "Responses to this method are not cacheable,
4998 * unless the response includes appropriate
4999 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005000 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005001 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005002 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005003 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5004 break;
5005 default:
5006 break;
5007 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005008
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005009 /*
5010 * 3: we may need to capture headers
5011 */
5012 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005013 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005014 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005015 txn->rsp.cap, s->fe->rsp_cap);
5016
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005017 /* 4: determine the transfer-length.
5018 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5019 * the presence of a message-body in a RESPONSE and its transfer length
5020 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005021 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005022 * All responses to the HEAD request method MUST NOT include a
5023 * message-body, even though the presence of entity-header fields
5024 * might lead one to believe they do. All 1xx (informational), 204
5025 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5026 * message-body. All other responses do include a message-body,
5027 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005028 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005029 * 1. Any response which "MUST NOT" include a message-body (such as the
5030 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5031 * always terminated by the first empty line after the header fields,
5032 * regardless of the entity-header fields present in the message.
5033 *
5034 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5035 * the "chunked" transfer-coding (Section 6.2) is used, the
5036 * transfer-length is defined by the use of this transfer-coding.
5037 * If a Transfer-Encoding header field is present and the "chunked"
5038 * transfer-coding is not present, the transfer-length is defined by
5039 * the sender closing the connection.
5040 *
5041 * 3. If a Content-Length header field is present, its decimal value in
5042 * OCTETs represents both the entity-length and the transfer-length.
5043 * If a message is received with both a Transfer-Encoding header
5044 * field and a Content-Length header field, the latter MUST be ignored.
5045 *
5046 * 4. If the message uses the media type "multipart/byteranges", and
5047 * the transfer-length is not otherwise specified, then this self-
5048 * delimiting media type defines the transfer-length. This media
5049 * type MUST NOT be used unless the sender knows that the recipient
5050 * can parse it; the presence in a request of a Range header with
5051 * multiple byte-range specifiers from a 1.1 client implies that the
5052 * client can parse multipart/byteranges responses.
5053 *
5054 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005055 */
5056
5057 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005058 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005059 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005060 * FIXME: should we parse anyway and return an error on chunked encoding ?
5061 */
5062 if (txn->meth == HTTP_METH_HEAD ||
5063 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005064 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005065 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005066 goto skip_content_length;
5067 }
5068
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005069 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005070 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005071 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005072 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005073 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005074 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5075 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005076 /* bad transfer-encoding (chunked followed by something else) */
5077 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005078 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005079 break;
5080 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005081 }
5082
5083 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5084 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005085 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005086 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005087 signed long long cl;
5088
Willy Tarreauad14f752011-09-02 20:33:27 +02005089 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005090 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005091 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005092 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005093
Willy Tarreauad14f752011-09-02 20:33:27 +02005094 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005095 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005096 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005097 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005098
Willy Tarreauad14f752011-09-02 20:33:27 +02005099 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005100 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005101 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005102 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005103
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005104 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005105 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005106 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005107 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005108
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005109 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005110 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005111 }
5112
William Lallemand82fe75c2012-10-23 10:25:10 +02005113 if (s->fe->comp || s->be->comp)
5114 select_compression_response_header(s, rep->buf);
5115
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005116 /* FIXME: we should also implement the multipart/byterange method.
5117 * For now on, we resort to close mode in this case (unknown length).
5118 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005119skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005120
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005121 /* end of job, return OK */
5122 rep->analysers &= ~an_bit;
5123 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005124 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005125 return 1;
5126}
5127
5128/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005129 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5130 * and updates t->rep->analysers. It might make sense to explode it into several
5131 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005132 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005133int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005134{
5135 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005136 struct http_msg *msg = &txn->rsp;
5137 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005138 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005139
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005140 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 +02005141 now_ms, __FUNCTION__,
5142 t,
5143 rep,
5144 rep->rex, rep->wex,
5145 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005146 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005147 rep->analysers);
5148
Willy Tarreau655dce92009-11-08 13:10:58 +01005149 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005150 return 0;
5151
5152 rep->analysers &= ~an_bit;
5153 rep->analyse_exp = TICK_ETERNITY;
5154
Willy Tarreau5b154472009-12-21 20:11:07 +01005155 /* Now we have to check if we need to modify the Connection header.
5156 * This is more difficult on the response than it is on the request,
5157 * because we can have two different HTTP versions and we don't know
5158 * how the client will interprete a response. For instance, let's say
5159 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5160 * HTTP/1.1 response without any header. Maybe it will bound itself to
5161 * HTTP/1.0 because it only knows about it, and will consider the lack
5162 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5163 * the lack of header as a keep-alive. Thus we will use two flags
5164 * indicating how a request MAY be understood by the client. In case
5165 * of multiple possibilities, we'll fix the header to be explicit. If
5166 * ambiguous cases such as both close and keepalive are seen, then we
5167 * will fall back to explicit close. Note that we won't take risks with
5168 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005169 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005170 */
5171
Willy Tarreaudc008c52010-02-01 16:20:08 +01005172 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5173 txn->status == 101)) {
5174 /* Either we've established an explicit tunnel, or we're
5175 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005176 * to understand the next protocols. We have to switch to tunnel
5177 * mode, so that we transfer the request and responses then let
5178 * this protocol pass unmodified. When we later implement specific
5179 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005180 * header which contains information about that protocol for
5181 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005182 */
5183 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5184 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005185 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5186 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5187 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005188 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005189
Willy Tarreau60466522010-01-18 19:08:45 +01005190 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005191 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005192 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5193 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005194
Willy Tarreau60466522010-01-18 19:08:45 +01005195 /* now adjust header transformations depending on current state */
5196 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5197 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5198 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005199 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005200 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005201 }
Willy Tarreau60466522010-01-18 19:08:45 +01005202 else { /* SCL / KAL */
5203 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005204 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005205 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005206 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005207
Willy Tarreau60466522010-01-18 19:08:45 +01005208 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005209 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005210
Willy Tarreau60466522010-01-18 19:08:45 +01005211 /* Some keep-alive responses are converted to Server-close if
5212 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005213 */
Willy Tarreau60466522010-01-18 19:08:45 +01005214 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5215 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005216 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005217 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005218 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005219 }
5220
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005221 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005222 /*
5223 * 3: we will have to evaluate the filters.
5224 * As opposed to version 1.2, now they will be evaluated in the
5225 * filters order and not in the header order. This means that
5226 * each filter has to be validated among all headers.
5227 *
5228 * Filters are tried with ->be first, then with ->fe if it is
5229 * different from ->be.
5230 */
5231
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005232 cur_proxy = t->be;
5233 while (1) {
5234 struct proxy *rule_set = cur_proxy;
5235
5236 /* try headers filters */
5237 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005238 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005239 return_bad_resp:
Willy Tarreau827aee92011-03-10 16:55:02 +01005240 if (target_srv(&t->target)) {
5241 target_srv(&t->target)->counters.failed_resp++;
5242 health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005243 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005244 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005245 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005246 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005247 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005248 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005249 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005250 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005251 if (!(t->flags & SN_ERR_MASK))
5252 t->flags |= SN_ERR_PRXCOND;
5253 if (!(t->flags & SN_FINST_MASK))
5254 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005255 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005256 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005257 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005258
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005259 /* has the response been denied ? */
5260 if (txn->flags & TX_SVDENY) {
Willy Tarreau827aee92011-03-10 16:55:02 +01005261 if (target_srv(&t->target))
5262 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005263
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005264 t->be->be_counters.denied_resp++;
5265 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005266 if (t->listener->counters)
5267 t->listener->counters->denied_resp++;
5268
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005269 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005270 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005271
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005272 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005273 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005274 if (txn->status < 200)
5275 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005276 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005277 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005278 ret = acl_pass(ret);
5279 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5280 ret = !ret;
5281 if (!ret)
5282 continue;
5283 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005284 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005285 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005286 }
5287
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005288 /* check whether we're already working on the frontend */
5289 if (cur_proxy == t->fe)
5290 break;
5291 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005292 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005293
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005294 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005295 * We may be facing a 100-continue response, in which case this
5296 * is not the right response, and we're waiting for the next one.
5297 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005298 * next one.
5299 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005300 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005301 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005302 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005303 msg->msg_state = HTTP_MSG_RPBEFORE;
5304 txn->status = 0;
5305 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5306 return 1;
5307 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005308 else if (unlikely(txn->status < 200))
5309 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005310
5311 /* we don't have any 1xx status code now */
5312
5313 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005314 * 4: check for server cookie.
5315 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005316 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5317 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005318 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005319
Willy Tarreaubaaee002006-06-26 02:48:02 +02005320
Willy Tarreaua15645d2007-03-18 16:22:39 +01005321 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005322 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005323 */
Willy Tarreau67402132012-05-31 20:40:20 +02005324 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005325 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005326
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005327 /*
5328 * 6: add server cookie in the response if needed
5329 */
Willy Tarreau67402132012-05-31 20:40:20 +02005330 if (target_srv(&t->target) && (t->be->ck_opts & PR_CK_INS) &&
5331 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005332 (!(t->flags & SN_DIRECT) ||
5333 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5334 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5335 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5336 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005337 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005338 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005339 /* the server is known, it's not the one the client requested, or the
5340 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005341 * insert a set-cookie here, except if we want to insert only on POST
5342 * requests and this one isn't. Note that servers which don't have cookies
5343 * (eg: some backup servers) will return a full cookie removal request.
5344 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005345 if (!target_srv(&t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005346 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005347 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5348 t->be->cookie_name);
5349 }
5350 else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005351 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005352
5353 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5354 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005355 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5356 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5357 trash.len += 5;
5358
Willy Tarreauef4f3912010-10-07 21:00:29 +02005359 if (t->be->cookie_maxlife) {
5360 /* emit first_date, which is either the original one or
5361 * the current date.
5362 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005363 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005364 s30tob64(txn->cookie_first_date ?
5365 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005366 (date.tv_sec+3) >> 2, trash.str + trash.len);
5367 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005368 }
5369 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005370 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005371 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005372
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005373 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005374 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005375
Willy Tarreau4992dd22012-05-31 21:02:17 +02005376 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005377 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005378
5379 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005380 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005381
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005382 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005383 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005384
Willy Tarreauf1348312010-10-07 15:54:11 +02005385 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau827aee92011-03-10 16:55:02 +01005386 if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005387 /* the server did not change, only the date was updated */
5388 txn->flags |= TX_SCK_UPDATED;
5389 else
5390 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005391
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005392 /* Here, we will tell an eventual cache on the client side that we don't
5393 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5394 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5395 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5396 */
Willy Tarreau67402132012-05-31 20:40:20 +02005397 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005398
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005399 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5400
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005401 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005402 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005403 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005404 }
5405 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005406
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005407 /*
5408 * 7: check if result will be cacheable with a cookie.
5409 * We'll block the response if security checks have caught
5410 * nasty things such as a cacheable cookie.
5411 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005412 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5413 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005414 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005415
5416 /* we're in presence of a cacheable response containing
5417 * a set-cookie header. We'll block it as requested by
5418 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005419 */
Willy Tarreau827aee92011-03-10 16:55:02 +01005420 if (target_srv(&t->target))
5421 target_srv(&t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005422
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005423 t->be->be_counters.denied_resp++;
5424 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005425 if (t->listener->counters)
5426 t->listener->counters->denied_resp++;
5427
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005428 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005429 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005430 send_log(t->be, LOG_ALERT,
5431 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau827aee92011-03-10 16:55:02 +01005432 t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005433 goto return_srv_prx_502;
5434 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005435
5436 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005437 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005438 */
Willy Tarreau60466522010-01-18 19:08:45 +01005439 if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5440 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) {
5441 unsigned int want_flags = 0;
5442
5443 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5444 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5445 /* we want a keep-alive response here. Keep-alive header
5446 * required if either side is not 1.1.
5447 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005448 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005449 want_flags |= TX_CON_KAL_SET;
5450 }
5451 else {
5452 /* we want a close response here. Close header required if
5453 * the server is 1.1, regardless of the client.
5454 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005455 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005456 want_flags |= TX_CON_CLO_SET;
5457 }
5458
5459 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005460 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005461 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005462
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005463 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005464 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005465 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005466 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005467
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005468 /*************************************************************
5469 * OK, that's finished for the headers. We have done what we *
5470 * could. Let's switch to the DATA state. *
5471 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005472
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005473 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005474
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005475 /* if the user wants to log as soon as possible, without counting
5476 * bytes from the server, then this is the right moment. We have
5477 * to temporarily assign bytes_out to log what we currently have.
5478 */
5479 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5480 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5481 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005482 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005483 t->logs.bytes_out = 0;
5484 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005485
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005486 /* Note: we must not try to cheat by jumping directly to DATA,
5487 * otherwise we would not let the client side wake up.
5488 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005489
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005490 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005491 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005492 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005493}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005494
Willy Tarreaud98cf932009-12-27 22:54:55 +01005495/* This function is an analyser which forwards response body (including chunk
5496 * sizes if any). It is called as soon as we must forward, even if we forward
5497 * zero byte. The only situation where it must not be called is when we're in
5498 * tunnel mode and we want to forward till the close. It's used both to forward
5499 * remaining data and to resync after end of body. It expects the msg_state to
5500 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5501 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005502 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005503 * bytes of pending data + the headers if not already done (between sol and sov).
5504 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005505 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005506int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005507{
5508 struct http_txn *txn = &s->txn;
5509 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005510 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005511 static struct buffer *tmpbuf = NULL;
5512 int compressing = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005513
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005514 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5515 return 0;
5516
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005517 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005518 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005519 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005520 /* Output closed while we were sending data. We must abort and
5521 * wake the other side up.
5522 */
5523 msg->msg_state = HTTP_MSG_ERROR;
5524 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005525 return 1;
5526 }
5527
Willy Tarreau4fe41902010-06-07 22:27:41 +02005528 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005529 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005530
William Lallemand82fe75c2012-10-23 10:25:10 +02005531 /* no data */
5532 if (res->buf->i == 0)
5533 return 0;
5534
5535 /* this is the first time we need the compression buffer */
5536 if (s->comp_algo != NULL && tmpbuf == NULL) {
5537 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5538 goto aborted_xfer; /* no memory */
5539 }
5540
Willy Tarreaud98cf932009-12-27 22:54:55 +01005541 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005542 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005543 * rep->buf.p still points to the beginning of the message and msg->sol
5544 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005545 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005546 channel_forward(res, msg->sov);
5547 msg->next = 0;
5548 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005549
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005550 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005551 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005552 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005553 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005554 }
5555
William Lallemand82fe75c2012-10-23 10:25:10 +02005556 if (s->comp_algo != NULL) {
5557 int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
5558 if (ret < 0)
5559 goto missing_data; /* not enough spaces in buffers */
5560 compressing = 1;
5561 }
5562
Willy Tarreaud98cf932009-12-27 22:54:55 +01005563 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005564 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005565 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005566 if (s->comp_algo == NULL) {
5567 bytes = msg->sov - msg->sol;
5568 if (msg->chunk_len || bytes) {
5569 msg->sol = msg->sov;
5570 msg->next -= bytes; /* will be forwarded */
5571 msg->chunk_len += bytes;
5572 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5573 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005574 }
5575
Willy Tarreaucaabe412010-01-03 23:08:28 +01005576 if (msg->msg_state == HTTP_MSG_DATA) {
5577 /* must still forward */
William Lallemand82fe75c2012-10-23 10:25:10 +02005578 if (compressing)
5579 http_compression_buffer_add_data(s, res->buf, tmpbuf);
5580
5581 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005582 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005583
5584 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005585 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005586 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005587 else
5588 msg->msg_state = HTTP_MSG_DONE;
5589 }
5590 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005591 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005592 * set ->sov and ->next to point to the body and switch to DATA or
5593 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005594 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005595 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005596
Willy Tarreau54d23df2012-10-25 19:04:45 +02005597 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005598 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005599 else if (ret < 0) {
5600 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005601 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005602 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005603 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005604 /* skipping data if we are in compression mode */
5605 if (compressing && msg->chunk_len > 0) {
5606 b_adv(res->buf, msg->next);
5607 msg->next = 0;
5608 msg->sov = 0;
5609 msg->sol = 0;
5610 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005611 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005612 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005613 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005614 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005615 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005616
Willy Tarreau54d23df2012-10-25 19:04:45 +02005617 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005618 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005619 else if (ret < 0) {
5620 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005621 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005622 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005623 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005624 /* skipping data in buffer for compression */
5625 if (compressing) {
5626 b_adv(res->buf, msg->next);
5627 msg->next = 0;
5628 msg->sov = 0;
5629 msg->sol = 0;
5630 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005631 /* we're in MSG_CHUNK_SIZE now */
5632 }
5633 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005634 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005635
Willy Tarreaud98cf932009-12-27 22:54:55 +01005636 if (ret == 0)
5637 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005638 else if (ret < 0) {
5639 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005640 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005641 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005642 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005643 if (compressing) {
5644 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5645 compressing = 0;
5646 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005647 /* we're in HTTP_MSG_DONE now */
5648 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005649 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005650 int old_state = msg->msg_state;
5651
William Lallemand82fe75c2012-10-23 10:25:10 +02005652 if (compressing) {
5653 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5654 compressing = 0;
5655 }
5656
Willy Tarreau610ecce2010-01-04 21:15:02 +01005657 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005658 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005659 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5660 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005661 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005662 if (http_resync_states(s)) {
5663 http_silent_debug(__LINE__, s);
5664 /* some state changes occurred, maybe the analyser
5665 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005666 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005667 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005668 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005669 /* response errors are most likely due to
5670 * the client aborting the transfer.
5671 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005672 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005673 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005674 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005675 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005676 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005677 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005678 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005679 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005680 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005681 }
5682 }
5683
Willy Tarreaud98cf932009-12-27 22:54:55 +01005684 missing_data:
William Lallemand82fe75c2012-10-23 10:25:10 +02005685 if (compressing) {
5686 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5687 compressing = 0;
5688 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005689 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005690 if (res->flags & CF_SHUTR) {
Willy Tarreau40dba092010-03-04 18:14:51 +01005691 if (!(s->flags & SN_ERR_MASK))
5692 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005693 s->be->be_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005694 if (target_srv(&s->target))
5695 target_srv(&s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005696 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005697 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005698
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005699 if (res->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005700 goto aborted_xfer;
5701
Willy Tarreau40dba092010-03-04 18:14:51 +01005702 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005703 if (!s->req->analysers)
5704 goto return_bad_res;
5705
Willy Tarreauea953162012-05-18 23:41:28 +02005706 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005707 if (s->comp_algo == NULL) {
5708 bytes = msg->sov - msg->sol;
5709 if (msg->chunk_len || bytes) {
5710 msg->sol = msg->sov;
5711 msg->next -= bytes; /* will be forwarded */
5712 msg->chunk_len += bytes;
5713 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5714 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005715 }
5716
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005717 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005718 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005719 * Similarly, with keep-alive on the client side, we don't want to forward a
5720 * close.
5721 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005722 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005723 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5724 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005725 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005726
Willy Tarreau5c620922011-05-11 19:56:11 +02005727 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005728 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005729 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005730 * modes are already handled by the stream sock layer. We must not do
5731 * this in content-length mode because it could present the MSG_MORE
5732 * flag with the last block of forwarded data, which would cause an
5733 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005734 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005735 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005736 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005737
Willy Tarreaud98cf932009-12-27 22:54:55 +01005738 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005739 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005740 return 0;
5741
Willy Tarreau40dba092010-03-04 18:14:51 +01005742 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005743 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005744 if (target_srv(&s->target))
5745 target_srv(&s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005746
5747 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005748 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005749 /* don't send any error message as we're in the body */
5750 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005751 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005752 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau827aee92011-03-10 16:55:02 +01005753 if (target_srv(&s->target))
5754 health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005755
5756 if (!(s->flags & SN_ERR_MASK))
5757 s->flags |= SN_ERR_PRXCOND;
5758 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005759 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005760 return 0;
5761
5762 aborted_xfer:
5763 txn->rsp.msg_state = HTTP_MSG_ERROR;
5764 /* don't send any error message as we're in the body */
5765 stream_int_retnclose(res->cons, NULL);
5766 res->analysers = 0;
5767 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5768
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005769 s->fe->fe_counters.cli_aborts++;
5770 s->be->be_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01005771 if (target_srv(&s->target))
5772 target_srv(&s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005773
5774 if (!(s->flags & SN_ERR_MASK))
5775 s->flags |= SN_ERR_CLICL;
5776 if (!(s->flags & SN_FINST_MASK))
5777 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005778 return 0;
5779}
5780
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005781/* Iterate the same filter through all request headers.
5782 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005783 * Since it can manage the switch to another backend, it updates the per-proxy
5784 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005785 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005786int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005787{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005788 char term;
5789 char *cur_ptr, *cur_end, *cur_next;
5790 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005791 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005792 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005793 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01005794
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005795 last_hdr = 0;
5796
Willy Tarreau9b28e032012-10-12 23:49:43 +02005797 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005798 old_idx = 0;
5799
5800 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01005801 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005802 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005803 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005804 (exp->action == ACT_ALLOW ||
5805 exp->action == ACT_DENY ||
5806 exp->action == ACT_TARPIT))
5807 return 0;
5808
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005809 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005810 if (!cur_idx)
5811 break;
5812
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005813 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005814 cur_ptr = cur_next;
5815 cur_end = cur_ptr + cur_hdr->len;
5816 cur_next = cur_end + cur_hdr->cr + 1;
5817
5818 /* Now we have one header between cur_ptr and cur_end,
5819 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005820 */
5821
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005822 /* The annoying part is that pattern matching needs
5823 * that we modify the contents to null-terminate all
5824 * strings before testing them.
5825 */
5826
5827 term = *cur_end;
5828 *cur_end = '\0';
5829
5830 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5831 switch (exp->action) {
5832 case ACT_SETBE:
5833 /* It is not possible to jump a second time.
5834 * FIXME: should we return an HTTP/500 here so that
5835 * the admin knows there's a problem ?
5836 */
5837 if (t->be != t->fe)
5838 break;
5839
5840 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005841 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005842 last_hdr = 1;
5843 break;
5844
5845 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005846 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005847 last_hdr = 1;
5848 break;
5849
5850 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005851 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005852 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005853
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005854 t->fe->fe_counters.denied_req++;
5855 if (t->fe != t->be)
5856 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005857 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005858 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005859
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005860 break;
5861
5862 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005863 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005864 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005865
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005866 t->fe->fe_counters.denied_req++;
5867 if (t->fe != t->be)
5868 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005869 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005870 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005871
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005872 break;
5873
5874 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005875 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
5876 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005877 /* FIXME: if the user adds a newline in the replacement, the
5878 * index will not be recalculated for now, and the new line
5879 * will not be counted as a new header.
5880 */
5881
5882 cur_end += delta;
5883 cur_next += delta;
5884 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01005885 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005886 break;
5887
5888 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02005889 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005890 cur_next += delta;
5891
Willy Tarreaufa355d42009-11-29 18:12:29 +01005892 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005893 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
5894 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005895 cur_hdr->len = 0;
5896 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01005897 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005898 break;
5899
5900 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01005901 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005902 if (cur_end)
5903 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01005904
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005905 /* keep the link from this header to next one in case of later
5906 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005907 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005908 old_idx = cur_idx;
5909 }
5910 return 0;
5911}
5912
5913
5914/* Apply the filter to the request line.
5915 * Returns 0 if nothing has been done, 1 if the filter has been applied,
5916 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005917 * Since it can manage the switch to another backend, it updates the per-proxy
5918 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005919 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005920int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005921{
5922 char term;
5923 char *cur_ptr, *cur_end;
5924 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005925 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005926 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005927
Willy Tarreau3d300592007-03-18 18:34:41 +01005928 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005929 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01005930 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005931 (exp->action == ACT_ALLOW ||
5932 exp->action == ACT_DENY ||
5933 exp->action == ACT_TARPIT))
5934 return 0;
5935 else if (exp->action == ACT_REMOVE)
5936 return 0;
5937
5938 done = 0;
5939
Willy Tarreau9b28e032012-10-12 23:49:43 +02005940 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005941 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005942
5943 /* Now we have the request line between cur_ptr and cur_end */
5944
5945 /* The annoying part is that pattern matching needs
5946 * that we modify the contents to null-terminate all
5947 * strings before testing them.
5948 */
5949
5950 term = *cur_end;
5951 *cur_end = '\0';
5952
5953 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
5954 switch (exp->action) {
5955 case ACT_SETBE:
5956 /* It is not possible to jump a second time.
5957 * FIXME: should we return an HTTP/500 here so that
5958 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01005959 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005960 if (t->be != t->fe)
5961 break;
5962
5963 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02005964 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005965 done = 1;
5966 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01005967
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005968 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01005969 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005970 done = 1;
5971 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005972
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005973 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01005974 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005975
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005976 t->fe->fe_counters.denied_req++;
5977 if (t->fe != t->be)
5978 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005979 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005980 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005981
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005982 done = 1;
5983 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005984
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005985 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01005986 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005987
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005988 t->fe->fe_counters.denied_req++;
5989 if (t->fe != t->be)
5990 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005991 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02005992 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005993
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005994 done = 1;
5995 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01005996
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005997 case ACT_REPLACE:
5998 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005999 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6000 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006001 /* FIXME: if the user adds a newline in the replacement, the
6002 * index will not be recalculated for now, and the new line
6003 * will not be counted as a new header.
6004 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006005
Willy Tarreaufa355d42009-11-29 18:12:29 +01006006 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006007 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006008 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006009 HTTP_MSG_RQMETH,
6010 cur_ptr, cur_end + 1,
6011 NULL, NULL);
6012 if (unlikely(!cur_end))
6013 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006014
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006015 /* we have a full request and we know that we have either a CR
6016 * or an LF at <ptr>.
6017 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006018 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6019 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006020 /* there is no point trying this regex on headers */
6021 return 1;
6022 }
6023 }
6024 *cur_end = term; /* restore the string terminator */
6025 return done;
6026}
Willy Tarreau97de6242006-12-27 17:18:38 +01006027
Willy Tarreau58f10d72006-12-04 02:26:12 +01006028
Willy Tarreau58f10d72006-12-04 02:26:12 +01006029
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006030/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006031 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006032 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006033 * unparsable request. Since it can manage the switch to another backend, it
6034 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006035 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006036int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006037{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006038 struct http_txn *txn = &s->txn;
6039 struct hdr_exp *exp;
6040
6041 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006042 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006043
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006044 /*
6045 * The interleaving of transformations and verdicts
6046 * makes it difficult to decide to continue or stop
6047 * the evaluation.
6048 */
6049
Willy Tarreau6c123b12010-01-28 20:22:06 +01006050 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6051 break;
6052
Willy Tarreau3d300592007-03-18 18:34:41 +01006053 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006054 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006055 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006056 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006057
6058 /* if this filter had a condition, evaluate it now and skip to
6059 * next filter if the condition does not match.
6060 */
6061 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006062 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006063 ret = acl_pass(ret);
6064 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6065 ret = !ret;
6066
6067 if (!ret)
6068 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006069 }
6070
6071 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006072 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006073 if (unlikely(ret < 0))
6074 return -1;
6075
6076 if (likely(ret == 0)) {
6077 /* The filter did not match the request, it can be
6078 * iterated through all headers.
6079 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006080 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006081 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006082 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006083 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006084}
6085
6086
Willy Tarreaua15645d2007-03-18 16:22:39 +01006087
Willy Tarreau58f10d72006-12-04 02:26:12 +01006088/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006089 * Try to retrieve the server associated to the appsession.
6090 * If the server is found, it's assigned to the session.
6091 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006092void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006093 struct http_txn *txn = &t->txn;
6094 appsess *asession = NULL;
6095 char *sessid_temp = NULL;
6096
Cyril Bontéb21570a2009-11-29 20:04:48 +01006097 if (len > t->be->appsession_len) {
6098 len = t->be->appsession_len;
6099 }
6100
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006101 if (t->be->options2 & PR_O2_AS_REQL) {
6102 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006103 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006104 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006105 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006106 }
6107
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006108 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006109 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6110 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6111 return;
6112 }
6113
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006114 memcpy(txn->sessid, buf, len);
6115 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006116 }
6117
6118 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6119 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6120 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6121 return;
6122 }
6123
Cyril Bontéb21570a2009-11-29 20:04:48 +01006124 memcpy(sessid_temp, buf, len);
6125 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006126
6127 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6128 /* free previously allocated memory */
6129 pool_free2(apools.sessid, sessid_temp);
6130
6131 if (asession != NULL) {
6132 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6133 if (!(t->be->options2 & PR_O2_AS_REQL))
6134 asession->request_count++;
6135
6136 if (asession->serverid != NULL) {
6137 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006138
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006139 while (srv) {
6140 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006141 if ((srv->state & SRV_RUNNING) ||
6142 (t->be->options & PR_O_PERSIST) ||
6143 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006144 /* we found the server and it's usable */
6145 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006146 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006147 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006148 set_target_server(&t->target, srv);
Willy Tarreau664beb82011-03-10 11:38:29 +01006149
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006150 break;
6151 } else {
6152 txn->flags &= ~TX_CK_MASK;
6153 txn->flags |= TX_CK_DOWN;
6154 }
6155 }
6156 srv = srv->next;
6157 }
6158 }
6159 }
6160}
6161
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006162/* Find the end of a cookie value contained between <s> and <e>. It works the
6163 * same way as with headers above except that the semi-colon also ends a token.
6164 * See RFC2965 for more information. Note that it requires a valid header to
6165 * return a valid result.
6166 */
6167char *find_cookie_value_end(char *s, const char *e)
6168{
6169 int quoted, qdpair;
6170
6171 quoted = qdpair = 0;
6172 for (; s < e; s++) {
6173 if (qdpair) qdpair = 0;
6174 else if (quoted) {
6175 if (*s == '\\') qdpair = 1;
6176 else if (*s == '"') quoted = 0;
6177 }
6178 else if (*s == '"') quoted = 1;
6179 else if (*s == ',' || *s == ';') return s;
6180 }
6181 return s;
6182}
6183
6184/* Delete a value in a header between delimiters <from> and <next> in buffer
6185 * <buf>. The number of characters displaced is returned, and the pointer to
6186 * the first delimiter is updated if required. The function tries as much as
6187 * possible to respect the following principles :
6188 * - replace <from> delimiter by the <next> one unless <from> points to a
6189 * colon, in which case <next> is simply removed
6190 * - set exactly one space character after the new first delimiter, unless
6191 * there are not enough characters in the block being moved to do so.
6192 * - remove unneeded spaces before the previous delimiter and after the new
6193 * one.
6194 *
6195 * It is the caller's responsibility to ensure that :
6196 * - <from> points to a valid delimiter or the colon ;
6197 * - <next> points to a valid delimiter or the final CR/LF ;
6198 * - there are non-space chars before <from> ;
6199 * - there is a CR/LF at or after <next>.
6200 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006201int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006202{
6203 char *prev = *from;
6204
6205 if (*prev == ':') {
6206 /* We're removing the first value, preserve the colon and add a
6207 * space if possible.
6208 */
6209 if (!http_is_crlf[(unsigned char)*next])
6210 next++;
6211 prev++;
6212 if (prev < next)
6213 *prev++ = ' ';
6214
6215 while (http_is_spht[(unsigned char)*next])
6216 next++;
6217 } else {
6218 /* Remove useless spaces before the old delimiter. */
6219 while (http_is_spht[(unsigned char)*(prev-1)])
6220 prev--;
6221 *from = prev;
6222
6223 /* copy the delimiter and if possible a space if we're
6224 * not at the end of the line.
6225 */
6226 if (!http_is_crlf[(unsigned char)*next]) {
6227 *prev++ = *next++;
6228 if (prev + 1 < next)
6229 *prev++ = ' ';
6230 while (http_is_spht[(unsigned char)*next])
6231 next++;
6232 }
6233 }
6234 return buffer_replace2(buf, prev, next, NULL, 0);
6235}
6236
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006237/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006238 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006239 * desirable to call it only when needed. This code is quite complex because
6240 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6241 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006242 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006243void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006244{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006245 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006246 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006247 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006248 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6249 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006250
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006251 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006252 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006253 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006254
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006255 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006256 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006257 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006258
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006259 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006260 hdr_beg = hdr_next;
6261 hdr_end = hdr_beg + cur_hdr->len;
6262 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006263
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006264 /* We have one full header between hdr_beg and hdr_end, and the
6265 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006266 * "Cookie:" headers.
6267 */
6268
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006269 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006270 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006271 old_idx = cur_idx;
6272 continue;
6273 }
6274
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006275 del_from = NULL; /* nothing to be deleted */
6276 preserve_hdr = 0; /* assume we may kill the whole header */
6277
Willy Tarreau58f10d72006-12-04 02:26:12 +01006278 /* Now look for cookies. Conforming to RFC2109, we have to support
6279 * attributes whose name begin with a '$', and associate them with
6280 * the right cookie, if we want to delete this cookie.
6281 * So there are 3 cases for each cookie read :
6282 * 1) it's a special attribute, beginning with a '$' : ignore it.
6283 * 2) it's a server id cookie that we *MAY* want to delete : save
6284 * some pointers on it (last semi-colon, beginning of cookie...)
6285 * 3) it's an application cookie : we *MAY* have to delete a previous
6286 * "special" cookie.
6287 * At the end of loop, if a "special" cookie remains, we may have to
6288 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006289 * *MUST* delete it.
6290 *
6291 * Note: RFC2965 is unclear about the processing of spaces around
6292 * the equal sign in the ATTR=VALUE form. A careful inspection of
6293 * the RFC explicitly allows spaces before it, and not within the
6294 * tokens (attrs or values). An inspection of RFC2109 allows that
6295 * too but section 10.1.3 lets one think that spaces may be allowed
6296 * after the equal sign too, resulting in some (rare) buggy
6297 * implementations trying to do that. So let's do what servers do.
6298 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6299 * allowed quoted strings in values, with any possible character
6300 * after a backslash, including control chars and delimitors, which
6301 * causes parsing to become ambiguous. Browsers also allow spaces
6302 * within values even without quotes.
6303 *
6304 * We have to keep multiple pointers in order to support cookie
6305 * removal at the beginning, middle or end of header without
6306 * corrupting the header. All of these headers are valid :
6307 *
6308 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6309 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6310 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6311 * | | | | | | | | |
6312 * | | | | | | | | hdr_end <--+
6313 * | | | | | | | +--> next
6314 * | | | | | | +----> val_end
6315 * | | | | | +-----------> val_beg
6316 * | | | | +--------------> equal
6317 * | | | +----------------> att_end
6318 * | | +---------------------> att_beg
6319 * | +--------------------------> prev
6320 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006321 */
6322
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006323 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6324 /* Iterate through all cookies on this line */
6325
6326 /* find att_beg */
6327 att_beg = prev + 1;
6328 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6329 att_beg++;
6330
6331 /* find att_end : this is the first character after the last non
6332 * space before the equal. It may be equal to hdr_end.
6333 */
6334 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006335
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006336 while (equal < hdr_end) {
6337 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006339 if (http_is_spht[(unsigned char)*equal++])
6340 continue;
6341 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006342 }
6343
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006344 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6345 * is between <att_beg> and <equal>, both may be identical.
6346 */
6347
6348 /* look for end of cookie if there is an equal sign */
6349 if (equal < hdr_end && *equal == '=') {
6350 /* look for the beginning of the value */
6351 val_beg = equal + 1;
6352 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6353 val_beg++;
6354
6355 /* find the end of the value, respecting quotes */
6356 next = find_cookie_value_end(val_beg, hdr_end);
6357
6358 /* make val_end point to the first white space or delimitor after the value */
6359 val_end = next;
6360 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6361 val_end--;
6362 } else {
6363 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006364 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006365
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006366 /* We have nothing to do with attributes beginning with '$'. However,
6367 * they will automatically be removed if a header before them is removed,
6368 * since they're supposed to be linked together.
6369 */
6370 if (*att_beg == '$')
6371 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006372
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006373 /* Ignore cookies with no equal sign */
6374 if (equal == next) {
6375 /* This is not our cookie, so we must preserve it. But if we already
6376 * scheduled another cookie for removal, we cannot remove the
6377 * complete header, but we can remove the previous block itself.
6378 */
6379 preserve_hdr = 1;
6380 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006381 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006382 val_end += delta;
6383 next += delta;
6384 hdr_end += delta;
6385 hdr_next += delta;
6386 cur_hdr->len += delta;
6387 http_msg_move_end(&txn->req, delta);
6388 prev = del_from;
6389 del_from = NULL;
6390 }
6391 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006392 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006393
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006394 /* if there are spaces around the equal sign, we need to
6395 * strip them otherwise we'll get trouble for cookie captures,
6396 * or even for rewrites. Since this happens extremely rarely,
6397 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006398 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006399 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6400 int stripped_before = 0;
6401 int stripped_after = 0;
6402
6403 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006404 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006405 equal += stripped_before;
6406 val_beg += stripped_before;
6407 }
6408
6409 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006410 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006411 val_beg += stripped_after;
6412 stripped_before += stripped_after;
6413 }
6414
6415 val_end += stripped_before;
6416 next += stripped_before;
6417 hdr_end += stripped_before;
6418 hdr_next += stripped_before;
6419 cur_hdr->len += stripped_before;
6420 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006421 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006422 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006423
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006424 /* First, let's see if we want to capture this cookie. We check
6425 * that we don't already have a client side cookie, because we
6426 * can only capture one. Also as an optimisation, we ignore
6427 * cookies shorter than the declared name.
6428 */
6429 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6430 (val_end - att_beg >= t->fe->capture_namelen) &&
6431 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6432 int log_len = val_end - att_beg;
6433
6434 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6435 Alert("HTTP logging : out of memory.\n");
6436 } else {
6437 if (log_len > t->fe->capture_len)
6438 log_len = t->fe->capture_len;
6439 memcpy(txn->cli_cookie, att_beg, log_len);
6440 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006441 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006442 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006443
Willy Tarreaubca99692010-10-06 19:25:55 +02006444 /* Persistence cookies in passive, rewrite or insert mode have the
6445 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006446 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006447 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006448 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006449 * For cookies in prefix mode, the form is :
6450 *
6451 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006452 */
6453 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6454 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6455 struct server *srv = t->be->srv;
6456 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006457
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006458 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6459 * have the server ID between val_beg and delim, and the original cookie between
6460 * delim+1 and val_end. Otherwise, delim==val_end :
6461 *
6462 * Cookie: NAME=SRV; # in all but prefix modes
6463 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6464 * | || || | |+-> next
6465 * | || || | +--> val_end
6466 * | || || +---------> delim
6467 * | || |+------------> val_beg
6468 * | || +-------------> att_end = equal
6469 * | |+-----------------> att_beg
6470 * | +------------------> prev
6471 * +-------------------------> hdr_beg
6472 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006473
Willy Tarreau67402132012-05-31 20:40:20 +02006474 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006475 for (delim = val_beg; delim < val_end; delim++)
6476 if (*delim == COOKIE_DELIM)
6477 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006478 } else {
6479 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006480 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006481 /* Now check if the cookie contains a date field, which would
6482 * appear after a vertical bar ('|') just after the server name
6483 * and before the delimiter.
6484 */
6485 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6486 if (vbar1) {
6487 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006488 * right is the last seen date. It is a base64 encoded
6489 * 30-bit value representing the UNIX date since the
6490 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006491 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006492 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006493 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006494 if (val_end - vbar1 >= 5) {
6495 val = b64tos30(vbar1);
6496 if (val > 0)
6497 txn->cookie_last_date = val << 2;
6498 }
6499 /* look for a second vertical bar */
6500 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6501 if (vbar1 && (val_end - vbar1 > 5)) {
6502 val = b64tos30(vbar1 + 1);
6503 if (val > 0)
6504 txn->cookie_first_date = val << 2;
6505 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006506 }
6507 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006508
Willy Tarreauf64d1412010-10-07 20:06:11 +02006509 /* if the cookie has an expiration date and the proxy wants to check
6510 * it, then we do that now. We first check if the cookie is too old,
6511 * then only if it has expired. We detect strict overflow because the
6512 * time resolution here is not great (4 seconds). Cookies with dates
6513 * in the future are ignored if their offset is beyond one day. This
6514 * allows an admin to fix timezone issues without expiring everyone
6515 * and at the same time avoids keeping unwanted side effects for too
6516 * long.
6517 */
6518 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006519 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6520 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006521 txn->flags &= ~TX_CK_MASK;
6522 txn->flags |= TX_CK_OLD;
6523 delim = val_beg; // let's pretend we have not found the cookie
6524 txn->cookie_first_date = 0;
6525 txn->cookie_last_date = 0;
6526 }
6527 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006528 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6529 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006530 txn->flags &= ~TX_CK_MASK;
6531 txn->flags |= TX_CK_EXPIRED;
6532 delim = val_beg; // let's pretend we have not found the cookie
6533 txn->cookie_first_date = 0;
6534 txn->cookie_last_date = 0;
6535 }
6536
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006537 /* Here, we'll look for the first running server which supports the cookie.
6538 * This allows to share a same cookie between several servers, for example
6539 * to dedicate backup servers to specific servers only.
6540 * However, to prevent clients from sticking to cookie-less backup server
6541 * when they have incidentely learned an empty cookie, we simply ignore
6542 * empty cookies and mark them as invalid.
6543 * The same behaviour is applied when persistence must be ignored.
6544 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006545 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006546 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006547
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006548 while (srv) {
6549 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6550 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6551 if ((srv->state & SRV_RUNNING) ||
6552 (t->be->options & PR_O_PERSIST) ||
6553 (t->flags & SN_FORCE_PRST)) {
6554 /* we found the server and we can use it */
6555 txn->flags &= ~TX_CK_MASK;
6556 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6557 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau9e000c62011-03-10 14:03:36 +01006558 set_target_server(&t->target, srv);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006559 break;
6560 } else {
6561 /* we found a server, but it's down,
6562 * mark it as such and go on in case
6563 * another one is available.
6564 */
6565 txn->flags &= ~TX_CK_MASK;
6566 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006567 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006568 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006569 srv = srv->next;
6570 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006571
Willy Tarreauf64d1412010-10-07 20:06:11 +02006572 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006573 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006574 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006575 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6576 txn->flags |= TX_CK_UNUSED;
6577 else
6578 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006579 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006580
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006581 /* depending on the cookie mode, we may have to either :
6582 * - delete the complete cookie if we're in insert+indirect mode, so that
6583 * the server never sees it ;
6584 * - remove the server id from the cookie value, and tag the cookie as an
6585 * application cookie so that it does not get accidentely removed later,
6586 * if we're in cookie prefix mode
6587 */
Willy Tarreau67402132012-05-31 20:40:20 +02006588 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006589 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006590
Willy Tarreau9b28e032012-10-12 23:49:43 +02006591 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006592 val_end += delta;
6593 next += delta;
6594 hdr_end += delta;
6595 hdr_next += delta;
6596 cur_hdr->len += delta;
6597 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006598
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006599 del_from = NULL;
6600 preserve_hdr = 1; /* we want to keep this cookie */
6601 }
6602 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006603 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006604 del_from = prev;
6605 }
6606 } else {
6607 /* This is not our cookie, so we must preserve it. But if we already
6608 * scheduled another cookie for removal, we cannot remove the
6609 * complete header, but we can remove the previous block itself.
6610 */
6611 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006612
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006613 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006614 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006615 if (att_beg >= del_from)
6616 att_beg += delta;
6617 if (att_end >= del_from)
6618 att_end += delta;
6619 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006620 val_end += delta;
6621 next += delta;
6622 hdr_end += delta;
6623 hdr_next += delta;
6624 cur_hdr->len += delta;
6625 http_msg_move_end(&txn->req, delta);
6626 prev = del_from;
6627 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006628 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006629 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006630
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006631 /* Look for the appsession cookie unless persistence must be ignored */
6632 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6633 int cmp_len, value_len;
6634 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006635
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006636 if (t->be->options2 & PR_O2_AS_PFX) {
6637 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6638 value_begin = att_beg + t->be->appsession_name_len;
6639 value_len = val_end - att_beg - t->be->appsession_name_len;
6640 } else {
6641 cmp_len = att_end - att_beg;
6642 value_begin = val_beg;
6643 value_len = val_end - val_beg;
6644 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006645
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006646 /* let's see if the cookie is our appcookie */
6647 if (cmp_len == t->be->appsession_name_len &&
6648 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6649 manage_client_side_appsession(t, value_begin, value_len);
6650 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006651 }
6652
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006653 /* continue with next cookie on this header line */
6654 att_beg = next;
6655 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006656
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006657 /* There are no more cookies on this line.
6658 * We may still have one (or several) marked for deletion at the
6659 * end of the line. We must do this now in two ways :
6660 * - if some cookies must be preserved, we only delete from the
6661 * mark to the end of line ;
6662 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006663 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006664 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006665 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006667 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006668 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006669 cur_hdr->len += delta;
6670 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006671 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006672
6673 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006674 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6675 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006676 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006677 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006678 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006679 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006680 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006681 }
6682
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006684 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006685 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006686}
6687
6688
Willy Tarreaua15645d2007-03-18 16:22:39 +01006689/* Iterate the same filter through all response headers contained in <rtr>.
6690 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6691 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006692int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006693{
6694 char term;
6695 char *cur_ptr, *cur_end, *cur_next;
6696 int cur_idx, old_idx, last_hdr;
6697 struct http_txn *txn = &t->txn;
6698 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006699 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006700
6701 last_hdr = 0;
6702
Willy Tarreau9b28e032012-10-12 23:49:43 +02006703 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006704 old_idx = 0;
6705
6706 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006707 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006708 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006709 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006710 (exp->action == ACT_ALLOW ||
6711 exp->action == ACT_DENY))
6712 return 0;
6713
6714 cur_idx = txn->hdr_idx.v[old_idx].next;
6715 if (!cur_idx)
6716 break;
6717
6718 cur_hdr = &txn->hdr_idx.v[cur_idx];
6719 cur_ptr = cur_next;
6720 cur_end = cur_ptr + cur_hdr->len;
6721 cur_next = cur_end + cur_hdr->cr + 1;
6722
6723 /* Now we have one header between cur_ptr and cur_end,
6724 * and the next header starts at cur_next.
6725 */
6726
6727 /* The annoying part is that pattern matching needs
6728 * that we modify the contents to null-terminate all
6729 * strings before testing them.
6730 */
6731
6732 term = *cur_end;
6733 *cur_end = '\0';
6734
6735 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6736 switch (exp->action) {
6737 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006738 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006739 last_hdr = 1;
6740 break;
6741
6742 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006743 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006744 last_hdr = 1;
6745 break;
6746
6747 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006748 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6749 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006750 /* FIXME: if the user adds a newline in the replacement, the
6751 * index will not be recalculated for now, and the new line
6752 * will not be counted as a new header.
6753 */
6754
6755 cur_end += delta;
6756 cur_next += delta;
6757 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006758 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006759 break;
6760
6761 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006762 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006763 cur_next += delta;
6764
Willy Tarreaufa355d42009-11-29 18:12:29 +01006765 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006766 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6767 txn->hdr_idx.used--;
6768 cur_hdr->len = 0;
6769 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006770 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006771 break;
6772
6773 }
6774 }
6775 if (cur_end)
6776 *cur_end = term; /* restore the string terminator */
6777
6778 /* keep the link from this header to next one in case of later
6779 * removal of next header.
6780 */
6781 old_idx = cur_idx;
6782 }
6783 return 0;
6784}
6785
6786
6787/* Apply the filter to the status line in the response buffer <rtr>.
6788 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6789 * or -1 if a replacement resulted in an invalid status line.
6790 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006791int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006792{
6793 char term;
6794 char *cur_ptr, *cur_end;
6795 int done;
6796 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006797 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006798
6799
Willy Tarreau3d300592007-03-18 18:34:41 +01006800 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006801 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006802 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006803 (exp->action == ACT_ALLOW ||
6804 exp->action == ACT_DENY))
6805 return 0;
6806 else if (exp->action == ACT_REMOVE)
6807 return 0;
6808
6809 done = 0;
6810
Willy Tarreau9b28e032012-10-12 23:49:43 +02006811 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006812 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006813
6814 /* Now we have the status line between cur_ptr and cur_end */
6815
6816 /* The annoying part is that pattern matching needs
6817 * that we modify the contents to null-terminate all
6818 * strings before testing them.
6819 */
6820
6821 term = *cur_end;
6822 *cur_end = '\0';
6823
6824 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6825 switch (exp->action) {
6826 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006827 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006828 done = 1;
6829 break;
6830
6831 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006832 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006833 done = 1;
6834 break;
6835
6836 case ACT_REPLACE:
6837 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006838 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6839 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006840 /* FIXME: if the user adds a newline in the replacement, the
6841 * index will not be recalculated for now, and the new line
6842 * will not be counted as a new header.
6843 */
6844
Willy Tarreaufa355d42009-11-29 18:12:29 +01006845 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006846 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006847 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02006848 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01006849 cur_ptr, cur_end + 1,
6850 NULL, NULL);
6851 if (unlikely(!cur_end))
6852 return -1;
6853
6854 /* we have a full respnse and we know that we have either a CR
6855 * or an LF at <ptr>.
6856 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02006857 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02006858 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01006859 /* there is no point trying this regex on headers */
6860 return 1;
6861 }
6862 }
6863 *cur_end = term; /* restore the string terminator */
6864 return done;
6865}
6866
6867
6868
6869/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006870 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006871 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
6872 * unparsable response.
6873 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006874int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006875{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006876 struct http_txn *txn = &s->txn;
6877 struct hdr_exp *exp;
6878
6879 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01006880 int ret;
6881
6882 /*
6883 * The interleaving of transformations and verdicts
6884 * makes it difficult to decide to continue or stop
6885 * the evaluation.
6886 */
6887
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006888 if (txn->flags & TX_SVDENY)
6889 break;
6890
Willy Tarreau3d300592007-03-18 18:34:41 +01006891 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006892 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
6893 exp->action == ACT_PASS)) {
6894 exp = exp->next;
6895 continue;
6896 }
6897
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006898 /* if this filter had a condition, evaluate it now and skip to
6899 * next filter if the condition does not match.
6900 */
6901 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006902 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006903 ret = acl_pass(ret);
6904 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6905 ret = !ret;
6906 if (!ret)
6907 continue;
6908 }
6909
Willy Tarreaua15645d2007-03-18 16:22:39 +01006910 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006911 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912 if (unlikely(ret < 0))
6913 return -1;
6914
6915 if (likely(ret == 0)) {
6916 /* The filter did not match the response, it can be
6917 * iterated through all headers.
6918 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01006919 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006920 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01006921 }
6922 return 0;
6923}
6924
6925
Willy Tarreaua15645d2007-03-18 16:22:39 +01006926/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006927 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02006928 * desirable to call it only when needed. This function is also used when we
6929 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01006930 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006931void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006932{
6933 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01006934 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006935 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006936 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02006937 char *hdr_beg, *hdr_end, *hdr_next;
6938 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006939
Willy Tarreaua15645d2007-03-18 16:22:39 +01006940 /* Iterate through the headers.
6941 * we start with the start line.
6942 */
6943 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006944 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006945
6946 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
6947 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006948 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006949
6950 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02006951 hdr_beg = hdr_next;
6952 hdr_end = hdr_beg + cur_hdr->len;
6953 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006954
Willy Tarreau24581ba2010-08-31 22:39:35 +02006955 /* We have one full header between hdr_beg and hdr_end, and the
6956 * next header starts at hdr_next. We're only interested in
6957 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006958 */
6959
Willy Tarreau24581ba2010-08-31 22:39:35 +02006960 is_cookie2 = 0;
6961 prev = hdr_beg + 10;
6962 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006963 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02006964 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
6965 if (!val) {
6966 old_idx = cur_idx;
6967 continue;
6968 }
6969 is_cookie2 = 1;
6970 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006971 }
6972
Willy Tarreau24581ba2010-08-31 22:39:35 +02006973 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
6974 * <prev> points to the colon.
6975 */
Willy Tarreauf1348312010-10-07 15:54:11 +02006976 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006977
Willy Tarreau24581ba2010-08-31 22:39:35 +02006978 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
6979 * check-cache is enabled) and we are not interested in checking
6980 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006981 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02006982 if (t->be->cookie_name == NULL &&
6983 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02006984 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006985 return;
6986
Willy Tarreau24581ba2010-08-31 22:39:35 +02006987 /* OK so now we know we have to process this response cookie.
6988 * The format of the Set-Cookie header is slightly different
6989 * from the format of the Cookie header in that it does not
6990 * support the comma as a cookie delimiter (thus the header
6991 * cannot be folded) because the Expires attribute described in
6992 * the original Netscape's spec may contain an unquoted date
6993 * with a comma inside. We have to live with this because
6994 * many browsers don't support Max-Age and some browsers don't
6995 * support quoted strings. However the Set-Cookie2 header is
6996 * clean.
6997 *
6998 * We have to keep multiple pointers in order to support cookie
6999 * removal at the beginning, middle or end of header without
7000 * corrupting the header (in case of set-cookie2). A special
7001 * pointer, <scav> points to the beginning of the set-cookie-av
7002 * fields after the first semi-colon. The <next> pointer points
7003 * either to the end of line (set-cookie) or next unquoted comma
7004 * (set-cookie2). All of these headers are valid :
7005 *
7006 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7007 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7008 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7009 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7010 * | | | | | | | | | |
7011 * | | | | | | | | +-> next hdr_end <--+
7012 * | | | | | | | +------------> scav
7013 * | | | | | | +--------------> val_end
7014 * | | | | | +--------------------> val_beg
7015 * | | | | +----------------------> equal
7016 * | | | +------------------------> att_end
7017 * | | +----------------------------> att_beg
7018 * | +------------------------------> prev
7019 * +-----------------------------------------> hdr_beg
7020 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007021
Willy Tarreau24581ba2010-08-31 22:39:35 +02007022 for (; prev < hdr_end; prev = next) {
7023 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007024
Willy Tarreau24581ba2010-08-31 22:39:35 +02007025 /* find att_beg */
7026 att_beg = prev + 1;
7027 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7028 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007029
Willy Tarreau24581ba2010-08-31 22:39:35 +02007030 /* find att_end : this is the first character after the last non
7031 * space before the equal. It may be equal to hdr_end.
7032 */
7033 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007034
Willy Tarreau24581ba2010-08-31 22:39:35 +02007035 while (equal < hdr_end) {
7036 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7037 break;
7038 if (http_is_spht[(unsigned char)*equal++])
7039 continue;
7040 att_end = equal;
7041 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007042
Willy Tarreau24581ba2010-08-31 22:39:35 +02007043 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7044 * is between <att_beg> and <equal>, both may be identical.
7045 */
7046
7047 /* look for end of cookie if there is an equal sign */
7048 if (equal < hdr_end && *equal == '=') {
7049 /* look for the beginning of the value */
7050 val_beg = equal + 1;
7051 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7052 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007053
Willy Tarreau24581ba2010-08-31 22:39:35 +02007054 /* find the end of the value, respecting quotes */
7055 next = find_cookie_value_end(val_beg, hdr_end);
7056
7057 /* make val_end point to the first white space or delimitor after the value */
7058 val_end = next;
7059 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7060 val_end--;
7061 } else {
7062 /* <equal> points to next comma, semi-colon or EOL */
7063 val_beg = val_end = next = equal;
7064 }
7065
7066 if (next < hdr_end) {
7067 /* Set-Cookie2 supports multiple cookies, and <next> points to
7068 * a colon or semi-colon before the end. So skip all attr-value
7069 * pairs and look for the next comma. For Set-Cookie, since
7070 * commas are permitted in values, skip to the end.
7071 */
7072 if (is_cookie2)
7073 next = find_hdr_value_end(next, hdr_end);
7074 else
7075 next = hdr_end;
7076 }
7077
7078 /* Now everything is as on the diagram above */
7079
7080 /* Ignore cookies with no equal sign */
7081 if (equal == val_end)
7082 continue;
7083
7084 /* If there are spaces around the equal sign, we need to
7085 * strip them otherwise we'll get trouble for cookie captures,
7086 * or even for rewrites. Since this happens extremely rarely,
7087 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007088 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007089 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7090 int stripped_before = 0;
7091 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007092
Willy Tarreau24581ba2010-08-31 22:39:35 +02007093 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007094 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007095 equal += stripped_before;
7096 val_beg += stripped_before;
7097 }
7098
7099 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007100 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007101 val_beg += stripped_after;
7102 stripped_before += stripped_after;
7103 }
7104
7105 val_end += stripped_before;
7106 next += stripped_before;
7107 hdr_end += stripped_before;
7108 hdr_next += stripped_before;
7109 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007110 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007111 }
7112
7113 /* First, let's see if we want to capture this cookie. We check
7114 * that we don't already have a server side cookie, because we
7115 * can only capture one. Also as an optimisation, we ignore
7116 * cookies shorter than the declared name.
7117 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007118 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007119 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007120 (val_end - att_beg >= t->fe->capture_namelen) &&
7121 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7122 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007123 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007124 Alert("HTTP logging : out of memory.\n");
7125 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007126 else {
7127 if (log_len > t->fe->capture_len)
7128 log_len = t->fe->capture_len;
7129 memcpy(txn->srv_cookie, att_beg, log_len);
7130 txn->srv_cookie[log_len] = 0;
7131 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007132 }
7133
Willy Tarreau827aee92011-03-10 16:55:02 +01007134 srv = target_srv(&t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007135 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007136 if (!(t->flags & SN_IGNORE_PRST) &&
7137 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7138 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007139 /* assume passive cookie by default */
7140 txn->flags &= ~TX_SCK_MASK;
7141 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007142
7143 /* If the cookie is in insert mode on a known server, we'll delete
7144 * this occurrence because we'll insert another one later.
7145 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007146 * a direct access.
7147 */
Willy Tarreau67402132012-05-31 20:40:20 +02007148 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007149 /* The "preserve" flag was set, we don't want to touch the
7150 * server's cookie.
7151 */
7152 }
Willy Tarreau67402132012-05-31 20:40:20 +02007153 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7154 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007155 /* this cookie must be deleted */
7156 if (*prev == ':' && next == hdr_end) {
7157 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007158 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007159 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7160 txn->hdr_idx.used--;
7161 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007162 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007163 hdr_next += delta;
7164 http_msg_move_end(&txn->rsp, delta);
7165 /* note: while both invalid now, <next> and <hdr_end>
7166 * are still equal, so the for() will stop as expected.
7167 */
7168 } else {
7169 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007170 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007171 next = prev;
7172 hdr_end += delta;
7173 hdr_next += delta;
7174 cur_hdr->len += delta;
7175 http_msg_move_end(&txn->rsp, delta);
7176 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007177 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007178 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007179 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007180 }
Willy Tarreau67402132012-05-31 20:40:20 +02007181 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007182 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007183 * with this server since we know it.
7184 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007185 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007186 next += delta;
7187 hdr_end += delta;
7188 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007189 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007190 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007191
Willy Tarreauf1348312010-10-07 15:54:11 +02007192 txn->flags &= ~TX_SCK_MASK;
7193 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007194 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007195 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007196 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007197 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007198 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007199 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007200 next += delta;
7201 hdr_end += delta;
7202 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007203 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007204 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007205
Willy Tarreau827aee92011-03-10 16:55:02 +01007206 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007207 txn->flags &= ~TX_SCK_MASK;
7208 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007209 }
7210 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007211 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7212 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007213 int cmp_len, value_len;
7214 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007215
Cyril Bontéb21570a2009-11-29 20:04:48 +01007216 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007217 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7218 value_begin = att_beg + t->be->appsession_name_len;
7219 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007220 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007221 cmp_len = att_end - att_beg;
7222 value_begin = val_beg;
7223 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007224 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007225
Cyril Bonté17530c32010-04-06 21:11:10 +02007226 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007227 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7228 /* free a possibly previously allocated memory */
7229 pool_free2(apools.sessid, txn->sessid);
7230
Cyril Bontéb21570a2009-11-29 20:04:48 +01007231 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007232 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007233 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7234 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7235 return;
7236 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007237 memcpy(txn->sessid, value_begin, value_len);
7238 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007239 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007240 }
7241 /* that's done for this cookie, check the next one on the same
7242 * line when next != hdr_end (only if is_cookie2).
7243 */
7244 }
7245 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007247 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007248
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007249 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007250 appsess *asession = NULL;
7251 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007252 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007253 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007254 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007255 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7256 Alert("Not enough Memory process_srv():asession:calloc().\n");
7257 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7258 return;
7259 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007260 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7261
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007262 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7263 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7264 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007265 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007266 return;
7267 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007268 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007269 asession->sessid[t->be->appsession_len] = 0;
7270
Willy Tarreau827aee92011-03-10 16:55:02 +01007271 server_id_len = strlen(target_srv(&t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007272 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007273 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007274 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007275 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007276 return;
7277 }
7278 asession->serverid[0] = '\0';
Willy Tarreau827aee92011-03-10 16:55:02 +01007279 memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007280
7281 asession->request_count = 0;
7282 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7283 }
7284
7285 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7286 asession->request_count++;
7287 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007288}
7289
7290
Willy Tarreaua15645d2007-03-18 16:22:39 +01007291/*
7292 * Check if response is cacheable or not. Updates t->flags.
7293 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007294void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295{
7296 struct http_txn *txn = &t->txn;
7297 char *p1, *p2;
7298
7299 char *cur_ptr, *cur_end, *cur_next;
7300 int cur_idx;
7301
Willy Tarreau5df51872007-11-25 16:20:08 +01007302 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007303 return;
7304
7305 /* Iterate through the headers.
7306 * we start with the start line.
7307 */
7308 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007309 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007310
7311 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7312 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007313 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007314
7315 cur_hdr = &txn->hdr_idx.v[cur_idx];
7316 cur_ptr = cur_next;
7317 cur_end = cur_ptr + cur_hdr->len;
7318 cur_next = cur_end + cur_hdr->cr + 1;
7319
7320 /* We have one full header between cur_ptr and cur_end, and the
7321 * next header starts at cur_next. We're only interested in
7322 * "Cookie:" headers.
7323 */
7324
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007325 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7326 if (val) {
7327 if ((cur_end - (cur_ptr + val) >= 8) &&
7328 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7329 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7330 return;
7331 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007332 }
7333
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007334 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7335 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007336 continue;
7337
7338 /* OK, right now we know we have a cache-control header at cur_ptr */
7339
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007340 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007341
7342 if (p1 >= cur_end) /* no more info */
7343 continue;
7344
7345 /* p1 is at the beginning of the value */
7346 p2 = p1;
7347
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007348 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007349 p2++;
7350
7351 /* we have a complete value between p1 and p2 */
7352 if (p2 < cur_end && *p2 == '=') {
7353 /* we have something of the form no-cache="set-cookie" */
7354 if ((cur_end - p1 >= 21) &&
7355 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7356 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007357 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007358 continue;
7359 }
7360
7361 /* OK, so we know that either p2 points to the end of string or to a comma */
7362 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7363 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7364 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7365 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007366 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007367 return;
7368 }
7369
7370 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007371 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007372 continue;
7373 }
7374 }
7375}
7376
7377
Willy Tarreau58f10d72006-12-04 02:26:12 +01007378/*
7379 * Try to retrieve a known appsession in the URI, then the associated server.
7380 * If the server is found, it's assigned to the session.
7381 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007382void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007383{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007384 char *end_params, *first_param, *cur_param, *next_param;
7385 char separator;
7386 int value_len;
7387
7388 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007389
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007390 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007391 (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 +01007392 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007393 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007394
Cyril Bontéb21570a2009-11-29 20:04:48 +01007395 first_param = NULL;
7396 switch (mode) {
7397 case PR_O2_AS_M_PP:
7398 first_param = memchr(begin, ';', len);
7399 break;
7400 case PR_O2_AS_M_QS:
7401 first_param = memchr(begin, '?', len);
7402 break;
7403 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007404
Cyril Bontéb21570a2009-11-29 20:04:48 +01007405 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007406 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007407 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007408
Cyril Bontéb21570a2009-11-29 20:04:48 +01007409 switch (mode) {
7410 case PR_O2_AS_M_PP:
7411 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7412 end_params = (char *) begin + len;
7413 }
7414 separator = ';';
7415 break;
7416 case PR_O2_AS_M_QS:
7417 end_params = (char *) begin + len;
7418 separator = '&';
7419 break;
7420 default:
7421 /* unknown mode, shouldn't happen */
7422 return;
7423 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007424
Cyril Bontéb21570a2009-11-29 20:04:48 +01007425 cur_param = next_param = end_params;
7426 while (cur_param > first_param) {
7427 cur_param--;
7428 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7429 /* let's see if this is the appsession parameter */
7430 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7431 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7432 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7433 /* Cool... it's the right one */
7434 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7435 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7436 if (value_len > 0) {
7437 manage_client_side_appsession(t, cur_param, value_len);
7438 }
7439 break;
7440 }
7441 next_param = cur_param;
7442 }
7443 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007444#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007445 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007446 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007447#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007448}
7449
Willy Tarreaub2513902006-12-17 14:52:38 +01007450/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007451 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007452 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007453 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007454 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007455 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007456 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007457 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007458 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007459int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007460{
7461 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007462 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007463 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007464 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007465
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007466 if (!uri_auth)
7467 return 0;
7468
Cyril Bonté70be45d2010-10-12 00:14:35 +02007469 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007470 return 0;
7471
Willy Tarreau295a8372011-03-10 11:25:07 +01007472 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007473 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007475 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007476 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007477 return 0;
7478
Willy Tarreau3a215be2012-03-09 21:39:51 +01007479 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007480 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007481 return 0;
7482
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007483 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007484 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007485 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007486 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007487 break;
7488 }
7489 h++;
7490 }
7491
7492 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007493 h = uri + uri_auth->uri_len;
7494 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007495 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007496 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007497 break;
7498 }
7499 h++;
7500 }
7501 }
7502
Willy Tarreau3a215be2012-03-09 21:39:51 +01007503 h = uri + uri_auth->uri_len;
7504 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007505 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007506 si->applet.ctx.stats.flags |= STAT_FMT_CSV;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007507 break;
7508 }
7509 h++;
7510 }
7511
Willy Tarreau3a215be2012-03-09 21:39:51 +01007512 h = uri + uri_auth->uri_len;
7513 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007514 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007515 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007516 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007517 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007518 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7519 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7520 si->applet.ctx.stats.st_code = i;
7521 break;
7522 }
7523 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007524 break;
7525 }
7526 h++;
7527 }
7528
Willy Tarreau295a8372011-03-10 11:25:07 +01007529 si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO;
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007530
Willy Tarreaub2513902006-12-17 14:52:38 +01007531 return 1;
7532}
7533
Willy Tarreau4076a152009-04-02 15:18:36 +02007534/*
7535 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007536 * By default it tries to report the error position as msg->err_pos. However if
7537 * this one is not set, it will then report msg->next, which is the last known
7538 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007539 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007540 */
7541void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007542 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007543 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007544{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007545 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007546 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007547
Willy Tarreau9b28e032012-10-12 23:49:43 +02007548 es->len = MIN(chn->buf->i, sizeof(es->buf));
7549 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007550 len1 = MIN(len1, es->len);
7551 len2 = es->len - len1; /* remaining data if buffer wraps */
7552
Willy Tarreau9b28e032012-10-12 23:49:43 +02007553 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007554 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007555 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007556
Willy Tarreau4076a152009-04-02 15:18:36 +02007557 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007558 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007559 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007560 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007561
Willy Tarreau4076a152009-04-02 15:18:36 +02007562 es->when = date; // user-visible date
7563 es->sid = s->uniq_id;
Willy Tarreau827aee92011-03-10 16:55:02 +01007564 es->srv = target_srv(&s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007565 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007566 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007567 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007568 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007569 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007570 es->s_flags = s->flags;
7571 es->t_flags = s->txn.flags;
7572 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007573 es->b_out = chn->buf->o;
7574 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007575 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007576 es->m_clen = msg->chunk_len;
7577 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007578}
Willy Tarreaub2513902006-12-17 14:52:38 +01007579
Willy Tarreau294c4732011-12-16 21:35:50 +01007580/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7581 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7582 * performed over the whole headers. Otherwise it must contain a valid header
7583 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7584 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7585 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7586 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7587 * -1.
7588 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007589 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007590unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007591 struct hdr_idx *idx, int occ,
7592 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007593{
Willy Tarreau294c4732011-12-16 21:35:50 +01007594 struct hdr_ctx local_ctx;
7595 char *ptr_hist[MAX_HDR_HISTORY];
7596 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007597 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007598 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007599
Willy Tarreau294c4732011-12-16 21:35:50 +01007600 if (!ctx) {
7601 local_ctx.idx = 0;
7602 ctx = &local_ctx;
7603 }
7604
Willy Tarreaubce70882009-09-07 11:51:47 +02007605 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007606 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007607 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007608 occ--;
7609 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007610 *vptr = ctx->line + ctx->val;
7611 *vlen = ctx->vlen;
7612 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007613 }
7614 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007615 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007616 }
7617
7618 /* negative occurrence, we scan all the list then walk back */
7619 if (-occ > MAX_HDR_HISTORY)
7620 return 0;
7621
Willy Tarreau294c4732011-12-16 21:35:50 +01007622 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007623 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007624 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7625 len_hist[hist_ptr] = ctx->vlen;
7626 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007627 hist_ptr = 0;
7628 found++;
7629 }
7630 if (-occ > found)
7631 return 0;
7632 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7633 * find occurrence -occ, so we have to check [hist_ptr+occ].
7634 */
7635 hist_ptr += occ;
7636 if (hist_ptr >= MAX_HDR_HISTORY)
7637 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007638 *vptr = ptr_hist[hist_ptr];
7639 *vlen = len_hist[hist_ptr];
7640 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007641}
7642
Willy Tarreaubaaee002006-06-26 02:48:02 +02007643/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007644 * Print a debug line with a header. Always stop at the first CR or LF char,
7645 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7646 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007647 */
7648void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7649{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007650 int max;
7651 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreaufb7508a2012-05-21 16:47:54 +02007652 dir, (unsigned short)si_fd(t->req->prod), (unsigned short)si_fd(t->req->cons));
Willy Tarreaue92693a2012-09-24 21:13:39 +02007653
7654 for (max = 0; start + max < end; max++)
7655 if (start[max] == '\r' || start[max] == '\n')
7656 break;
7657
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007658 UBOUND(max, trash.size - trash.len - 3);
7659 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7660 trash.str[trash.len++] = '\n';
7661 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007662}
7663
Willy Tarreau0937bc42009-12-22 15:03:09 +01007664/*
7665 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7666 * the required fields are properly allocated and that we only need to (re)init
7667 * them. This should be used before processing any new request.
7668 */
7669void http_init_txn(struct session *s)
7670{
7671 struct http_txn *txn = &s->txn;
7672 struct proxy *fe = s->fe;
7673
7674 txn->flags = 0;
7675 txn->status = -1;
7676
William Lallemand5f232402012-04-05 18:02:55 +02007677 global.req_count++;
7678
Willy Tarreauf64d1412010-10-07 20:06:11 +02007679 txn->cookie_first_date = 0;
7680 txn->cookie_last_date = 0;
7681
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007682 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007683 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007684 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007685 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007686 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007687 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007688 txn->req.chunk_len = 0LL;
7689 txn->req.body_len = 0LL;
7690 txn->rsp.chunk_len = 0LL;
7691 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007692 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7693 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007694 txn->req.chn = s->req;
7695 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007696
7697 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007698
7699 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7700 if (fe->options2 & PR_O2_REQBUG_OK)
7701 txn->req.err_pos = -1; /* let buggy requests pass */
7702
Willy Tarreau46023632010-01-07 22:51:47 +01007703 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007704 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7705
Willy Tarreau46023632010-01-07 22:51:47 +01007706 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007707 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7708
7709 if (txn->hdr_idx.v)
7710 hdr_idx_init(&txn->hdr_idx);
7711}
7712
7713/* to be used at the end of a transaction */
7714void http_end_txn(struct session *s)
7715{
7716 struct http_txn *txn = &s->txn;
7717
7718 /* these ones will have been dynamically allocated */
7719 pool_free2(pool2_requri, txn->uri);
7720 pool_free2(pool2_capture, txn->cli_cookie);
7721 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007722 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007723 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007724
William Lallemanda73203e2012-03-12 12:48:57 +01007725 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007726 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007727 txn->uri = NULL;
7728 txn->srv_cookie = NULL;
7729 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007730
7731 if (txn->req.cap) {
7732 struct cap_hdr *h;
7733 for (h = s->fe->req_cap; h; h = h->next)
7734 pool_free2(h->pool, txn->req.cap[h->index]);
7735 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7736 }
7737
7738 if (txn->rsp.cap) {
7739 struct cap_hdr *h;
7740 for (h = s->fe->rsp_cap; h; h = h->next)
7741 pool_free2(h->pool, txn->rsp.cap[h->index]);
7742 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7743 }
7744
Willy Tarreau0937bc42009-12-22 15:03:09 +01007745}
7746
7747/* to be used at the end of a transaction to prepare a new one */
7748void http_reset_txn(struct session *s)
7749{
7750 http_end_txn(s);
7751 http_init_txn(s);
7752
7753 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007754 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007755 session_del_srv_conn(s);
Willy Tarreau9e000c62011-03-10 14:03:36 +01007756 clear_target(&s->target);
Emeric Brunb982a3d2010-01-04 15:45:53 +01007757 /* re-init store persistence */
7758 s->store_count = 0;
7759
Willy Tarreau0937bc42009-12-22 15:03:09 +01007760 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007761
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007762 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007763
Willy Tarreau739cfba2010-01-25 23:11:14 +01007764 /* We must trim any excess data from the response buffer, because we
7765 * may have blocked an invalid response from a server that we don't
7766 * want to accidentely forward once we disable the analysers, nor do
7767 * we want those data to come along with next response. A typical
7768 * example of such data would be from a buggy server responding to
7769 * a HEAD with some data, or sending more than the advertised
7770 * content-length.
7771 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007772 if (unlikely(s->rep->buf->i))
7773 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007774
Willy Tarreau0937bc42009-12-22 15:03:09 +01007775 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007776 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007777
Willy Tarreaud04e8582010-05-31 12:31:35 +02007778 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007779 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007780
7781 s->req->rex = TICK_ETERNITY;
7782 s->req->wex = TICK_ETERNITY;
7783 s->req->analyse_exp = TICK_ETERNITY;
7784 s->rep->rex = TICK_ETERNITY;
7785 s->rep->wex = TICK_ETERNITY;
7786 s->rep->analyse_exp = TICK_ETERNITY;
7787}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007788
Willy Tarreauff011f22011-01-06 17:51:27 +01007789void free_http_req_rules(struct list *r) {
7790 struct http_req_rule *tr, *pr;
7791
7792 list_for_each_entry_safe(pr, tr, r, list) {
7793 LIST_DEL(&pr->list);
7794 if (pr->action == HTTP_REQ_ACT_HTTP_AUTH)
7795 free(pr->http_auth.realm);
7796
7797 free(pr);
7798 }
7799}
7800
7801struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
7802{
7803 struct http_req_rule *rule;
7804 int cur_arg;
7805
7806 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
7807 if (!rule) {
7808 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
7809 return NULL;
7810 }
7811
7812 if (!*args[0]) {
7813 goto req_error_parsing;
7814 } else if (!strcmp(args[0], "allow")) {
7815 rule->action = HTTP_REQ_ACT_ALLOW;
7816 cur_arg = 1;
7817 } else if (!strcmp(args[0], "deny")) {
7818 rule->action = HTTP_REQ_ACT_DENY;
7819 cur_arg = 1;
7820 } else if (!strcmp(args[0], "auth")) {
7821 rule->action = HTTP_REQ_ACT_HTTP_AUTH;
7822 cur_arg = 1;
7823
7824 while(*args[cur_arg]) {
7825 if (!strcmp(args[cur_arg], "realm")) {
7826 rule->http_auth.realm = strdup(args[cur_arg + 1]);
7827 cur_arg+=2;
7828 continue;
7829 } else
7830 break;
7831 }
7832 } else {
7833req_error_parsing:
7834 Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n",
7835 file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]);
7836 return NULL;
7837 }
7838
7839 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
7840 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007841 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01007842
Willy Tarreaub7451bb2012-04-27 12:38:15 +02007843 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
7844 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
7845 file, linenum, args[0], errmsg);
7846 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01007847 return NULL;
7848 }
7849 rule->cond = cond;
7850 }
7851 else if (*args[cur_arg]) {
7852 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
7853 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
7854 file, linenum, args[0], args[cur_arg]);
7855 return NULL;
7856 }
7857
7858 return rule;
7859}
7860
Willy Tarreau8797c062007-05-07 00:55:35 +02007861/************************************************************************/
7862/* The code below is dedicated to ACL parsing and matching */
7863/************************************************************************/
7864
7865
Willy Tarreau14174bc2012-04-16 14:34:04 +02007866/* This function ensures that the prerequisites for an L7 fetch are ready,
7867 * which means that a request or response is ready. If some data is missing,
7868 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02007869 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
7870 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02007871 *
7872 * The function returns :
7873 * 0 if some data is missing or if the requested data cannot be fetched
7874 * -1 if it is certain that we'll never have any HTTP message there
7875 * 1 if an HTTP message is ready
7876 */
7877static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007878acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007879 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007880{
7881 struct http_txn *txn = l7;
7882 struct http_msg *msg = &txn->req;
7883
7884 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
7885 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
7886 */
7887
7888 if (unlikely(!s || !txn))
7889 return 0;
7890
7891 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02007892 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007893
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007894 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02007895 if (unlikely(!s->req))
7896 return 0;
7897
7898 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007899 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007900 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007901 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007902 return -1;
7903 }
7904
7905 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007906 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02007907 http_msg_analyzer(msg, &txn->hdr_idx);
7908
7909 /* Still no valid request ? */
7910 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02007911 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02007912 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007913 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007914 return -1;
7915 }
7916 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02007917 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007918 return 0;
7919 }
7920
7921 /* OK we just got a valid HTTP request. We have some minor
7922 * preparation to perform so that further checks can rely
7923 * on HTTP tests.
7924 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007925 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02007926 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
7927 s->flags |= SN_REDIRECTABLE;
7928
7929 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02007930 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02007931 return -1;
7932 }
7933 }
7934
Willy Tarreau24e32d82012-04-23 23:55:44 +02007935 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02007936 return 0; /* data might have moved and indexes changed */
7937
7938 /* otherwise everything's ready for the request */
7939 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02007940 else {
7941 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02007942 if (txn->rsp.msg_state < HTTP_MSG_BODY)
7943 return 0;
7944 }
7945
7946 /* everything's OK */
7947 return 1;
7948}
Willy Tarreau8797c062007-05-07 00:55:35 +02007949
Willy Tarreauc0239e02012-04-16 14:42:55 +02007950#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007951 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 +02007952
Willy Tarreau24e32d82012-04-23 23:55:44 +02007953#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007954 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 +02007955
Willy Tarreau8797c062007-05-07 00:55:35 +02007956
7957/* 1. Check on METHOD
7958 * We use the pre-parsed method if it is known, and store its number as an
7959 * integer. If it is unknown, we use the pointer and the length.
7960 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007961static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02007962{
7963 int len, meth;
7964
Willy Tarreauae8b7962007-06-09 23:10:04 +02007965 len = strlen(*text);
7966 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02007967
7968 pattern->val.i = meth;
7969 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02007970 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007971 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02007972 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02007973 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02007974 }
Willy Tarreau8797c062007-05-07 00:55:35 +02007975 pattern->len = len;
7976 }
7977 return 1;
7978}
7979
Willy Tarreau8e5e9552011-12-16 15:38:49 +01007980/* This function fetches the method of current HTTP request and stores
7981 * it in the global pattern struct as a chunk. There are two possibilities :
7982 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
7983 * in <len> and <ptr> is NULL ;
7984 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
7985 * <len> to its length.
7986 * This is intended to be used with acl_match_meth() only.
7987 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02007988static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007989acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02007990 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02007991{
7992 int meth;
7993 struct http_txn *txn = l7;
7994
Willy Tarreau24e32d82012-04-23 23:55:44 +02007995 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02007996
Willy Tarreau8797c062007-05-07 00:55:35 +02007997 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02007998 smp->type = SMP_T_UINT;
7999 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008000 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008001 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8002 /* ensure the indexes are not affected */
8003 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008004 smp->type = SMP_T_CSTR;
8005 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008006 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008007 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008008 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008009 return 1;
8010}
8011
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008012/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008013static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008014{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008015 int icase;
8016
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008017
Willy Tarreauf853c462012-04-23 18:53:56 +02008018 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008019 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008020 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008021 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008022 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008023 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008024
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008025 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8026 if (pattern->val.i != HTTP_METH_OTHER)
8027 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008028
8029 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008030 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008031 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008032
8033 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008034 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8035 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008036 return ACL_PAT_FAIL;
8037 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008038}
8039
8040/* 2. Check on Request/Status Version
8041 * We simply compare strings here.
8042 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008043static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008044{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008045 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008046 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008047 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008048 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008049 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008050 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008051 return 1;
8052}
8053
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008054static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008055acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008056 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008057{
8058 struct http_txn *txn = l7;
8059 char *ptr;
8060 int len;
8061
Willy Tarreauc0239e02012-04-16 14:42:55 +02008062 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008063
Willy Tarreau8797c062007-05-07 00:55:35 +02008064 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008065 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008066
8067 while ((len-- > 0) && (*ptr++ != '/'));
8068 if (len <= 0)
8069 return 0;
8070
Willy Tarreauf853c462012-04-23 18:53:56 +02008071 smp->type = SMP_T_CSTR;
8072 smp->data.str.str = ptr;
8073 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008074
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008075 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008076 return 1;
8077}
8078
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008079static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008080acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008081 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008082{
8083 struct http_txn *txn = l7;
8084 char *ptr;
8085 int len;
8086
Willy Tarreauc0239e02012-04-16 14:42:55 +02008087 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008088
Willy Tarreau8797c062007-05-07 00:55:35 +02008089 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008090 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008091
8092 while ((len-- > 0) && (*ptr++ != '/'));
8093 if (len <= 0)
8094 return 0;
8095
Willy Tarreauf853c462012-04-23 18:53:56 +02008096 smp->type = SMP_T_CSTR;
8097 smp->data.str.str = ptr;
8098 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008099
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008100 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008101 return 1;
8102}
8103
8104/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008105static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008106acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008107 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008108{
8109 struct http_txn *txn = l7;
8110 char *ptr;
8111 int len;
8112
Willy Tarreauc0239e02012-04-16 14:42:55 +02008113 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008114
Willy Tarreau8797c062007-05-07 00:55:35 +02008115 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008116 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008117
Willy Tarreauf853c462012-04-23 18:53:56 +02008118 smp->type = SMP_T_UINT;
8119 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008120 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008121 return 1;
8122}
8123
8124/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008125static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008126smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008127 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008128{
8129 struct http_txn *txn = l7;
8130
Willy Tarreauc0239e02012-04-16 14:42:55 +02008131 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008132
Willy Tarreauf853c462012-04-23 18:53:56 +02008133 smp->type = SMP_T_CSTR;
8134 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008135 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008136 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008137 return 1;
8138}
8139
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008140static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008141smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008142 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008143{
8144 struct http_txn *txn = l7;
8145
Willy Tarreauc0239e02012-04-16 14:42:55 +02008146 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008147
8148 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008149 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8150 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008151 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008152 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008153 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008154
8155 /*
8156 * If we are parsing url in frontend space, we prepare backend stage
8157 * to not parse again the same url ! optimization lazyness...
8158 */
8159 if (px->options & PR_O_HTTP_PROXY)
8160 l4->flags |= SN_ADDR_SET;
8161
Willy Tarreau37406352012-04-23 16:16:37 +02008162 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008163 return 1;
8164}
8165
8166static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008167smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008168 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008169{
8170 struct http_txn *txn = l7;
8171
Willy Tarreauc0239e02012-04-16 14:42:55 +02008172 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008173
8174 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008175 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 +02008176 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008177 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008178
8179 if (px->options & PR_O_HTTP_PROXY)
8180 l4->flags |= SN_ADDR_SET;
8181
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008182 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008183 return 1;
8184}
8185
Willy Tarreau185b5c42012-04-26 15:11:51 +02008186/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8187 * Accepts an optional argument of type string containing the header field name,
8188 * and an optional argument of type signed or unsigned integer to request an
8189 * explicit occurrence of the header. Note that in the event of a missing name,
8190 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008191 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008192static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008193smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008194 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008195{
8196 struct http_txn *txn = l7;
8197 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008198 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008199 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008200 int occ = 0;
8201 const char *name_str = NULL;
8202 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008203
Willy Tarreau185b5c42012-04-26 15:11:51 +02008204 if (args) {
8205 if (args[0].type != ARGT_STR)
8206 return 0;
8207 name_str = args[0].data.str.str;
8208 name_len = args[0].data.str.len;
8209
8210 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8211 occ = args[1].data.uint;
8212 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008213
Willy Tarreaue333ec92012-04-16 16:26:40 +02008214 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008215
Willy Tarreau185b5c42012-04-26 15:11:51 +02008216 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008217 /* search for header from the beginning */
8218 ctx->idx = 0;
8219
Willy Tarreau185b5c42012-04-26 15:11:51 +02008220 if (!occ && !(opt & SMP_OPT_ITERATE))
8221 /* no explicit occurrence and single fetch => last header by default */
8222 occ = -1;
8223
8224 if (!occ)
8225 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008226 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008227
Willy Tarreau185b5c42012-04-26 15:11:51 +02008228 smp->type = SMP_T_CSTR;
8229 smp->flags |= SMP_F_VOL_HDR;
8230 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 +02008231 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008232
Willy Tarreau37406352012-04-23 16:16:37 +02008233 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008234 return 0;
8235}
8236
Willy Tarreauc11416f2007-06-17 16:58:38 +02008237/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008238 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008239 */
8240static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008241smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008242 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008243{
8244 struct http_txn *txn = l7;
8245 struct hdr_idx *idx = &txn->hdr_idx;
8246 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008247 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008248 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008249
Willy Tarreau24e32d82012-04-23 23:55:44 +02008250 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008251 return 0;
8252
Willy Tarreaue333ec92012-04-16 16:26:40 +02008253 CHECK_HTTP_MESSAGE_FIRST();
8254
Willy Tarreau33a7e692007-06-10 19:45:56 +02008255 ctx.idx = 0;
8256 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008257 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 +02008258 cnt++;
8259
Willy Tarreauf853c462012-04-23 18:53:56 +02008260 smp->type = SMP_T_UINT;
8261 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008262 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008263 return 1;
8264}
8265
Willy Tarreau185b5c42012-04-26 15:11:51 +02008266/* Fetch an HTTP header's integer value. The integer value is returned. It
8267 * takes a mandatory argument of type string and an optional one of type int
8268 * to designate a specific occurrence. It returns an unsigned integer, which
8269 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008270 */
8271static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008272smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008273 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008274{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008275 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008276
Willy Tarreauf853c462012-04-23 18:53:56 +02008277 if (ret > 0) {
8278 smp->type = SMP_T_UINT;
8279 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8280 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008281
Willy Tarreaud53e2422012-04-16 17:21:11 +02008282 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008283}
8284
Cyril Bonté69fa9922012-10-25 00:01:06 +02008285/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8286 * and an optional one of type int to designate a specific occurrence.
8287 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008288 */
8289static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008290smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008291 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008292{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008293 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008294
Willy Tarreau185b5c42012-04-26 15:11:51 +02008295 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008296 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8297 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008298 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008299 } else {
8300 struct chunk *temp = sample_get_trash_chunk();
8301 if (smp->data.str.len < temp->size - 1) {
8302 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8303 temp->str[smp->data.str.len] = '\0';
8304 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8305 smp->type = SMP_T_IPV6;
8306 break;
8307 }
8308 }
8309 }
8310
Willy Tarreaud53e2422012-04-16 17:21:11 +02008311 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008312 if (!(smp->flags & SMP_F_NOT_LAST))
8313 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008314 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008315 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008316}
8317
Willy Tarreau737b0c12007-06-10 21:28:46 +02008318/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8319 * the first '/' after the possible hostname, and ends before the possible '?'.
8320 */
8321static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008322smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008323 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008324{
8325 struct http_txn *txn = l7;
8326 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008327
Willy Tarreauc0239e02012-04-16 14:42:55 +02008328 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008329
Willy Tarreau9b28e032012-10-12 23:49:43 +02008330 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008331 ptr = http_get_path(txn);
8332 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008333 return 0;
8334
8335 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008336 smp->type = SMP_T_CSTR;
8337 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008338
8339 while (ptr < end && *ptr != '?')
8340 ptr++;
8341
Willy Tarreauf853c462012-04-23 18:53:56 +02008342 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008343 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008344 return 1;
8345}
8346
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008347/* This produces a concatenation of the first occurrence of the Host header
8348 * followed by the path component if it begins with a slash ('/'). This means
8349 * that '*' will not be added, resulting in exactly the first Host entry.
8350 * If no Host header is found, then the path is returned as-is. The returned
8351 * value is stored in the trash so it does not need to be marked constant.
8352 */
8353static int
8354smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8355 const struct arg *args, struct sample *smp)
8356{
8357 struct http_txn *txn = l7;
8358 char *ptr, *end, *beg;
8359 struct hdr_ctx ctx;
8360
8361 CHECK_HTTP_MESSAGE_FIRST();
8362
8363 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008364 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 +02008365 !ctx.vlen)
8366 return smp_fetch_path(px, l4, l7, opt, args, smp);
8367
8368 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008369 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008370 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008371 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008372 smp->data.str.len = ctx.vlen;
8373
8374 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008375 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 +02008376 beg = http_get_path(txn);
8377 if (!beg)
8378 beg = end;
8379
8380 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8381
8382 if (beg < ptr && *beg == '/') {
8383 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8384 smp->data.str.len += ptr - beg;
8385 }
8386
8387 smp->flags = SMP_F_VOL_1ST;
8388 return 1;
8389}
8390
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008391static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008392acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008393 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008394{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008395 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8396 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8397 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008398
Willy Tarreau24e32d82012-04-23 23:55:44 +02008399 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008400
Willy Tarreauf853c462012-04-23 18:53:56 +02008401 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008402 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008403 return 1;
8404}
8405
Willy Tarreau7f18e522010-10-22 20:04:13 +02008406/* return a valid test if the current request is the first one on the connection */
8407static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008408acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008409 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008410{
8411 if (!s)
8412 return 0;
8413
Willy Tarreauf853c462012-04-23 18:53:56 +02008414 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008415 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008416 return 1;
8417}
8418
Willy Tarreau34db1082012-04-19 17:16:54 +02008419/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008420static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008421acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008422 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008423{
8424
Willy Tarreau24e32d82012-04-23 23:55:44 +02008425 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008426 return 0;
8427
Willy Tarreauc0239e02012-04-16 14:42:55 +02008428 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008429
Willy Tarreauc0239e02012-04-16 14:42:55 +02008430 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008431 return 0;
8432
Willy Tarreauf853c462012-04-23 18:53:56 +02008433 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008434 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 +01008435 return 1;
8436}
Willy Tarreau8797c062007-05-07 00:55:35 +02008437
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008438/* Accepts exactly 1 argument of type userlist */
8439static int
8440acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8441 const struct arg *args, struct sample *smp)
8442{
8443
8444 if (!args || args->type != ARGT_USR)
8445 return 0;
8446
8447 CHECK_HTTP_MESSAGE_FIRST();
8448
8449 if (!get_http_auth(l4))
8450 return 0;
8451
8452 /* acl_match_auth() will need several information at once */
8453 smp->ctx.a[0] = args->data.usr; /* user list */
8454 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8455 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8456
8457 /* if the user does not belong to the userlist or has a wrong password,
8458 * report that it unconditionally does not match. Otherwise we return
8459 * a non-zero integer which will be ignored anyway since all the params
8460 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8461 */
8462 smp->type = SMP_T_BOOL;
8463 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8464 if (smp->data.uint)
8465 smp->type = SMP_T_UINT;
8466
8467 return 1;
8468}
8469
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008470/* Try to find the next occurrence of a cookie name in a cookie header value.
8471 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8472 * the cookie value is returned into *value and *value_l, and the function
8473 * returns a pointer to the next pointer to search from if the value was found.
8474 * Otherwise if the cookie was not found, NULL is returned and neither value
8475 * nor value_l are touched. The input <hdr> string should first point to the
8476 * header's value, and the <hdr_end> pointer must point to the first character
8477 * not part of the value. <list> must be non-zero if value may represent a list
8478 * of values (cookie headers). This makes it faster to abort parsing when no
8479 * list is expected.
8480 */
8481static char *
8482extract_cookie_value(char *hdr, const char *hdr_end,
8483 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008484 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008485{
8486 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8487 char *next;
8488
8489 /* we search at least a cookie name followed by an equal, and more
8490 * generally something like this :
8491 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8492 */
8493 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8494 /* Iterate through all cookies on this line */
8495
8496 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8497 att_beg++;
8498
8499 /* find att_end : this is the first character after the last non
8500 * space before the equal. It may be equal to hdr_end.
8501 */
8502 equal = att_end = att_beg;
8503
8504 while (equal < hdr_end) {
8505 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8506 break;
8507 if (http_is_spht[(unsigned char)*equal++])
8508 continue;
8509 att_end = equal;
8510 }
8511
8512 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8513 * is between <att_beg> and <equal>, both may be identical.
8514 */
8515
8516 /* look for end of cookie if there is an equal sign */
8517 if (equal < hdr_end && *equal == '=') {
8518 /* look for the beginning of the value */
8519 val_beg = equal + 1;
8520 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8521 val_beg++;
8522
8523 /* find the end of the value, respecting quotes */
8524 next = find_cookie_value_end(val_beg, hdr_end);
8525
8526 /* make val_end point to the first white space or delimitor after the value */
8527 val_end = next;
8528 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8529 val_end--;
8530 } else {
8531 val_beg = val_end = next = equal;
8532 }
8533
8534 /* We have nothing to do with attributes beginning with '$'. However,
8535 * they will automatically be removed if a header before them is removed,
8536 * since they're supposed to be linked together.
8537 */
8538 if (*att_beg == '$')
8539 continue;
8540
8541 /* Ignore cookies with no equal sign */
8542 if (equal == next)
8543 continue;
8544
8545 /* Now we have the cookie name between att_beg and att_end, and
8546 * its value between val_beg and val_end.
8547 */
8548
8549 if (att_end - att_beg == cookie_name_l &&
8550 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8551 /* let's return this value and indicate where to go on from */
8552 *value = val_beg;
8553 *value_l = val_end - val_beg;
8554 return next + 1;
8555 }
8556
8557 /* Set-Cookie headers only have the name in the first attr=value part */
8558 if (!list)
8559 break;
8560 }
8561
8562 return NULL;
8563}
8564
Willy Tarreaue333ec92012-04-16 16:26:40 +02008565/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008566 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8567 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008568 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008569 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008570 * Accepts exactly 1 argument of type string. If the input options indicate
8571 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008572 */
8573static int
Willy Tarreau51539362012-05-08 12:46:28 +02008574smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8575 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008576{
8577 struct http_txn *txn = l7;
8578 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008579 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008580 const struct http_msg *msg;
8581 const char *hdr_name;
8582 int hdr_name_len;
8583 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008584 int occ = 0;
8585 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008586
Willy Tarreau24e32d82012-04-23 23:55:44 +02008587 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008588 return 0;
8589
Willy Tarreaue333ec92012-04-16 16:26:40 +02008590 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008591
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008592 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008593 msg = &txn->req;
8594 hdr_name = "Cookie";
8595 hdr_name_len = 6;
8596 } else {
8597 msg = &txn->rsp;
8598 hdr_name = "Set-Cookie";
8599 hdr_name_len = 10;
8600 }
8601
Willy Tarreau28376d62012-04-26 21:26:10 +02008602 if (!occ && !(opt & SMP_OPT_ITERATE))
8603 /* no explicit occurrence and single fetch => last cookie by default */
8604 occ = -1;
8605
8606 /* OK so basically here, either we want only one value and it's the
8607 * last one, or we want to iterate over all of them and we fetch the
8608 * next one.
8609 */
8610
Willy Tarreau9b28e032012-10-12 23:49:43 +02008611 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008612 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008613 /* search for the header from the beginning, we must first initialize
8614 * the search parameters.
8615 */
Willy Tarreau37406352012-04-23 16:16:37 +02008616 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008617 ctx->idx = 0;
8618 }
8619
Willy Tarreau28376d62012-04-26 21:26:10 +02008620 smp->flags |= SMP_F_VOL_HDR;
8621
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008622 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008623 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8624 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008625 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8626 goto out;
8627
Willy Tarreau24e32d82012-04-23 23:55:44 +02008628 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008629 continue;
8630
Willy Tarreau37406352012-04-23 16:16:37 +02008631 smp->ctx.a[0] = ctx->line + ctx->val;
8632 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008633 }
8634
Willy Tarreauf853c462012-04-23 18:53:56 +02008635 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008636 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008637 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008638 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008639 &smp->data.str.str,
8640 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008641 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008642 found = 1;
8643 if (occ >= 0) {
8644 /* one value was returned into smp->data.str.{str,len} */
8645 smp->flags |= SMP_F_NOT_LAST;
8646 return 1;
8647 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008648 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008649 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008650 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008651 /* all cookie headers and values were scanned. If we're looking for the
8652 * last occurrence, we may return it now.
8653 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008654 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008655 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008656 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008657}
8658
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008659/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008660 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008661 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008662 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008663 */
8664static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008665acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008666 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008667{
8668 struct http_txn *txn = l7;
8669 struct hdr_idx *idx = &txn->hdr_idx;
8670 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008671 const struct http_msg *msg;
8672 const char *hdr_name;
8673 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008674 int cnt;
8675 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008676 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008677
Willy Tarreau24e32d82012-04-23 23:55:44 +02008678 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008679 return 0;
8680
Willy Tarreaue333ec92012-04-16 16:26:40 +02008681 CHECK_HTTP_MESSAGE_FIRST();
8682
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008683 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008684 msg = &txn->req;
8685 hdr_name = "Cookie";
8686 hdr_name_len = 6;
8687 } else {
8688 msg = &txn->rsp;
8689 hdr_name = "Set-Cookie";
8690 hdr_name_len = 10;
8691 }
8692
Willy Tarreau9b28e032012-10-12 23:49:43 +02008693 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02008694 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008695 ctx.idx = 0;
8696 cnt = 0;
8697
8698 while (1) {
8699 /* Note: val_beg == NULL every time we need to fetch a new header */
8700 if (!val_beg) {
8701 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
8702 break;
8703
Willy Tarreau24e32d82012-04-23 23:55:44 +02008704 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008705 continue;
8706
8707 val_beg = ctx.line + ctx.val;
8708 val_end = val_beg + ctx.vlen;
8709 }
8710
Willy Tarreauf853c462012-04-23 18:53:56 +02008711 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008712 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008713 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008714 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008715 &smp->data.str.str,
8716 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008717 cnt++;
8718 }
8719 }
8720
Willy Tarreauf853c462012-04-23 18:53:56 +02008721 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008722 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008723 return 1;
8724}
8725
Willy Tarreau51539362012-05-08 12:46:28 +02008726/* Fetch an cookie's integer value. The integer value is returned. It
8727 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
8728 */
8729static int
8730smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8731 const struct arg *args, struct sample *smp)
8732{
8733 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
8734
8735 if (ret > 0) {
8736 smp->type = SMP_T_UINT;
8737 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8738 }
8739
8740 return ret;
8741}
8742
Willy Tarreau8797c062007-05-07 00:55:35 +02008743/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02008744/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02008745/************************************************************************/
8746
David Cournapeau16023ee2010-12-23 20:55:41 +09008747/*
8748 * Given a path string and its length, find the position of beginning of the
8749 * query string. Returns NULL if no query string is found in the path.
8750 *
8751 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
8752 *
8753 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
8754 */
bedis4c75cca2012-10-05 08:38:24 +02008755static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008756{
8757 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02008758
bedis4c75cca2012-10-05 08:38:24 +02008759 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09008760 return p ? p + 1 : NULL;
8761}
8762
bedis4c75cca2012-10-05 08:38:24 +02008763static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008764{
bedis4c75cca2012-10-05 08:38:24 +02008765 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09008766}
8767
8768/*
8769 * Given a url parameter, find the starting position of the first occurence,
8770 * or NULL if the parameter is not found.
8771 *
8772 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
8773 * the function will return query_string+8.
8774 */
8775static char*
8776find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02008777 char* url_param_name, size_t url_param_name_l,
8778 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008779{
8780 char *pos, *last;
8781
8782 pos = query_string;
8783 last = query_string + query_string_l - url_param_name_l - 1;
8784
8785 while (pos <= last) {
8786 if (pos[url_param_name_l] == '=') {
8787 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
8788 return pos;
8789 pos += url_param_name_l + 1;
8790 }
bedis4c75cca2012-10-05 08:38:24 +02008791 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008792 pos++;
8793 pos++;
8794 }
8795 return NULL;
8796}
8797
8798/*
8799 * Given a url parameter name, returns its value and size into *value and
8800 * *value_l respectively, and returns non-zero. If the parameter is not found,
8801 * zero is returned and value/value_l are not touched.
8802 */
8803static int
8804find_url_param_value(char* path, size_t path_l,
8805 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02008806 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09008807{
8808 char *query_string, *qs_end;
8809 char *arg_start;
8810 char *value_start, *value_end;
8811
bedis4c75cca2012-10-05 08:38:24 +02008812 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008813 if (!query_string)
8814 return 0;
8815
8816 qs_end = path + path_l;
8817 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02008818 url_param_name, url_param_name_l,
8819 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09008820 if (!arg_start)
8821 return 0;
8822
8823 value_start = arg_start + url_param_name_l + 1;
8824 value_end = value_start;
8825
bedis4c75cca2012-10-05 08:38:24 +02008826 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008827 value_end++;
8828
8829 *value = value_start;
8830 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01008831 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09008832}
8833
8834static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008835smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8836 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09008837{
bedis4c75cca2012-10-05 08:38:24 +02008838 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09008839 struct http_txn *txn = l7;
8840 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008841
bedis4c75cca2012-10-05 08:38:24 +02008842 if (!args || args[0].type != ARGT_STR ||
8843 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008844 return 0;
8845
8846 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09008847
bedis4c75cca2012-10-05 08:38:24 +02008848 if (args[1].type)
8849 delim = *args[1].data.str.str;
8850
Willy Tarreau9b28e032012-10-12 23:49:43 +02008851 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02008852 args->data.str.str, args->data.str.len,
8853 &smp->data.str.str, &smp->data.str.len,
8854 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09008855 return 0;
8856
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02008857 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008858 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09008859 return 1;
8860}
8861
Willy Tarreaua9fddca2012-07-31 07:51:48 +02008862/* Return the signed integer value for the specified url parameter (see url_param
8863 * above).
8864 */
8865static int
8866smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8867 const struct arg *args, struct sample *smp)
8868{
8869 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
8870
8871 if (ret > 0) {
8872 smp->type = SMP_T_UINT;
8873 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8874 }
8875
8876 return ret;
8877}
8878
Willy Tarreau185b5c42012-04-26 15:11:51 +02008879/* This function is used to validate the arguments passed to any "hdr" fetch
8880 * keyword. These keywords support an optional positive or negative occurrence
8881 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
8882 * is assumed that the types are already the correct ones. Returns 0 on error,
8883 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
8884 * error message in case of error, that the caller is responsible for freeing.
8885 * The initial location must either be freeable or NULL.
8886 */
8887static int val_hdr(struct arg *arg, char **err_msg)
8888{
8889 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008890 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02008891 return 0;
8892 }
8893 return 1;
8894}
8895
Willy Tarreau4a568972010-05-12 08:08:50 +02008896/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008897/* All supported ACL keywords must be declared here. */
8898/************************************************************************/
8899
8900/* Note: must not be declared <const> as its list will be overwritten.
8901 * Please take care of keeping this list alphabetically sorted.
8902 */
8903static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008904 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8905 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8906 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8907 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8908 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8909 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8910 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8911 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
8912
Willy Tarreau51539362012-05-08 12:46:28 +02008913 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8914 { "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 +02008915 { "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 +02008916 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8917 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8918 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8919 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8920 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8921 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8922 { "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 +02008923
Willy Tarreau185b5c42012-04-26 15:11:51 +02008924 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8925 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8926 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
8927 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8928 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8929 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8930 { "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 },
8931 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8932 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8933 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8934 { "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 +02008935
8936 { "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 +02008937 { "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 +02008938 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8939
8940 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
8941
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008942 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8943 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8944 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8945 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8946 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8947 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8948 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8949 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008950
8951 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
8952 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8953 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
8954
Willy Tarreau51539362012-05-08 12:46:28 +02008955 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
8956 { "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 +02008957 { "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 +02008958 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8959 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8960 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8961 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8962 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8963 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8964 { "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 +02008965
Willy Tarreau185b5c42012-04-26 15:11:51 +02008966 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
8967 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8968 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
8969 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8970 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8971 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8972 { "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 },
8973 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8974 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8975 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
8976 { "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 +02008977
8978 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
8979
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008980 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8981 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
8982 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
8983 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
8984 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
8985 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
8986 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
8987 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
8988 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
8989 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02008990
8991 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8992 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8993 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8994 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8995 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8996 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
8997 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8998 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
8999 { "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 +02009000 { "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 +02009001
9002 { NULL, NULL, NULL, NULL },
9003}};
9004
9005/************************************************************************/
9006/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009007/************************************************************************/
9008/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009009static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009010 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9011 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9012 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9013 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9014 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
9015 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
9016 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9017 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
9018 { "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 +02009019 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009020}};
9021
Willy Tarreau8797c062007-05-07 00:55:35 +02009022
9023__attribute__((constructor))
9024static void __http_protocol_init(void)
9025{
9026 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009027 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009028}
9029
9030
Willy Tarreau58f10d72006-12-04 02:26:12 +01009031/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009032 * Local variables:
9033 * c-indent-level: 8
9034 * c-basic-offset: 8
9035 * End:
9036 */