blob: 69ef88136f10c7939f4ac98bbdbeeade8c85d92c [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);
William Lallemanda73203e2012-03-12 12:48:57 +0100272 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100273}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274
Willy Tarreau53b6c742006-12-17 13:37:46 +0100275/*
276 * We have 26 list of methods (1 per first letter), each of which can have
277 * up to 3 entries (2 valid, 1 null).
278 */
279struct http_method_desc {
280 http_meth_t meth;
281 int len;
282 const char text[8];
283};
284
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100285const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100286 ['C' - 'A'] = {
287 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
288 },
289 ['D' - 'A'] = {
290 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
291 },
292 ['G' - 'A'] = {
293 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
294 },
295 ['H' - 'A'] = {
296 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
297 },
298 ['P' - 'A'] = {
299 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
300 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
301 },
302 ['T' - 'A'] = {
303 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
304 },
305 /* rest is empty like this :
306 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
307 */
308};
309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100310/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200311 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100312 * RFC2616 for those chars.
313 */
314
315const char http_is_spht[256] = {
316 [' '] = 1, ['\t'] = 1,
317};
318
319const char http_is_crlf[256] = {
320 ['\r'] = 1, ['\n'] = 1,
321};
322
323const char http_is_lws[256] = {
324 [' '] = 1, ['\t'] = 1,
325 ['\r'] = 1, ['\n'] = 1,
326};
327
328const char http_is_sep[256] = {
329 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
330 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
331 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
332 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
333 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
334};
335
336const char http_is_ctl[256] = {
337 [0 ... 31] = 1,
338 [127] = 1,
339};
340
341/*
342 * A token is any ASCII char that is neither a separator nor a CTL char.
343 * Do not overwrite values in assignment since gcc-2.95 will not handle
344 * them correctly. Instead, define every non-CTL char's status.
345 */
346const char http_is_token[256] = {
347 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
348 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
349 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
350 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
351 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
352 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
353 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
354 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
355 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
356 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
357 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
358 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
359 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
360 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
361 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
362 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
363 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
364 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
365 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
366 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
367 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
368 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
369 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
370 ['|'] = 1, ['}'] = 0, ['~'] = 1,
371};
372
373
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100374/*
375 * An http ver_token is any ASCII which can be found in an HTTP version,
376 * which includes 'H', 'T', 'P', '/', '.' and any digit.
377 */
378const char http_is_ver_token[256] = {
379 ['.'] = 1, ['/'] = 1,
380 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
381 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
382 ['H'] = 1, ['P'] = 1, ['T'] = 1,
383};
384
385
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100386/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100387 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
388 */
389#if defined(DEBUG_FSM)
390static void http_silent_debug(int line, struct session *s)
391{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100392 chunk_printf(&trash,
393 "[%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",
394 line,
395 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
396 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);
397 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100398
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100399 chunk_printf(&trash,
400 " %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",
401 line,
402 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
403 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);
404 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100405}
406#else
407#define http_silent_debug(l,s) do { } while (0)
408#endif
409
410/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100411 * Adds a header and its CRLF at the tail of the message's buffer, just before
412 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100413 * The header is also automatically added to the index <hdr_idx>, and the end
414 * of headers is automatically adjusted. The number of bytes added is returned
415 * on success, otherwise <0 is returned indicating an error.
416 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100417int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100418{
419 int bytes, len;
420
421 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200422 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100423 if (!bytes)
424 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100425 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100426 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
427}
428
429/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100430 * Adds a header and its CRLF at the tail of the message's buffer, just before
431 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100432 * the buffer is only opened and the space reserved, but nothing is copied.
433 * The header is also automatically added to the index <hdr_idx>, and the end
434 * of headers is automatically adjusted. The number of bytes added is returned
435 * on success, otherwise <0 is returned indicating an error.
436 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100437int http_header_add_tail2(struct http_msg *msg,
438 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100439{
440 int bytes;
441
Willy Tarreau9b28e032012-10-12 23:49:43 +0200442 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100443 if (!bytes)
444 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100445 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100446 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
447}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448
449/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100450 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
451 * If so, returns the position of the first non-space character relative to
452 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
453 * to return a pointer to the place after the first space. Returns 0 if the
454 * header name does not match. Checks are case-insensitive.
455 */
456int http_header_match2(const char *hdr, const char *end,
457 const char *name, int len)
458{
459 const char *val;
460
461 if (hdr + len >= end)
462 return 0;
463 if (hdr[len] != ':')
464 return 0;
465 if (strncasecmp(hdr, name, len) != 0)
466 return 0;
467 val = hdr + len + 1;
468 while (val < end && HTTP_IS_SPHT(*val))
469 val++;
470 if ((val >= end) && (len + 2 <= end - hdr))
471 return len + 2; /* we may replace starting from second space */
472 return val - hdr;
473}
474
Willy Tarreau68085d82010-01-18 14:54:04 +0100475/* Find the end of the header value contained between <s> and <e>. See RFC2616,
476 * par 2.2 for more information. Note that it requires a valid header to return
477 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200478 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100479char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200480{
481 int quoted, qdpair;
482
483 quoted = qdpair = 0;
484 for (; s < e; s++) {
485 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200486 else if (quoted) {
487 if (*s == '\\') qdpair = 1;
488 else if (*s == '"') quoted = 0;
489 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200490 else if (*s == '"') quoted = 1;
491 else if (*s == ',') return s;
492 }
493 return s;
494}
495
496/* Find the first or next occurrence of header <name> in message buffer <sol>
497 * using headers index <idx>, and return it in the <ctx> structure. This
498 * structure holds everything necessary to use the header and find next
499 * occurrence. If its <idx> member is 0, the header is searched from the
500 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100501 * 1 when it finds a value, and 0 when there is no more. It is designed to work
502 * with headers defined as comma-separated lists. As a special case, if ctx->val
503 * is NULL when searching for a new values of a header, the current header is
504 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200505 */
506int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100507 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200508 struct hdr_ctx *ctx)
509{
Willy Tarreau68085d82010-01-18 14:54:04 +0100510 char *eol, *sov;
511 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200512
Willy Tarreau68085d82010-01-18 14:54:04 +0100513 cur_idx = ctx->idx;
514 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200515 /* We have previously returned a value, let's search
516 * another one on the same line.
517 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200518 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200519 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100520 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200521 eol = sol + idx->v[cur_idx].len;
522
523 if (sov >= eol)
524 /* no more values in this header */
525 goto next_hdr;
526
Willy Tarreau68085d82010-01-18 14:54:04 +0100527 /* values remaining for this header, skip the comma but save it
528 * for later use (eg: for header deletion).
529 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200530 sov++;
531 while (sov < eol && http_is_lws[(unsigned char)*sov])
532 sov++;
533
534 goto return_hdr;
535 }
536
537 /* first request for this header */
538 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100539 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200540 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200541 while (cur_idx) {
542 eol = sol + idx->v[cur_idx].len;
543
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200544 if (len == 0) {
545 /* No argument was passed, we want any header.
546 * To achieve this, we simply build a fake request. */
547 while (sol + len < eol && sol[len] != ':')
548 len++;
549 name = sol;
550 }
551
Willy Tarreau33a7e692007-06-10 19:45:56 +0200552 if ((len < eol - sol) &&
553 (sol[len] == ':') &&
554 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100555 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200556 sov = sol + len + 1;
557 while (sov < eol && http_is_lws[(unsigned char)*sov])
558 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100559
Willy Tarreau33a7e692007-06-10 19:45:56 +0200560 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100561 ctx->prev = old_idx;
562 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200563 ctx->idx = cur_idx;
564 ctx->val = sov - sol;
565
566 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200567 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200568 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200569 eol--;
570 ctx->tws++;
571 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 ctx->vlen = eol - sov;
573 return 1;
574 }
575 next_hdr:
576 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100577 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200578 cur_idx = idx->v[cur_idx].next;
579 }
580 return 0;
581}
582
583int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100584 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200585 struct hdr_ctx *ctx)
586{
587 return http_find_header2(name, strlen(name), sol, idx, ctx);
588}
589
Willy Tarreau68085d82010-01-18 14:54:04 +0100590/* Remove one value of a header. This only works on a <ctx> returned by one of
591 * the http_find_header functions. The value is removed, as well as surrounding
592 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100593 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100594 * message <msg>. The new index is returned. If it is zero, it means there is
595 * no more header, so any processing may stop. The ctx is always left in a form
596 * that can be handled by http_find_header2() to find next occurrence.
597 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100598int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100599{
600 int cur_idx = ctx->idx;
601 char *sol = ctx->line;
602 struct hdr_idx_elem *hdr;
603 int delta, skip_comma;
604
605 if (!cur_idx)
606 return 0;
607
608 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200609 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100610 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200611 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100612 http_msg_move_end(msg, delta);
613 idx->used--;
614 hdr->len = 0; /* unused entry */
615 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100616 if (idx->tail == ctx->idx)
617 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100618 ctx->idx = ctx->prev; /* walk back to the end of previous header */
619 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
620 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200621 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100622 return ctx->idx;
623 }
624
625 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200626 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
627 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 */
629
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200630 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200631 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200632 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100633 NULL, 0);
634 hdr->len += delta;
635 http_msg_move_end(msg, delta);
636 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200637 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100638 return ctx->idx;
639}
640
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100641/* This function handles a server error at the stream interface level. The
642 * stream interface is assumed to be already in a closed state. An optional
643 * message is copied into the input buffer, and an HTTP status code stored.
644 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100645 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100647static void http_server_error(struct session *t, struct stream_interface *si,
648 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200649{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200650 channel_auto_read(si->ob);
651 channel_abort(si->ob);
652 channel_auto_close(si->ob);
653 channel_erase(si->ob);
654 channel_auto_close(si->ib);
655 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100656 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100657 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200658 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200659 }
660 if (!(t->flags & SN_ERR_MASK))
661 t->flags |= err;
662 if (!(t->flags & SN_FINST_MASK))
663 t->flags |= finst;
664}
665
Willy Tarreau80587432006-12-24 17:47:20 +0100666/* This function returns the appropriate error location for the given session
667 * and message.
668 */
669
Willy Tarreau783f2582012-09-04 12:19:04 +0200670struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100671{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200672 if (s->be->errmsg[msgnum].str)
673 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100674 else if (s->fe->errmsg[msgnum].str)
675 return &s->fe->errmsg[msgnum];
676 else
677 return &http_err_chunks[msgnum];
678}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200679
Willy Tarreau53b6c742006-12-17 13:37:46 +0100680/*
681 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
682 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
683 */
684static http_meth_t find_http_meth(const char *str, const int len)
685{
686 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100687 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100688
689 m = ((unsigned)*str - 'A');
690
691 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100692 for (h = http_methods[m]; h->len > 0; h++) {
693 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100694 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100695 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100697 };
698 return HTTP_METH_OTHER;
699 }
700 return HTTP_METH_NONE;
701
702}
703
Willy Tarreau21d2af32008-02-14 20:25:24 +0100704/* Parse the URI from the given transaction (which is assumed to be in request
705 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
706 * It is returned otherwise.
707 */
708static char *
709http_get_path(struct http_txn *txn)
710{
711 char *ptr, *end;
712
Willy Tarreau9b28e032012-10-12 23:49:43 +0200713 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100714 end = ptr + txn->req.sl.rq.u_l;
715
716 if (ptr >= end)
717 return NULL;
718
719 /* RFC2616, par. 5.1.2 :
720 * Request-URI = "*" | absuri | abspath | authority
721 */
722
723 if (*ptr == '*')
724 return NULL;
725
726 if (isalpha((unsigned char)*ptr)) {
727 /* this is a scheme as described by RFC3986, par. 3.1 */
728 ptr++;
729 while (ptr < end &&
730 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
731 ptr++;
732 /* skip '://' */
733 if (ptr == end || *ptr++ != ':')
734 return NULL;
735 if (ptr == end || *ptr++ != '/')
736 return NULL;
737 if (ptr == end || *ptr++ != '/')
738 return NULL;
739 }
740 /* skip [user[:passwd]@]host[:[port]] */
741
742 while (ptr < end && *ptr != '/')
743 ptr++;
744
745 if (ptr == end)
746 return NULL;
747
748 /* OK, we got the '/' ! */
749 return ptr;
750}
751
Willy Tarreauefb453c2008-10-26 20:49:47 +0100752/* Returns a 302 for a redirectable request. This may only be called just after
753 * the stream interface has moved to SI_ST_ASS. Unprocessable requests are
754 * left unchanged and will follow normal proxy processing.
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200755 * NOTE: this function is designed to support being called once data are scheduled
756 * for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100757 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758void perform_http_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100759{
760 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100761 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100762 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200763 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100764
765 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100766 trash.len = strlen(HTTP_302);
767 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100768
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100769 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100770
Willy Tarreauefb453c2008-10-26 20:49:47 +0100771 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100772 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 return;
774
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100775 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100776 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100777 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
778 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100779 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200781 /* 3: add the request URI. Since it was already forwarded, we need
782 * to temporarily rewind the buffer.
783 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200785 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200786
Willy Tarreauefb453c2008-10-26 20:49:47 +0100787 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200788 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 +0200789
Willy Tarreau9b28e032012-10-12 23:49:43 +0200790 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200791
Willy Tarreauefb453c2008-10-26 20:49:47 +0100792 if (!path)
793 return;
794
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100795 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796 return;
797
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100798 memcpy(trash.str + trash.len, path, len);
799 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100800
801 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100802 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
803 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100804 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100805 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
806 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100807 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808
809 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200810 si_shutr(si);
811 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100812 si->err_type = SI_ET_NONE;
813 si->err_loc = NULL;
814 si->state = SI_ST_CLO;
815
816 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100817 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100818
819 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau827aee92011-03-10 16:55:02 +0100820 if (srv)
821 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100822}
823
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100824/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100825 * that the server side is closed. Note that err_type is actually a
826 * bitmask, where almost only aborts may be cumulated with other
827 * values. We consider that aborted operations are more important
828 * than timeouts or errors due to the fact that nobody else in the
829 * logs might explain incomplete retries. All others should avoid
830 * being cumulated. It should normally not be possible to have multiple
831 * aborts at once, but just in case, the first one in sequence is reported.
832 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100833void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100835 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100836
837 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100838 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200839 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100841 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200842 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100843 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100844 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200845 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100846 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100847 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200848 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100850 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200851 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100852 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200854 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100855 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100856 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200857 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100858}
859
Willy Tarreau42250582007-04-01 01:30:43 +0200860extern const char sess_term_cond[8];
861extern const char sess_fin_state[8];
862extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200863struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100864struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100865struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100866
Willy Tarreau117f59e2007-03-04 18:17:17 +0100867/*
868 * Capture headers from message starting at <som> according to header list
869 * <cap_hdr>, and fill the <idx> structure appropriately.
870 */
871void capture_headers(char *som, struct hdr_idx *idx,
872 char **cap, struct cap_hdr *cap_hdr)
873{
874 char *eol, *sol, *col, *sov;
875 int cur_idx;
876 struct cap_hdr *h;
877 int len;
878
879 sol = som + hdr_idx_first_pos(idx);
880 cur_idx = hdr_idx_first_idx(idx);
881
882 while (cur_idx) {
883 eol = sol + idx->v[cur_idx].len;
884
885 col = sol;
886 while (col < eol && *col != ':')
887 col++;
888
889 sov = col + 1;
890 while (sov < eol && http_is_lws[(unsigned char)*sov])
891 sov++;
892
893 for (h = cap_hdr; h; h = h->next) {
894 if ((h->namelen == col - sol) &&
895 (strncasecmp(sol, h->name, h->namelen) == 0)) {
896 if (cap[h->index] == NULL)
897 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200898 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100899
900 if (cap[h->index] == NULL) {
901 Alert("HTTP capture : out of memory.\n");
902 continue;
903 }
904
905 len = eol - sov;
906 if (len > h->len)
907 len = h->len;
908
909 memcpy(cap[h->index], sov, len);
910 cap[h->index][len]=0;
911 }
912 }
913 sol = eol + idx->v[cur_idx].cr + 1;
914 cur_idx = idx->v[cur_idx].next;
915 }
916}
917
918
Willy Tarreau42250582007-04-01 01:30:43 +0200919/* either we find an LF at <ptr> or we jump to <bad>.
920 */
921#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
922
923/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
924 * otherwise to <http_msg_ood> with <state> set to <st>.
925 */
926#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
927 ptr++; \
928 if (likely(ptr < end)) \
929 goto good; \
930 else { \
931 state = (st); \
932 goto http_msg_ood; \
933 } \
934 } while (0)
935
936
Willy Tarreaubaaee002006-06-26 02:48:02 +0200937/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100938 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100939 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
940 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
941 * will give undefined results.
942 * Note that it is upon the caller's responsibility to ensure that ptr < end,
943 * and that msg->sol points to the beginning of the response.
944 * If a complete line is found (which implies that at least one CR or LF is
945 * found before <end>, the updated <ptr> is returned, otherwise NULL is
946 * returned indicating an incomplete line (which does not mean that parts have
947 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
948 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
949 * upon next call.
950 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200951 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100952 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
953 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200954 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100955 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200956const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100957 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100958 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100959{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200960 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100961
Willy Tarreau8973c702007-01-21 23:58:29 +0100962 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100963 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200964 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100965 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100966 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
967
968 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100969 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
971 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100972 state = HTTP_MSG_ERROR;
973 break;
974
Willy Tarreau8973c702007-01-21 23:58:29 +0100975 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200976 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100977 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100978 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100979 goto http_msg_rpcode;
980 }
981 if (likely(HTTP_IS_SPHT(*ptr)))
982 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
983 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100984 state = HTTP_MSG_ERROR;
985 break;
Willy Tarreau8973c702007-01-21 23:58:29 +0100986
Willy Tarreau8973c702007-01-21 23:58:29 +0100987 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200988 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +0100989 if (likely(!HTTP_IS_LWS(*ptr)))
990 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
991
992 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100993 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
995 }
996
997 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +0100998 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +0100999 http_msg_rsp_reason:
1000 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001001 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 msg->sl.st.r_l = 0;
1003 goto http_msg_rpline_eol;
1004
Willy Tarreau8973c702007-01-21 23:58:29 +01001005 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001006 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001007 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001008 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001009 goto http_msg_rpreason;
1010 }
1011 if (likely(HTTP_IS_SPHT(*ptr)))
1012 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1013 /* so it's a CR/LF, so there is no reason phrase */
1014 goto http_msg_rsp_reason;
1015
Willy Tarreau8973c702007-01-21 23:58:29 +01001016 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001017 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001018 if (likely(!HTTP_IS_CRLF(*ptr)))
1019 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001020 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001021 http_msg_rpline_eol:
1022 /* We have seen the end of line. Note that we do not
1023 * necessarily have the \n yet, but at least we know that we
1024 * have EITHER \r OR \n, otherwise the response would not be
1025 * complete. We can then record the response length and return
1026 * to the caller which will be able to register it.
1027 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001028 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001029 return ptr;
1030
1031#ifdef DEBUG_FULL
1032 default:
1033 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1034 exit(1);
1035#endif
1036 }
1037
1038 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001039 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001040 if (ret_state)
1041 *ret_state = state;
1042 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001043 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001044 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001045}
1046
Willy Tarreau8973c702007-01-21 23:58:29 +01001047/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001048 * This function parses a request line between <ptr> and <end>, starting with
1049 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1050 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1051 * will give undefined results.
1052 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1053 * and that msg->sol points to the beginning of the request.
1054 * If a complete line is found (which implies that at least one CR or LF is
1055 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1056 * returned indicating an incomplete line (which does not mean that parts have
1057 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1058 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1059 * upon next call.
1060 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001061 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001062 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1063 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001064 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001065 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001066const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001067 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001068 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001069{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001070 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001071
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001072 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001073 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001074 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001075 if (likely(HTTP_IS_TOKEN(*ptr)))
1076 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001077
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001078 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001079 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001080 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1081 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001082
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001083 if (likely(HTTP_IS_CRLF(*ptr))) {
1084 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001085 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001086 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001087 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001089 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001091 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001092 msg->sl.rq.v_l = 0;
1093 goto http_msg_rqline_eol;
1094 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001095 state = HTTP_MSG_ERROR;
1096 break;
1097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001099 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001100 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001101 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001102 goto http_msg_rquri;
1103 }
1104 if (likely(HTTP_IS_SPHT(*ptr)))
1105 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1106 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1107 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001108
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001109 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001110 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001111 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001112 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001113
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001114 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001115 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001116 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1117 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001118
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001119 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001120 /* non-ASCII chars are forbidden unless option
1121 * accept-invalid-http-request is enabled in the frontend.
1122 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001123 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001124 if (msg->err_pos < -1)
1125 goto invalid_char;
1126 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001127 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001128 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1129 }
1130
1131 if (likely(HTTP_IS_CRLF(*ptr))) {
1132 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1133 goto http_msg_req09_uri_e;
1134 }
1135
1136 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001137 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001138 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001139 state = HTTP_MSG_ERROR;
1140 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001141
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001142 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001143 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001144 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001145 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001146 goto http_msg_rqver;
1147 }
1148 if (likely(HTTP_IS_SPHT(*ptr)))
1149 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1150 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1151 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001152
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001153 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001154 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001155 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001156 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001157
1158 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001159 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001160 http_msg_rqline_eol:
1161 /* We have seen the end of line. Note that we do not
1162 * necessarily have the \n yet, but at least we know that we
1163 * have EITHER \r OR \n, otherwise the request would not be
1164 * complete. We can then record the request length and return
1165 * to the caller which will be able to register it.
1166 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001167 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001168 return ptr;
1169 }
1170
1171 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001172 state = HTTP_MSG_ERROR;
1173 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001174
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001175#ifdef DEBUG_FULL
1176 default:
1177 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1178 exit(1);
1179#endif
1180 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001182 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001183 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001184 if (ret_state)
1185 *ret_state = state;
1186 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001187 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001188 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001189}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001190
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001191/*
1192 * Returns the data from Authorization header. Function may be called more
1193 * than once so data is stored in txn->auth_data. When no header is found
1194 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1195 * searching again for something we are unable to find anyway.
1196 */
1197
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001198char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001199
1200int
1201get_http_auth(struct session *s)
1202{
1203
1204 struct http_txn *txn = &s->txn;
1205 struct chunk auth_method;
1206 struct hdr_ctx ctx;
1207 char *h, *p;
1208 int len;
1209
1210#ifdef DEBUG_AUTH
1211 printf("Auth for session %p: %d\n", s, txn->auth.method);
1212#endif
1213
1214 if (txn->auth.method == HTTP_AUTH_WRONG)
1215 return 0;
1216
1217 if (txn->auth.method)
1218 return 1;
1219
1220 txn->auth.method = HTTP_AUTH_WRONG;
1221
1222 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001223
1224 if (txn->flags & TX_USE_PX_CONN) {
1225 h = "Proxy-Authorization";
1226 len = strlen(h);
1227 } else {
1228 h = "Authorization";
1229 len = strlen(h);
1230 }
1231
Willy Tarreau9b28e032012-10-12 23:49:43 +02001232 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001233 return 0;
1234
1235 h = ctx.line + ctx.val;
1236
1237 p = memchr(h, ' ', ctx.vlen);
1238 if (!p || p == h)
1239 return 0;
1240
1241 chunk_initlen(&auth_method, h, 0, p-h);
1242 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1243
1244 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1245
1246 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001247 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248
1249 if (len < 0)
1250 return 0;
1251
1252
1253 get_http_auth_buff[len] = '\0';
1254
1255 p = strchr(get_http_auth_buff, ':');
1256
1257 if (!p)
1258 return 0;
1259
1260 txn->auth.user = get_http_auth_buff;
1261 *p = '\0';
1262 txn->auth.pass = p+1;
1263
1264 txn->auth.method = HTTP_AUTH_BASIC;
1265 return 1;
1266 }
1267
1268 return 0;
1269}
1270
Willy Tarreau58f10d72006-12-04 02:26:12 +01001271
Willy Tarreau8973c702007-01-21 23:58:29 +01001272/*
1273 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001274 * depending on the initial msg->msg_state. The caller is responsible for
1275 * ensuring that the message does not wrap. The function can be preempted
1276 * everywhere when data are missing and recalled at the exact same location
1277 * with no information loss. The message may even be realigned between two
1278 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001279 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001280 * fields. Note that msg->sol will be initialized after completing the first
1281 * state, so that none of the msg pointers has to be initialized prior to the
1282 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001283 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001284void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001285{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001286 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001287 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001288 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001289
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001290 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001291 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001292 ptr = buf->p + msg->next;
1293 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001295 if (unlikely(ptr >= end))
1296 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001297
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001298 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001299 /*
1300 * First, states that are specific to the response only.
1301 * We check them first so that request and headers are
1302 * closer to each other (accessed more often).
1303 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001304 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001305 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001306 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001307 /* we have a start of message, but we have to check
1308 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001309 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001310 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001311 if (unlikely(ptr != buf->p)) {
1312 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001313 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001314 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001315 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001316 }
Willy Tarreau26927362012-05-18 23:22:52 +02001317 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001318 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 hdr_idx_init(idx);
1320 state = HTTP_MSG_RPVER;
1321 goto http_msg_rpver;
1322 }
1323
1324 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1325 goto http_msg_invalid;
1326
1327 if (unlikely(*ptr == '\n'))
1328 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1329 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1330 /* stop here */
1331
Willy Tarreau8973c702007-01-21 23:58:29 +01001332 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001333 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 EXPECT_LF_HERE(ptr, http_msg_invalid);
1335 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1336 /* stop here */
1337
Willy Tarreau8973c702007-01-21 23:58:29 +01001338 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001339 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001340 case HTTP_MSG_RPVER_SP:
1341 case HTTP_MSG_RPCODE:
1342 case HTTP_MSG_RPCODE_SP:
1343 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001344 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001345 state, ptr, end,
1346 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 if (unlikely(!ptr))
1348 return;
1349
1350 /* we have a full response and we know that we have either a CR
1351 * or an LF at <ptr>.
1352 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1354
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001355 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001356 if (likely(*ptr == '\r'))
1357 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1358 goto http_msg_rpline_end;
1359
Willy Tarreau8973c702007-01-21 23:58:29 +01001360 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001361 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 /* msg->sol must point to the first of CR or LF. */
1363 EXPECT_LF_HERE(ptr, http_msg_invalid);
1364 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1365 /* stop here */
1366
1367 /*
1368 * Second, states that are specific to the request only
1369 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001370 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001371 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001372 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001373 /* we have a start of message, but we have to check
1374 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001375 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001376 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001377 if (likely(ptr != buf->p)) {
1378 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001379 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001380 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001381 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001382 }
Willy Tarreau26927362012-05-18 23:22:52 +02001383 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001384 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001385 state = HTTP_MSG_RQMETH;
1386 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001388
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001389 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1390 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001391
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001392 if (unlikely(*ptr == '\n'))
1393 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1394 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001395 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001396
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001398 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001399 EXPECT_LF_HERE(ptr, http_msg_invalid);
1400 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001401 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001402
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001403 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001404 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001405 case HTTP_MSG_RQMETH_SP:
1406 case HTTP_MSG_RQURI:
1407 case HTTP_MSG_RQURI_SP:
1408 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001409 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001410 state, ptr, end,
1411 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 if (unlikely(!ptr))
1413 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001414
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001415 /* we have a full request and we know that we have either a CR
1416 * or an LF at <ptr>.
1417 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001419
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001420 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001421 if (likely(*ptr == '\r'))
1422 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001423 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001424
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001425 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001426 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 /* check for HTTP/0.9 request : no version information available.
1428 * msg->sol must point to the first of CR or LF.
1429 */
1430 if (unlikely(msg->sl.rq.v_l == 0))
1431 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001432
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 EXPECT_LF_HERE(ptr, http_msg_invalid);
1434 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001435 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001436
Willy Tarreau8973c702007-01-21 23:58:29 +01001437 /*
1438 * Common states below
1439 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001441 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001442 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001443 if (likely(!HTTP_IS_CRLF(*ptr))) {
1444 goto http_msg_hdr_name;
1445 }
1446
1447 if (likely(*ptr == '\r'))
1448 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1449 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001450
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001451 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001452 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001453 /* assumes msg->sol points to the first char */
1454 if (likely(HTTP_IS_TOKEN(*ptr)))
1455 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001456
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001457 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001459
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001460 if (likely(msg->err_pos < -1) || *ptr == '\n')
1461 goto http_msg_invalid;
1462
1463 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001464 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001465
1466 /* and we still accept this non-token character */
1467 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001468
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001469 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001470 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001471 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001472 if (likely(HTTP_IS_SPHT(*ptr)))
1473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001474
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001475 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001476 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001477
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001478 if (likely(!HTTP_IS_CRLF(*ptr))) {
1479 goto http_msg_hdr_val;
1480 }
1481
1482 if (likely(*ptr == '\r'))
1483 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1484 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001485
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001486 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001487 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001488 EXPECT_LF_HERE(ptr, http_msg_invalid);
1489 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001490
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001491 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001492 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(HTTP_IS_SPHT(*ptr))) {
1494 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001495 for (; buf->p + msg->sov < ptr; msg->sov++)
1496 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001497 goto http_msg_hdr_l1_sp;
1498 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001499 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001500 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 goto http_msg_complete_header;
1502
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001504 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001505 /* assumes msg->sol points to the first char, and msg->sov
1506 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001507 */
1508 if (likely(!HTTP_IS_CRLF(*ptr)))
1509 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001510
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001511 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 /* Note: we could also copy eol into ->eoh so that we have the
1513 * real header end in case it ends with lots of LWS, but is this
1514 * really needed ?
1515 */
1516 if (likely(*ptr == '\r'))
1517 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1518 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001519
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001520 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001521 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 EXPECT_LF_HERE(ptr, http_msg_invalid);
1523 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001524
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001525 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001526 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1528 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001529 for (; buf->p + msg->eol < ptr; msg->eol++)
1530 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001531 goto http_msg_hdr_val;
1532 }
1533 http_msg_complete_header:
1534 /*
1535 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001536 * Assumes msg->sol points to the first char, msg->sov points
1537 * to the first character of the value and msg->eol to the
1538 * first CR or LF so we know how the line ends. We insert last
1539 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001541 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 idx, idx->tail) < 0))
1543 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001544
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001545 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 if (likely(!HTTP_IS_CRLF(*ptr))) {
1547 goto http_msg_hdr_name;
1548 }
1549
1550 if (likely(*ptr == '\r'))
1551 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1552 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001553
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001554 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001555 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001556 /* Assumes msg->sol points to the first of either CR or LF */
1557 EXPECT_LF_HERE(ptr, http_msg_invalid);
1558 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001559 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001560 msg->eoh = msg->sol;
1561 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001562 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001563 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001564
1565 case HTTP_MSG_ERROR:
1566 /* this may only happen if we call http_msg_analyser() twice with an error */
1567 break;
1568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569#ifdef DEBUG_FULL
1570 default:
1571 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1572 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001573#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001574 }
1575 http_msg_ood:
1576 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001578 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001579 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001580
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001581 http_msg_invalid:
1582 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001583 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001584 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001585 return;
1586}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001587
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001588/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1589 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1590 * nothing is done and 1 is returned.
1591 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001592static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001593{
1594 int delta;
1595 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001596 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001597
1598 if (msg->sl.rq.v_l != 0)
1599 return 1;
1600
Willy Tarreau9b28e032012-10-12 23:49:43 +02001601 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001602 delta = 0;
1603
1604 if (msg->sl.rq.u_l == 0) {
1605 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001606 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001607 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001608 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001609 }
1610 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001611 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001612 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001613 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001614 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001615 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001616 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 NULL, NULL);
1618 if (unlikely(!cur_end))
1619 return 0;
1620
1621 /* we have a full HTTP/1.0 request now and we know that
1622 * we have either a CR or an LF at <ptr>.
1623 */
1624 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1625 return 1;
1626}
1627
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001628/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001629 * and "keep-alive" values. If we already know that some headers may safely
1630 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001631 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1632 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001633 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001634 * 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 Tarreau50fc7772012-11-11 22:19:57 +01001670 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1671 txn->flags |= TX_HDR_CONN_UPG;
1672 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001673 }
1674
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675 txn->flags |= TX_HDR_CONN_PRS;
1676 return;
1677}
Willy Tarreau5b154472009-12-21 20:11:07 +01001678
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001679/* Apply desired changes on the Connection: header. Values may be removed and/or
1680 * added depending on the <wanted> flags, which are exclusively composed of
1681 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1682 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1683 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001684void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001685{
1686 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001687 const char *hdr_val = "Connection";
1688 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001689
1690 ctx.idx = 0;
1691
Willy Tarreau88d349d2010-01-25 12:15:43 +01001692
1693 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1694 hdr_val = "Proxy-Connection";
1695 hdr_len = 16;
1696 }
1697
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001698 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001699 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001700 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1701 if (wanted & TX_CON_KAL_SET)
1702 txn->flags |= TX_CON_KAL_SET;
1703 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001704 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001705 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001706 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1707 if (wanted & TX_CON_CLO_SET)
1708 txn->flags |= TX_CON_CLO_SET;
1709 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001710 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001711 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001712 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713
1714 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1715 return;
1716
1717 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1718 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001719 hdr_val = "Connection: close";
1720 hdr_len = 17;
1721 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1722 hdr_val = "Proxy-Connection: close";
1723 hdr_len = 23;
1724 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001725 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001726 }
1727
1728 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1729 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001730 hdr_val = "Connection: keep-alive";
1731 hdr_len = 22;
1732 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1733 hdr_val = "Proxy-Connection: keep-alive";
1734 hdr_len = 28;
1735 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001736 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001737 }
1738 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001739}
1740
Willy Tarreaua458b672012-03-05 11:17:50 +01001741/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001742 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001743 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001744 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001745 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001746 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001747static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001748{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001749 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001750 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001751 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001752 const char *end = buf->data + buf->size;
1753 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001754 unsigned int chunk = 0;
1755
1756 /* The chunk size is in the following form, though we are only
1757 * interested in the size and CRLF :
1758 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1759 */
1760 while (1) {
1761 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001762 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001763 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001765 if (c < 0) /* not a hex digit anymore */
1766 break;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001767 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001768 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001769 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001770 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001771 chunk = (chunk << 4) + c;
1772 }
1773
Willy Tarreaud98cf932009-12-27 22:54:55 +01001774 /* empty size not allowed */
Willy Tarreaua458b672012-03-05 11:17:50 +01001775 if (ptr == ptr_old)
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001776 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001777
1778 while (http_is_spht[(unsigned char)*ptr]) {
1779 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001780 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001781 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001782 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001783 }
1784
Willy Tarreaud98cf932009-12-27 22:54:55 +01001785 /* Up to there, we know that at least one byte is present at *ptr. Check
1786 * for the end of chunk size.
1787 */
1788 while (1) {
1789 if (likely(HTTP_IS_CRLF(*ptr))) {
1790 /* we now have a CR or an LF at ptr */
1791 if (likely(*ptr == '\r')) {
1792 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001793 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001794 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001795 return 0;
1796 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001797
Willy Tarreaud98cf932009-12-27 22:54:55 +01001798 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001799 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001801 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001802 /* done */
1803 break;
1804 }
1805 else if (*ptr == ';') {
1806 /* chunk extension, ends at next CRLF */
1807 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001808 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001809 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001810 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001811
1812 while (!HTTP_IS_CRLF(*ptr)) {
1813 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001814 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001815 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001816 return 0;
1817 }
1818 /* we have a CRLF now, loop above */
1819 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001820 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001821 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001822 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001823 }
1824
Willy Tarreaud98cf932009-12-27 22:54:55 +01001825 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001826 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001827 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001828 */
Willy Tarreaub8ffd372012-09-27 15:08:56 +02001829 if (ptr < ptr_old)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001830 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001831 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001832 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001833 msg->chunk_len = chunk;
1834 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001836 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001837 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001838 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001839 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001840}
1841
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001842/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001843 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001844 * the trailers is found, it is automatically scheduled to be forwarded,
1845 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1846 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001847 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001848 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001849 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001850 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1851 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001852 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001853 * matches the length of trailers already parsed and not forwarded. It is also
1854 * important to note that this function is designed to be able to parse wrapped
1855 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001857static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001859 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001860
Willy Tarreaua458b672012-03-05 11:17:50 +01001861 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001862 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001863 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001864 const char *ptr = b_ptr(buf, msg->next);
1865 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001866 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001867
1868 /* scan current line and stop at LF or CRLF */
1869 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001870 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001871 return 0;
1872
1873 if (*ptr == '\n') {
1874 if (!p1)
1875 p1 = ptr;
1876 p2 = ptr;
1877 break;
1878 }
1879
1880 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001881 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001882 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001883 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001884 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001885 p1 = ptr;
1886 }
1887
1888 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001889 if (ptr >= buf->data + buf->size)
1890 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001891 }
1892
1893 /* after LF; point to beginning of next line */
1894 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001895 if (p2 >= buf->data + buf->size)
1896 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001897
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001898 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001899 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001900 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001901
1902 /* schedule this line for forwarding */
1903 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 if (msg->sov >= buf->size)
1905 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001907 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001908 /* LF/CRLF at beginning of line => end of trailers at p2.
1909 * Everything was scheduled for forwarding, there's nothing
1910 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001911 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001912 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001913 msg->msg_state = HTTP_MSG_DONE;
1914 return 1;
1915 }
1916 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001917 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001918 }
1919}
1920
Willy Tarreau54d23df2012-10-25 19:04:45 +02001921/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001923 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001924 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1926 * not enough data are available, the function does not change anything and
1927 * returns zero. If a parse error is encountered, the function returns < 0 and
1928 * does not change anything. Note: this function is designed to parse wrapped
1929 * CRLF at the end of the buffer.
1930 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001931static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001932{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001933 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001934 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001935 int bytes;
1936
1937 /* NB: we'll check data availabilty at the end. It's not a
1938 * problem because whatever we match first will be checked
1939 * against the correct length.
1940 */
1941 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001942 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001943 if (*ptr == '\r') {
1944 bytes++;
1945 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001946 if (ptr >= buf->data + buf->size)
1947 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001948 }
1949
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001950 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001951 return 0;
1952
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001953 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001954 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001955 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001956 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001957
1958 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001959 if (ptr >= buf->data + buf->size)
1960 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001961 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1962 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001963 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001964 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1965 return 1;
1966}
Willy Tarreau5b154472009-12-21 20:11:07 +01001967
William Lallemand82fe75c2012-10-23 10:25:10 +02001968
1969/*
1970 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001971 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001972int select_compression_request_header(struct session *s, struct buffer *req)
1973{
1974 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001975 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001976 struct hdr_ctx ctx;
1977 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001978 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001979
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001980 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1981 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001982 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1983 */
1984 ctx.idx = 0;
1985 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
1986 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001987 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
1988 (ctx.vlen < 31 ||
1989 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
1990 ctx.line[ctx.val + 30] < '6' ||
1991 (ctx.line[ctx.val + 30] == '6' &&
1992 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
1993 s->comp_algo = NULL;
1994 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02001995 }
1996
William Lallemand82fe75c2012-10-23 10:25:10 +02001997 ctx.idx = 0;
1998 /* no compression when Cache-Control: no-transform found */
1999 while (http_find_header2("Cache-Control", 13, req->p, &txn->hdr_idx, &ctx)) {
2000 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) {
2001 s->comp_algo = NULL;
2002 return 0;
2003 }
2004 }
2005
2006 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002007 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 +02002008 ctx.idx = 0;
2009 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002010 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002011 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2012 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002013
2014 /* remove all occurrences of the header when "compression offload" is set */
2015
2016 if ((s->be->comp && s->be->comp->offload) ||
2017 (s->fe->comp && s->fe->comp->offload)) {
2018 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2019 ctx.idx = 0;
2020 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2021 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2022 }
2023 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002024 return 1;
2025 }
2026 }
2027 }
2028 }
2029
2030 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002031 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2032 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002033 if (comp_algo->add_data == identity_add_data) {
2034 s->comp_algo = comp_algo;
2035 return 1;
2036 }
2037 }
2038 }
2039
2040 s->comp_algo = NULL;
2041
2042 return 0;
2043}
2044
2045/*
2046 * Selects a comression algorithm depending of the server response.
2047 */
2048int select_compression_response_header(struct session *s, struct buffer *res)
2049{
2050 struct http_txn *txn = &s->txn;
2051 struct http_msg *msg = &txn->rsp;
2052 struct hdr_ctx ctx;
2053 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002054
2055 /* no common compression algorithm was found in request header */
2056 if (s->comp_algo == NULL)
2057 goto fail;
2058
2059 /* HTTP < 1.1 should not be compressed */
2060 if (!(msg->flags & HTTP_MSGF_VER_11))
2061 goto fail;
2062
William Lallemandd3002612012-11-26 14:34:47 +01002063 /* 200 only */
2064 if (txn->status != 200)
2065 goto fail;
2066
William Lallemand82fe75c2012-10-23 10:25:10 +02002067 /* Content-Length is null */
2068 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2069 goto fail;
2070
2071 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002072 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002073 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2074 goto fail;
2075
2076 comp_type = NULL;
2077
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002078 /* we don't want to compress multipart content-types, nor content-types that are
2079 * not listed in the "compression type" directive if any. If no content-type was
2080 * found but configuration requires one, we don't compress either. Backend has
2081 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002082 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002083 ctx.idx = 0;
2084 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2085 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2086 goto fail;
2087
2088 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2089 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002090 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002091 if (ctx.vlen >= comp_type->name_len &&
2092 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002093 /* this Content-Type should be compressed */
2094 break;
2095 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002096 /* this Content-Type should not be compressed */
2097 if (comp_type == NULL)
2098 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002099 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002100 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002101 else { /* no content-type header */
2102 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2103 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002104 }
2105
William Lallemandd85f9172012-11-09 17:05:39 +01002106 /* limit compression rate */
2107 if (global.comp_rate_lim > 0)
2108 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2109 goto fail;
2110
William Lallemand072a2bf2012-11-20 17:01:01 +01002111 /* limit cpu usage */
2112 if (idle_pct < compress_min_idle)
2113 goto fail;
2114
William Lallemand4c49fae2012-11-07 15:00:23 +01002115 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002116 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002117 goto fail;
2118
William Lallemandec3e3892012-11-12 17:02:18 +01002119 s->flags |= SN_COMP_READY;
2120
William Lallemand82fe75c2012-10-23 10:25:10 +02002121 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002122 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002123 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2124 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2125
2126 /* add Transfer-Encoding header */
2127 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2128 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2129
2130 /*
2131 * Add Content-Encoding header when it's not identity encoding.
2132 * RFC 2616 : Identity encoding: This content-coding is used only in the
2133 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2134 * header.
2135 */
2136 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002137 trash.len = 18;
2138 memcpy(trash.str, "Content-Encoding: ", trash.len);
2139 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2140 trash.len += s->comp_algo->name_len;
2141 trash.str[trash.len] = '\0';
2142 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002143 }
2144
William Lallemand82fe75c2012-10-23 10:25:10 +02002145 return 1;
2146
2147fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002148 if (s->flags & SN_COMP_READY)
William Lallemand1c2d6222012-10-30 15:52:53 +01002149 s->comp_algo->end(&s->comp_ctx);
Willy Tarreaub97b6192012-11-19 14:55:02 +01002150 s->comp_algo = NULL;
2151 s->flags &= ~SN_COMP_READY;
William Lallemand82fe75c2012-10-23 10:25:10 +02002152 return 0;
2153}
2154
2155
Willy Tarreaud787e662009-07-07 10:14:51 +02002156/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2157 * processing can continue on next analysers, or zero if it either needs more
2158 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2159 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2160 * when it has nothing left to do, and may remove any analyser when it wants to
2161 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002162 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002163int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002164{
Willy Tarreau59234e92008-11-30 23:51:27 +01002165 /*
2166 * We will parse the partial (or complete) lines.
2167 * We will check the request syntax, and also join multi-line
2168 * headers. An index of all the lines will be elaborated while
2169 * parsing.
2170 *
2171 * For the parsing, we use a 28 states FSM.
2172 *
2173 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002174 * req->buf->p = beginning of request
2175 * req->buf->p + msg->eoh = end of processed headers / start of current one
2176 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002177 * msg->eol = end of current header or line (LF or CRLF)
2178 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002179 *
2180 * At end of parsing, we may perform a capture of the error (if any), and
2181 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2182 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2183 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002184 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002185
Willy Tarreau59234e92008-11-30 23:51:27 +01002186 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002187 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002188 struct http_txn *txn = &s->txn;
2189 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002190 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002191
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002192 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 +01002193 now_ms, __FUNCTION__,
2194 s,
2195 req,
2196 req->rex, req->wex,
2197 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002198 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002199 req->analysers);
2200
Willy Tarreau52a0c602009-08-16 22:45:38 +02002201 /* we're speaking HTTP here, so let's speak HTTP to the client */
2202 s->srv_error = http_return_srv_error;
2203
Willy Tarreau83e3af02009-12-28 17:39:57 +01002204 /* There's a protected area at the end of the buffer for rewriting
2205 * purposes. We don't want to start to parse the request if the
2206 * protected area is affected, because we may have to move processed
2207 * data later, which is much more complicated.
2208 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002209 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau065e8332010-01-08 00:30:20 +01002210 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002211 unlikely(channel_full(req) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002212 bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2213 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) {
2214 if (req->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002215 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002216 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002217 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002218 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002219 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002220 return 0;
2221 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002222 if (bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2223 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)
2224 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002225 }
2226
Willy Tarreau065e8332010-01-08 00:30:20 +01002227 /* Note that we have the same problem with the response ; we
2228 * may want to send a redirect, error or anything which requires
2229 * some spare space. So we'll ensure that we have at least
2230 * maxrewrite bytes available in the response buffer before
2231 * processing that one. This will only affect pipelined
2232 * keep-alive requests.
2233 */
2234 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02002235 unlikely(channel_full(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002236 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2237 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2238 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002239 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002240 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002241 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002242 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002243 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002244 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002245 return 0;
2246 }
2247 }
2248
Willy Tarreau9b28e032012-10-12 23:49:43 +02002249 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002250 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002251 }
2252
Willy Tarreau59234e92008-11-30 23:51:27 +01002253 /* 1: we might have to print this header in debug mode */
2254 if (unlikely((global.mode & MODE_DEBUG) &&
2255 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002256 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002257 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002258
Willy Tarreau9b28e032012-10-12 23:49:43 +02002259 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002260 /* this is a bit complex : in case of error on the request line,
2261 * we know that rq.l is still zero, so we display only the part
2262 * up to the end of the line (truncated by debug_hdr).
2263 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002264 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002265 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002266
Willy Tarreau59234e92008-11-30 23:51:27 +01002267 sol += hdr_idx_first_pos(&txn->hdr_idx);
2268 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002269
Willy Tarreau59234e92008-11-30 23:51:27 +01002270 while (cur_idx) {
2271 eol = sol + txn->hdr_idx.v[cur_idx].len;
2272 debug_hdr("clihdr", s, sol, eol);
2273 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2274 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002275 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002276 }
2277
Willy Tarreau58f10d72006-12-04 02:26:12 +01002278
Willy Tarreau59234e92008-11-30 23:51:27 +01002279 /*
2280 * Now we quickly check if we have found a full valid request.
2281 * If not so, we check the FD and buffer states before leaving.
2282 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002283 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002284 * requests are checked first. When waiting for a second request
2285 * on a keep-alive session, if we encounter and error, close, t/o,
2286 * we note the error in the session flags but don't set any state.
2287 * Since the error will be noted there, it will not be counted by
2288 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002289 * Last, we may increase some tracked counters' http request errors on
2290 * the cases that are deliberately the client's fault. For instance,
2291 * a timeout or connection reset is not counted as an error. However
2292 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002293 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002294
Willy Tarreau655dce92009-11-08 13:10:58 +01002295 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002296 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002297 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002298 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002299 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002300 session_inc_http_req_ctr(s);
2301 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002302 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002303 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002304 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002305
Willy Tarreau59234e92008-11-30 23:51:27 +01002306 /* 1: Since we are in header mode, if there's no space
2307 * left for headers, we won't be able to free more
2308 * later, so the session will never terminate. We
2309 * must terminate it now.
2310 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002311 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 /* FIXME: check if URI is set and return Status
2313 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002314 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002315 session_inc_http_req_ctr(s);
2316 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002317 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002318 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002319 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002320 goto return_bad_req;
2321 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002322
Willy Tarreau59234e92008-11-30 23:51:27 +01002323 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002324 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002325 if (!(s->flags & SN_ERR_MASK))
2326 s->flags |= SN_ERR_CLICL;
2327
Willy Tarreaufcffa692010-01-10 14:21:19 +01002328 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002329 goto failed_keep_alive;
2330
Willy Tarreau59234e92008-11-30 23:51:27 +01002331 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002332 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002333 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002334 session_inc_http_err_ctr(s);
2335 }
2336
Willy Tarreaudc979f22012-12-04 10:39:01 +01002337 txn->status = 400;
2338 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002339 msg->msg_state = HTTP_MSG_ERROR;
2340 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002341
Willy Tarreauda7ff642010-06-23 11:44:09 +02002342 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002343 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002344 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002345 if (s->listener->counters)
2346 s->listener->counters->failed_req++;
2347
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 if (!(s->flags & SN_FINST_MASK))
2349 s->flags |= SN_FINST_R;
2350 return 0;
2351 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002352
Willy Tarreau59234e92008-11-30 23:51:27 +01002353 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002354 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002355 if (!(s->flags & SN_ERR_MASK))
2356 s->flags |= SN_ERR_CLITO;
2357
Willy Tarreaufcffa692010-01-10 14:21:19 +01002358 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002359 goto failed_keep_alive;
2360
Willy Tarreau59234e92008-11-30 23:51:27 +01002361 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002362 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002363 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002364 session_inc_http_err_ctr(s);
2365 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002366 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002367 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002368 msg->msg_state = HTTP_MSG_ERROR;
2369 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002370
Willy Tarreauda7ff642010-06-23 11:44:09 +02002371 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002372 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002373 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002374 if (s->listener->counters)
2375 s->listener->counters->failed_req++;
2376
Willy Tarreau59234e92008-11-30 23:51:27 +01002377 if (!(s->flags & SN_FINST_MASK))
2378 s->flags |= SN_FINST_R;
2379 return 0;
2380 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002381
Willy Tarreau59234e92008-11-30 23:51:27 +01002382 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002383 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002384 if (!(s->flags & SN_ERR_MASK))
2385 s->flags |= SN_ERR_CLICL;
2386
Willy Tarreaufcffa692010-01-10 14:21:19 +01002387 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002388 goto failed_keep_alive;
2389
Willy Tarreau4076a152009-04-02 15:18:36 +02002390 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002391 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002392 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002393 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002394 msg->msg_state = HTTP_MSG_ERROR;
2395 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002396
Willy Tarreauda7ff642010-06-23 11:44:09 +02002397 session_inc_http_err_ctr(s);
2398 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002399 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002400 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002401 if (s->listener->counters)
2402 s->listener->counters->failed_req++;
2403
Willy Tarreau59234e92008-11-30 23:51:27 +01002404 if (!(s->flags & SN_FINST_MASK))
2405 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002406 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002407 }
2408
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002409 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002410 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2411 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002412#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002413 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002414 /* We need more data, we have to re-enable quick-ack in case we
2415 * previously disabled it, otherwise we might cause the client
2416 * to delay next data.
2417 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002418 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002419 }
2420#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002421
Willy Tarreaufcffa692010-01-10 14:21:19 +01002422 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2423 /* If the client starts to talk, let's fall back to
2424 * request timeout processing.
2425 */
2426 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002427 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002428 }
2429
Willy Tarreau59234e92008-11-30 23:51:27 +01002430 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002431 if (!tick_isset(req->analyse_exp)) {
2432 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2433 (txn->flags & TX_WAIT_NEXT_RQ) &&
2434 tick_isset(s->be->timeout.httpka))
2435 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2436 else
2437 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2438 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002439
Willy Tarreau59234e92008-11-30 23:51:27 +01002440 /* we're not ready yet */
2441 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002442
2443 failed_keep_alive:
2444 /* Here we process low-level errors for keep-alive requests. In
2445 * short, if the request is not the first one and it experiences
2446 * a timeout, read error or shutdown, we just silently close so
2447 * that the client can try again.
2448 */
2449 txn->status = 0;
2450 msg->msg_state = HTTP_MSG_RQBEFORE;
2451 req->analysers = 0;
2452 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002453 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002454 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002455 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002456 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002457
Willy Tarreaud787e662009-07-07 10:14:51 +02002458 /* OK now we have a complete HTTP request with indexed headers. Let's
2459 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002460 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002461 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002462 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002463 * byte after the last LF. msg->sov points to the first byte of data.
2464 * msg->eol cannot be trusted because it may have been left uninitialized
2465 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002466 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002467
Willy Tarreauda7ff642010-06-23 11:44:09 +02002468 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002469 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2470
Willy Tarreaub16a5742010-01-10 14:46:16 +01002471 if (txn->flags & TX_WAIT_NEXT_RQ) {
2472 /* kill the pending keep-alive timeout */
2473 txn->flags &= ~TX_WAIT_NEXT_RQ;
2474 req->analyse_exp = TICK_ETERNITY;
2475 }
2476
2477
Willy Tarreaud787e662009-07-07 10:14:51 +02002478 /* Maybe we found in invalid header name while we were configured not
2479 * to block on that, so we have to capture it now.
2480 */
2481 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002482 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002483
Willy Tarreau59234e92008-11-30 23:51:27 +01002484 /*
2485 * 1: identify the method
2486 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002487 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002488
2489 /* we can make use of server redirect on GET and HEAD */
2490 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2491 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002492
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 /*
2494 * 2: check if the URI matches the monitor_uri.
2495 * We have to do this for every request which gets in, because
2496 * the monitor-uri is defined by the frontend.
2497 */
2498 if (unlikely((s->fe->monitor_uri_len != 0) &&
2499 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002500 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002501 s->fe->monitor_uri,
2502 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002503 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002504 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002505 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002506 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002507
Willy Tarreau59234e92008-11-30 23:51:27 +01002508 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002509 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002510
Willy Tarreau59234e92008-11-30 23:51:27 +01002511 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002512 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002513 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002514
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 ret = acl_pass(ret);
2516 if (cond->pol == ACL_COND_UNLESS)
2517 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002518
Willy Tarreau59234e92008-11-30 23:51:27 +01002519 if (ret) {
2520 /* we fail this request, let's return 503 service unavail */
2521 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002522 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002523 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002524 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002525 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002526
Willy Tarreau59234e92008-11-30 23:51:27 +01002527 /* nothing to fail, let's reply normaly */
2528 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002529 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002530 goto return_prx_cond;
2531 }
2532
2533 /*
2534 * 3: Maybe we have to copy the original REQURI for the logs ?
2535 * Note: we cannot log anymore if the request has been
2536 * classified as invalid.
2537 */
2538 if (unlikely(s->logs.logwait & LW_REQ)) {
2539 /* we have a complete HTTP request that we must log */
2540 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2541 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002542
Willy Tarreau59234e92008-11-30 23:51:27 +01002543 if (urilen >= REQURI_LEN)
2544 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002545 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002546 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002547
Willy Tarreau59234e92008-11-30 23:51:27 +01002548 if (!(s->logs.logwait &= ~LW_REQ))
2549 s->do_log(s);
2550 } else {
2551 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002552 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002553 }
Willy Tarreau06619262006-12-17 08:37:22 +01002554
William Lallemanda73203e2012-03-12 12:48:57 +01002555 if (!LIST_ISEMPTY(&s->fe->format_unique_id)) {
2556 s->unique_id = pool_alloc2(pool2_uniqueid);
2557 }
2558
Willy Tarreau59234e92008-11-30 23:51:27 +01002559 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002560 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002561 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002562
Willy Tarreau5b154472009-12-21 20:11:07 +01002563 /* ... and check if the request is HTTP/1.1 or above */
2564 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002565 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2566 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2567 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002568 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002569
2570 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002571 txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL | TX_HDR_CONN_UPG);
Willy Tarreau5b154472009-12-21 20:11:07 +01002572
Willy Tarreau88d349d2010-01-25 12:15:43 +01002573 /* if the frontend has "option http-use-proxy-header", we'll check if
2574 * we have what looks like a proxied connection instead of a connection,
2575 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2576 * Note that this is *not* RFC-compliant, however browsers and proxies
2577 * happen to do that despite being non-standard :-(
2578 * We consider that a request not beginning with either '/' or '*' is
2579 * a proxied connection, which covers both "scheme://location" and
2580 * CONNECT ip:port.
2581 */
2582 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002583 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002584 txn->flags |= TX_USE_PX_CONN;
2585
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002586 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002587 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002588
Willy Tarreau59234e92008-11-30 23:51:27 +01002589 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002590 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002591 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002592 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002593
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002594 /* 6: determine the transfer-length.
2595 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2596 * the presence of a message-body in a REQUEST and its transfer length
2597 * must be determined that way (in order of precedence) :
2598 * 1. The presence of a message-body in a request is signaled by the
2599 * inclusion of a Content-Length or Transfer-Encoding header field
2600 * in the request's header fields. When a request message contains
2601 * both a message-body of non-zero length and a method that does
2602 * not define any semantics for that request message-body, then an
2603 * origin server SHOULD either ignore the message-body or respond
2604 * with an appropriate error message (e.g., 413). A proxy or
2605 * gateway, when presented the same request, SHOULD either forward
2606 * the request inbound with the message- body or ignore the
2607 * message-body when determining a response.
2608 *
2609 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2610 * and the "chunked" transfer-coding (Section 6.2) is used, the
2611 * transfer-length is defined by the use of this transfer-coding.
2612 * If a Transfer-Encoding header field is present and the "chunked"
2613 * transfer-coding is not present, the transfer-length is defined
2614 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002615 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002616 * 3. If a Content-Length header field is present, its decimal value in
2617 * OCTETs represents both the entity-length and the transfer-length.
2618 * If a message is received with both a Transfer-Encoding header
2619 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002620 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002621 * 4. By the server closing the connection. (Closing the connection
2622 * cannot be used to indicate the end of a request body, since that
2623 * would leave no possibility for the server to send back a response.)
2624 *
2625 * Whenever a transfer-coding is applied to a message-body, the set of
2626 * transfer-codings MUST include "chunked", unless the message indicates
2627 * it is terminated by closing the connection. When the "chunked"
2628 * transfer-coding is used, it MUST be the last transfer-coding applied
2629 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002630 */
2631
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002632 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002633 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002634 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002635 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002636 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002637 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002638 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2639 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002640 /* bad transfer-encoding (chunked followed by something else) */
2641 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002642 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002643 break;
2644 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002645 }
2646
Willy Tarreau32b47f42009-10-18 20:55:02 +02002647 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002648 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002649 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002650 signed long long cl;
2651
Willy Tarreauad14f752011-09-02 20:33:27 +02002652 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002653 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002654 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002655 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002656
Willy Tarreauad14f752011-09-02 20:33:27 +02002657 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002658 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002659 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002660 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002661
Willy Tarreauad14f752011-09-02 20:33:27 +02002662 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002663 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002664 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002665 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002666
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002667 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002668 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002669 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002670 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002671
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002672 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002673 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002674 }
2675
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002676 /* bodyless requests have a known length */
2677 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002678 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002679
Willy Tarreaud787e662009-07-07 10:14:51 +02002680 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002681 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002682 req->analyse_exp = TICK_ETERNITY;
2683 return 1;
2684
2685 return_bad_req:
2686 /* We centralize bad requests processing here */
2687 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2688 /* we detected a parsing error. We want to archive this request
2689 * in the dedicated proxy area for later troubleshooting.
2690 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002691 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002692 }
2693
2694 txn->req.msg_state = HTTP_MSG_ERROR;
2695 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002696 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002697
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002698 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002699 if (s->listener->counters)
2700 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002701
2702 return_prx_cond:
2703 if (!(s->flags & SN_ERR_MASK))
2704 s->flags |= SN_ERR_PRXCOND;
2705 if (!(s->flags & SN_FINST_MASK))
2706 s->flags |= SN_FINST_R;
2707
2708 req->analysers = 0;
2709 req->analyse_exp = TICK_ETERNITY;
2710 return 0;
2711}
2712
Cyril Bonté70be45d2010-10-12 00:14:35 +02002713/* We reached the stats page through a POST request.
2714 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002715 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002716 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002717int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002718{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002719 struct proxy *px = NULL;
2720 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002721
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002722 char key[LINESIZE];
2723 int action = ST_ADM_ACTION_NONE;
2724 int reprocess = 0;
2725
2726 int total_servers = 0;
2727 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002728
2729 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002730 char *st_cur_param = NULL;
2731 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002732
Willy Tarreau9b28e032012-10-12 23:49:43 +02002733 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002734 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002735
2736 cur_param = next_param = end_params;
2737
Willy Tarreau9b28e032012-10-12 23:49:43 +02002738 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002739 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002740 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002741 return 1;
2742 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002743 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002744 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002745 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002746 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002747 }
2748
2749 *end_params = '\0';
2750
Willy Tarreau295a8372011-03-10 11:25:07 +01002751 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002752
2753 /*
2754 * Parse the parameters in reverse order to only store the last value.
2755 * From the html form, the backend and the action are at the end.
2756 */
2757 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002758 char *value;
2759 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002760
2761 cur_param--;
2762 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002763 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002764 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002765 poffset = (cur_param != first_param ? 1 : 0);
2766 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2767 if ((plen > 0) && (plen <= sizeof(key))) {
2768 strncpy(key, cur_param + poffset, plen);
2769 key[plen - 1] = '\0';
2770 } else {
2771 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2772 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002773 }
2774
2775 /* Parse the value */
2776 value = key;
2777 while (*value != '\0' && *value != '=') {
2778 value++;
2779 }
2780 if (*value == '=') {
2781 /* Ok, a value is found, we can mark the end of the key */
2782 *value++ = '\0';
2783 }
2784
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002785 if (!url_decode(key) || !url_decode(value))
2786 break;
2787
Cyril Bonté70be45d2010-10-12 00:14:35 +02002788 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002789 if (!px && (strcmp(key, "b") == 0)) {
2790 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2791 /* the backend name is unknown or ambiguous (duplicate names) */
2792 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2793 goto out;
2794 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002795 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002796 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002797 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002798 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002799 }
2800 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002801 action = ST_ADM_ACTION_ENABLE;
2802 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002803 else if (strcmp(value, "stop") == 0) {
2804 action = ST_ADM_ACTION_STOP;
2805 }
2806 else if (strcmp(value, "start") == 0) {
2807 action = ST_ADM_ACTION_START;
2808 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002809 else if (strcmp(value, "shutdown") == 0) {
2810 action = ST_ADM_ACTION_SHUTDOWN;
2811 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002812 else {
2813 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2814 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002815 }
2816 }
2817 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002818 if (!(px && action)) {
2819 /*
2820 * Indicates that we'll need to reprocess the parameters
2821 * as soon as backend and action are known
2822 */
2823 if (!reprocess) {
2824 st_cur_param = cur_param;
2825 st_next_param = next_param;
2826 }
2827 reprocess = 1;
2828 }
2829 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002830 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002831 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002832 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002833 /* Not already in maintenance, we can change the server state */
2834 sv->state |= SRV_MAINTAIN;
2835 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002836 altered_servers++;
2837 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002838 }
2839 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002840 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002841 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002842 /* Already in maintenance, we can change the server state */
2843 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002844 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002845 altered_servers++;
2846 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002847 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002848 break;
2849 case ST_ADM_ACTION_STOP:
2850 case ST_ADM_ACTION_START:
2851 if (action == ST_ADM_ACTION_START)
2852 sv->uweight = sv->iweight;
2853 else
2854 sv->uweight = 0;
2855
2856 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2857 /* we must take care of not pushing the server to full throttle during slow starts */
2858 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2859 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2860 else
2861 sv->eweight = BE_WEIGHT_SCALE;
2862 sv->eweight *= sv->uweight;
2863 } else {
2864 sv->eweight = sv->uweight;
2865 }
2866
2867 /* static LB algorithms are a bit harder to update */
2868 if (px->lbprm.update_server_eweight)
2869 px->lbprm.update_server_eweight(sv);
2870 else if (sv->eweight) {
2871 if (px->lbprm.set_server_status_up)
2872 px->lbprm.set_server_status_up(sv);
2873 }
2874 else {
2875 if (px->lbprm.set_server_status_down)
2876 px->lbprm.set_server_status_down(sv);
2877 }
2878 altered_servers++;
2879 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002880 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002881 case ST_ADM_ACTION_SHUTDOWN:
2882 if (px->state != PR_STSTOPPED) {
2883 struct session *sess, *sess_bck;
2884
2885 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2886 if (sess->srv_conn == sv)
2887 session_shutdown(sess, SN_ERR_KILLED);
2888
2889 altered_servers++;
2890 total_servers++;
2891 }
2892 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002893 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002894 } else {
2895 /* the server name is unknown or ambiguous (duplicate names) */
2896 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002897 }
2898 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002899 if (reprocess && px && action) {
2900 /* Now, we know the backend and the action chosen by the user.
2901 * We can safely restart from the first server parameter
2902 * to reprocess them
2903 */
2904 cur_param = st_cur_param;
2905 next_param = st_next_param;
2906 reprocess = 0;
2907 goto reprocess_servers;
2908 }
2909
Cyril Bonté70be45d2010-10-12 00:14:35 +02002910 next_param = cur_param;
2911 }
2912 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002913
2914 if (total_servers == 0) {
2915 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2916 }
2917 else if (altered_servers == 0) {
2918 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2919 }
2920 else if (altered_servers == total_servers) {
2921 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2922 }
2923 else {
2924 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2925 }
2926 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002927 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002928}
2929
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002930/* This function checks whether we need to enable a POST analyser to parse a
2931 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002932 * if it needs to come back again, otherwise non-zero if it finishes. In the
2933 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002934 */
2935int http_handle_stats(struct session *s, struct channel *req)
2936{
2937 struct stats_admin_rule *stats_admin_rule;
2938 struct stream_interface *si = s->rep->prod;
2939 struct http_txn *txn = &s->txn;
2940 struct http_msg *msg = &txn->req;
2941 struct uri_auth *uri = s->be->uri_auth;
2942
2943 /* now check whether we have some admin rules for this request */
2944 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2945 int ret = 1;
2946
2947 if (stats_admin_rule->cond) {
2948 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2949 ret = acl_pass(ret);
2950 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2951 ret = !ret;
2952 }
2953
2954 if (ret) {
2955 /* no rule, or the rule matches */
2956 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
2957 break;
2958 }
2959 }
2960
2961 /* Was the status page requested with a POST ? */
2962 if (unlikely(txn->meth == HTTP_METH_POST)) {
2963 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
2964 if (msg->msg_state < HTTP_MSG_100_SENT) {
2965 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2966 * send an HTTP/1.1 100 Continue intermediate response.
2967 */
2968 if (msg->flags & HTTP_MSGF_VER_11) {
2969 struct hdr_ctx ctx;
2970 ctx.idx = 0;
2971 /* Expect is allowed in 1.1, look for it */
2972 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2973 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2974 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2975 }
2976 }
2977 msg->msg_state = HTTP_MSG_100_SENT;
2978 s->logs.tv_request = now; /* update the request timer to reflect full request */
2979 }
2980 if (!http_process_req_stat_post(si, txn, req))
2981 return 0; /* we need more data */
2982 }
2983 else
2984 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
2985
2986 /* We don't want to land on the posted stats page because a refresh will
2987 * repost the data. We don't want this to happen on accident so we redirect
2988 * the browse to the stats page with a GET.
2989 */
2990 chunk_printf(&trash,
2991 "HTTP/1.0 303 See Other\r\n"
2992 "Cache-Control: no-cache\r\n"
2993 "Content-Type: text/plain\r\n"
2994 "Connection: close\r\n"
2995 "Location: %s;st=%s\r\n"
2996 "\r\n",
2997 uri->uri_prefix,
2998 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
2999 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3000 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3001 stat_status_codes[si->applet.ctx.stats.st_code] :
3002 stat_status_codes[STAT_STATUS_UNKN]);
3003
3004 s->txn.status = 303;
3005 s->logs.tv_request = now;
3006 stream_int_retnclose(req->prod, &trash);
3007 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3008
3009 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3010 s->fe->fe_counters.intercepted_req++;
3011
3012 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3013 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3014 if (!(s->flags & SN_FINST_MASK))
3015 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003016 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003017 return 1;
3018 }
3019
3020 /* OK, let's go on now */
3021
3022 chunk_printf(&trash,
3023 "HTTP/1.0 200 OK\r\n"
3024 "Cache-Control: no-cache\r\n"
3025 "Connection: close\r\n"
3026 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003027 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003028
3029 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3030 chunk_appendf(&trash, "Refresh: %d\r\n",
3031 uri->refresh);
3032
3033 chunk_appendf(&trash, "\r\n");
3034
3035 s->txn.status = 200;
3036 s->logs.tv_request = now;
3037
3038 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3039 s->fe->fe_counters.intercepted_req++;
3040
3041 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3042 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3043 if (!(s->flags & SN_FINST_MASK))
3044 s->flags |= SN_FINST_R;
3045
3046 if (s->txn.meth == HTTP_METH_HEAD) {
3047 /* that's all we return in case of HEAD request, so let's immediately close. */
3048 stream_int_retnclose(req->prod, &trash);
3049 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003050 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003051 return 1;
3052 }
3053
3054 /* OK, push the response and hand over to the stats I/O handler */
3055 bi_putchk(s->rep, &trash);
3056
3057 s->task->nice = -32; /* small boost for HTTP statistics */
3058 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3059 s->target = s->rep->prod->conn->target; // for logging only
3060 s->rep->prod->conn->xprt_ctx = s;
3061 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3062 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003063 return 1;
3064}
3065
Willy Tarreau20b0de52012-12-24 15:45:22 +01003066/* Executes the http-request rules <rules> for session <s>, proxy <px> and
3067 * transaction <txn>. Returns NULL if it executed all rules, or a pointer to
Willy Tarreau1a1e8072012-12-27 10:34:21 +01003068 * the last rule if it had to stop before the end (auth, deny). It may set
3069 * the TX_CLDENY on txn->flags if it encounters a deny rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003070 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003071static struct http_req_rule *
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003072http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
3073{
Willy Tarreauff011f22011-01-06 17:51:27 +01003074 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003075 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003076
Willy Tarreauff011f22011-01-06 17:51:27 +01003077 list_for_each_entry(rule, rules, list) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003078 int ret = 1;
3079
Willy Tarreauff011f22011-01-06 17:51:27 +01003080 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003081 continue;
3082
3083 /* check condition, but only if attached */
Willy Tarreauff011f22011-01-06 17:51:27 +01003084 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003085 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003086 ret = acl_pass(ret);
3087
Willy Tarreauff011f22011-01-06 17:51:27 +01003088 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003089 ret = !ret;
3090 }
3091
3092 if (ret) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01003093 switch (rule->action) {
3094 case HTTP_REQ_ACT_ALLOW:
Willy Tarreau1a1e8072012-12-27 10:34:21 +01003095 return NULL;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003096 case HTTP_REQ_ACT_DENY:
3097 txn->flags |= TX_CLDENY;
3098 return rule;
3099 case HTTP_REQ_ACT_AUTH:
3100 return rule;
3101 case HTTP_REQ_ACT_SET_HDR:
3102 ctx.idx = 0;
3103 /* remove all occurrences of the header */
3104 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3105 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3106 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
3107 }
3108 /* now fall through to header addition */
3109
3110 case HTTP_REQ_ACT_ADD_HDR:
3111 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3112 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3113 trash.len = rule->arg.hdr_add.name_len;
3114 trash.str[trash.len++] = ':';
3115 trash.str[trash.len++] = ' ';
3116 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3117 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
Willy Tarreau8b80f0c2012-12-25 21:53:25 +01003118 break;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003119 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003120 }
3121 }
Willy Tarreau418c1a02012-12-25 20:52:58 +01003122 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003123}
3124
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003125/* This stream analyser runs all HTTP request processing which is common to
3126 * frontends and backends, which means blocking ACLs, filters, connection-close,
3127 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003128 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003129 * either needs more data or wants to immediately abort the request (eg: deny,
3130 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003131 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003132int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003133{
Willy Tarreaud787e662009-07-07 10:14:51 +02003134 struct http_txn *txn = &s->txn;
3135 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003136 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003137 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003138 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003139 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003140 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003141
Willy Tarreau655dce92009-11-08 13:10:58 +01003142 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003143 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003144 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003145 return 0;
3146 }
3147
Willy Tarreau3a816292009-07-07 10:55:49 +02003148 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003149 req->analyse_exp = TICK_ETERNITY;
3150
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003151 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 +02003152 now_ms, __FUNCTION__,
3153 s,
3154 req,
3155 req->rex, req->wex,
3156 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003157 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003158 req->analysers);
3159
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003160 /* first check whether we have some ACLs set to block this request */
3161 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003162 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003163
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003164 ret = acl_pass(ret);
3165 if (cond->pol == ACL_COND_UNLESS)
3166 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003167
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003168 if (ret) {
3169 txn->status = 403;
3170 /* let's log the request time */
3171 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003172 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003173 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003174 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003175 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003176 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003177
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003178 /* just in case we have some per-backend tracking */
3179 session_inc_be_http_req_ctr(s);
3180
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003181 /* evaluate http-request rules */
Willy Tarreauff011f22011-01-06 17:51:27 +01003182 http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003183
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003184 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003185 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003186 do_stats = stats_check_uri(s->rep->prod, txn, px);
3187 if (do_stats)
Willy Tarreauff011f22011-01-06 17:51:27 +01003188 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 +01003189 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003190 else
3191 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003192
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003193 /* return a 403 if either rule has blocked */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003194 if (txn->flags & TX_CLDENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003195 txn->status = 403;
3196 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003197 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003198 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003199 s->fe->fe_counters.denied_req++;
3200 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3201 s->be->be_counters.denied_req++;
3202 if (s->listener->counters)
3203 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003204 goto return_prx_cond;
3205 }
3206
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003207 /* try headers filters */
3208 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003209 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003210 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003211
Willy Tarreau59234e92008-11-30 23:51:27 +01003212 /* has the request been denied ? */
3213 if (txn->flags & TX_CLDENY) {
3214 /* no need to go further */
3215 txn->status = 403;
3216 /* let's log the request time */
3217 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003218 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003219 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003220 goto return_prx_cond;
3221 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003222
3223 /* When a connection is tarpitted, we use the tarpit timeout,
3224 * which may be the same as the connect timeout if unspecified.
3225 * If unset, then set it to zero because we really want it to
3226 * eventually expire. We build the tarpit as an analyser.
3227 */
3228 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003229 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003230 /* wipe the request out so that we can drop the connection early
3231 * if the client closes first.
3232 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003233 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003234 req->analysers = 0; /* remove switching rules etc... */
3235 req->analysers |= AN_REQ_HTTP_TARPIT;
3236 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3237 if (!req->analyse_exp)
3238 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003239 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003240 return 1;
3241 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003242 }
Willy Tarreau06619262006-12-17 08:37:22 +01003243
Willy Tarreau5b154472009-12-21 20:11:07 +01003244 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3245 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003246 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3247 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003248 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3249 * avoid to redo the same work if FE and BE have the same settings (common).
3250 * The method consists in checking if options changed between the two calls
3251 * (implying that either one is non-null, or one of them is non-null and we
3252 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003253 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003254
Willy Tarreaudc008c52010-02-01 16:20:08 +01003255 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3256 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3257 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3258 (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 +01003259 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003260
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003261 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3262 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003263 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003264 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3265 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003266 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003267 tmp = TX_CON_WANT_CLO;
3268
Willy Tarreau5b154472009-12-21 20:11:07 +01003269 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3270 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003271
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003272 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3273 /* parse the Connection header and possibly clean it */
3274 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003275 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003276 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3277 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003278 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003279 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003280 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003281 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003282 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003283
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003284 /* check if client or config asks for explicit close in KAL/SCL */
3285 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3286 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3287 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003288 (!(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 +01003289 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003290 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003291 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003292 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3293 }
Willy Tarreau78599912009-10-17 20:12:21 +02003294
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003295 /* we can be blocked here because the request needs to be authenticated,
3296 * either to pass or to access stats.
3297 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003298 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003299 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003300
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003301 if (!realm)
3302 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3303
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003304 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003305 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003306 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003307 /* on 401 we still count one error, because normal browsing
3308 * won't significantly increase the counter but brute force
3309 * attempts will.
3310 */
3311 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003312 goto return_prx_cond;
3313 }
3314
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003315 /* add request headers from the rule sets in the same order */
3316 list_for_each_entry(wl, &px->req_add, list) {
3317 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003318 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003319 ret = acl_pass(ret);
3320 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3321 ret = !ret;
3322 if (!ret)
3323 continue;
3324 }
3325
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003326 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003327 goto return_bad_req;
3328 }
3329
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003330 if (unlikely(do_stats)) {
3331 /* process the stats request now */
3332 if (!http_handle_stats(s, req)) {
3333 /* we need more data, let's come back here later */
3334 req->analysers |= an_bit;
3335 channel_dont_connect(req);
Cyril Bonté70be45d2010-10-12 00:14:35 +02003336 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003337 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003338 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003339
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003340 /* check whether we have some ACLs set to redirect this request */
3341 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003342 int ret = ACL_PAT_PASS;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003343
Willy Tarreauf285f542010-01-03 20:03:03 +01003344 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003345 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003346 ret = acl_pass(ret);
3347 if (rule->cond->pol == ACL_COND_UNLESS)
3348 ret = !ret;
3349 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003350
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003351 if (ret) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003352 const char *msg_fmt;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003353
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003354 /* build redirect message */
3355 switch(rule->code) {
3356 case 303:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003357 msg_fmt = HTTP_303;
3358 break;
3359 case 301:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003360 msg_fmt = HTTP_301;
3361 break;
3362 case 302:
3363 default:
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003364 msg_fmt = HTTP_302;
3365 break;
3366 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003367
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003368 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003369 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003370
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003371 switch(rule->type) {
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003372 case REDIRECT_TYPE_SCHEME: {
3373 const char *path;
3374 const char *host;
3375 struct hdr_ctx ctx;
3376 int pathlen;
3377 int hostlen;
3378
3379 host = "";
3380 hostlen = 0;
3381 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02003382 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 +02003383 host = ctx.line + ctx.val;
3384 hostlen = ctx.vlen;
3385 }
3386
3387 path = http_get_path(txn);
3388 /* build message using path */
3389 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003390 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003391 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3392 int qs = 0;
3393 while (qs < pathlen) {
3394 if (path[qs] == '?') {
3395 pathlen = qs;
3396 break;
3397 }
3398 qs++;
3399 }
3400 }
3401 } else {
3402 path = "/";
3403 pathlen = 1;
3404 }
3405
3406 /* check if we can add scheme + "://" + host + path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003407 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003408 goto return_bad_req;
3409
3410 /* add scheme */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003411 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3412 trash.len += rule->rdr_len;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003413
3414 /* add "://" */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003415 memcpy(trash.str + trash.len, "://", 3);
3416 trash.len += 3;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003417
3418 /* add host */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003419 memcpy(trash.str + trash.len, host, hostlen);
3420 trash.len += hostlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003421
3422 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003423 memcpy(trash.str + trash.len, path, pathlen);
3424 trash.len += pathlen;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003425
3426 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003427 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003428 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003429 if (trash.len > trash.size - 5)
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003430 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003431 trash.str[trash.len] = '/';
3432 trash.len++;
Willy Tarreau2e1dca82012-09-12 08:43:15 +02003433 }
3434
3435 break;
3436 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003437 case REDIRECT_TYPE_PREFIX: {
3438 const char *path;
3439 int pathlen;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003440
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003441 path = http_get_path(txn);
3442 /* build message using path */
3443 if (path) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003444 pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003445 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3446 int qs = 0;
3447 while (qs < pathlen) {
3448 if (path[qs] == '?') {
3449 pathlen = qs;
3450 break;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003451 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003452 qs++;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003453 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003454 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003455 } else {
3456 path = "/";
3457 pathlen = 1;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003458 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003459
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003460 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003461 goto return_bad_req;
3462
3463 /* add prefix. Note that if prefix == "/", we don't want to
3464 * add anything, otherwise it makes it hard for the user to
3465 * configure a self-redirection.
3466 */
3467 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003468 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3469 trash.len += rule->rdr_len;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003470 }
3471
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003472 /* add path */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003473 memcpy(trash.str + trash.len, path, pathlen);
3474 trash.len += pathlen;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003475
3476 /* append a slash at the end of the location is needed and missing */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003477 if (trash.len && trash.str[trash.len - 1] != '/' &&
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003478 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003479 if (trash.len > trash.size - 5)
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003480 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003481 trash.str[trash.len] = '/';
3482 trash.len++;
Willy Tarreau81e3b4f2010-01-10 00:42:19 +01003483 }
3484
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003485 break;
3486 }
3487 case REDIRECT_TYPE_LOCATION:
3488 default:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003489 if (trash.len + rule->rdr_len > trash.size - 4)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003490 goto return_bad_req;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003491
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003492 /* add location */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003493 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3494 trash.len += rule->rdr_len;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003495 break;
3496 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003497
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003498 if (rule->cookie_len) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003499 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3500 trash.len += 14;
3501 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3502 trash.len += rule->cookie_len;
3503 memcpy(trash.str + trash.len, "\r\n", 2);
3504 trash.len += 2;
Willy Tarreau06b917c2009-07-06 16:34:52 +02003505 }
Willy Tarreau06b917c2009-07-06 16:34:52 +02003506
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003507 /* add end of headers and the keep-alive/close status.
3508 * We may choose to set keep-alive if the Location begins
3509 * with a slash, because the client will come back to the
3510 * same server.
3511 */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003512 txn->status = rule->code;
3513 /* let's log the request time */
3514 s->logs.tv_request = now;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003515
3516 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003517 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3518 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003519 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3520 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3521 /* keep-alive possible */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003522 if (!(msg->flags & HTTP_MSGF_VER_11)) {
Willy Tarreau88d349d2010-01-25 12:15:43 +01003523 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003524 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3525 trash.len += 30;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003526 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003527 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3528 trash.len += 24;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003529 }
Willy Tarreau75661452010-01-10 10:35:01 +01003530 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003531 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3532 trash.len += 4;
3533 bo_inject(req->prod->ob, trash.str, trash.len);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003534 /* "eat" the request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003535 bi_fast_delete(req->buf, msg->sov);
Willy Tarreau26927362012-05-18 23:22:52 +02003536 msg->sov = 0;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003537 req->analysers = AN_REQ_HTTP_XFER_BODY;
Willy Tarreau9300fb22010-01-05 00:58:24 +01003538 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3539 txn->req.msg_state = HTTP_MSG_CLOSED;
3540 txn->rsp.msg_state = HTTP_MSG_DONE;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003541 break;
3542 } else {
3543 /* keep-alive not possible */
Willy Tarreau88d349d2010-01-25 12:15:43 +01003544 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003545 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3546 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003547 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003548 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3549 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +01003550 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003551 stream_int_retnclose(req->prod, &trash);
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003552 goto return_prx_cond;
3553 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003554 }
3555 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003556
Willy Tarreau2be39392010-01-03 17:24:51 +01003557 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3558 * If this happens, then the data will not come immediately, so we must
3559 * send all what we have without waiting. Note that due to the small gain
3560 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003561 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003562 * itself once used.
3563 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003564 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003565
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003566 /* that's OK for us now, let's move on to next analysers */
3567 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003568
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003569 return_bad_req:
3570 /* We centralize bad requests processing here */
3571 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3572 /* we detected a parsing error. We want to archive this request
3573 * in the dedicated proxy area for later troubleshooting.
3574 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003575 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003576 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003577
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003578 txn->req.msg_state = HTTP_MSG_ERROR;
3579 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003580 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003581
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003582 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003583 if (s->listener->counters)
3584 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003585
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003586 return_prx_cond:
3587 if (!(s->flags & SN_ERR_MASK))
3588 s->flags |= SN_ERR_PRXCOND;
3589 if (!(s->flags & SN_FINST_MASK))
3590 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003591
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003592 req->analysers = 0;
3593 req->analyse_exp = TICK_ETERNITY;
3594 return 0;
3595}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003596
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003597/* This function performs all the processing enabled for the current request.
3598 * It returns 1 if the processing can continue on next analysers, or zero if it
3599 * needs more data, encounters an error, or wants to immediately abort the
3600 * request. It relies on buffers flags, and updates s->req->analysers.
3601 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003602int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003603{
3604 struct http_txn *txn = &s->txn;
3605 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003606
Willy Tarreau655dce92009-11-08 13:10:58 +01003607 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003608 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003609 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003610 return 0;
3611 }
3612
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003613 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 +02003614 now_ms, __FUNCTION__,
3615 s,
3616 req,
3617 req->rex, req->wex,
3618 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003619 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003620 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003621
William Lallemand82fe75c2012-10-23 10:25:10 +02003622 if (s->fe->comp || s->be->comp)
3623 select_compression_request_header(s, req->buf);
3624
Willy Tarreau59234e92008-11-30 23:51:27 +01003625 /*
3626 * Right now, we know that we have processed the entire headers
3627 * and that unwanted requests have been filtered out. We can do
3628 * whatever we want with the remaining request. Also, now we
3629 * may have separate values for ->fe, ->be.
3630 */
Willy Tarreau06619262006-12-17 08:37:22 +01003631
Willy Tarreau59234e92008-11-30 23:51:27 +01003632 /*
3633 * If HTTP PROXY is set we simply get remote server address
3634 * parsing incoming request.
3635 */
3636 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003637 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 +01003638 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003639
Willy Tarreau59234e92008-11-30 23:51:27 +01003640 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003641 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003642 * Note that doing so might move headers in the request, but
3643 * the fields will stay coherent and the URI will not move.
3644 * This should only be performed in the backend.
3645 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003646 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003647 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3648 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003649
Willy Tarreau59234e92008-11-30 23:51:27 +01003650 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003651 * 8: the appsession cookie was looked up very early in 1.2,
3652 * so let's do the same now.
3653 */
3654
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003655 /* It needs to look into the URI unless persistence must be ignored */
3656 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003657 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 +01003658 }
3659
William Lallemanda73203e2012-03-12 12:48:57 +01003660 /* add unique-id if "header-unique-id" is specified */
3661
3662 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3663 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3664
3665 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003666 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3667 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003668 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003669 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003670 goto return_bad_req;
3671 }
3672
Cyril Bontéb21570a2009-11-29 20:04:48 +01003673 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003674 * 9: add X-Forwarded-For if either the frontend or the backend
3675 * asks for it.
3676 */
3677 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003678 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003679 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003680 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3681 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003682 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003683 /* The header is set to be added only if none is present
3684 * and we found it, so don't do anything.
3685 */
3686 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003687 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003688 /* Add an X-Forwarded-For header unless the source IP is
3689 * in the 'except' network range.
3690 */
3691 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003692 (((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 +01003693 != s->fe->except_net.s_addr) &&
3694 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003695 (((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 +01003696 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003697 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003698 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003699 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003700
3701 /* Note: we rely on the backend to get the header name to be used for
3702 * x-forwarded-for, because the header is really meant for the backends.
3703 * However, if the backend did not specify any option, we have to rely
3704 * on the frontend's header name.
3705 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003706 if (s->be->fwdfor_hdr_len) {
3707 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003708 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003709 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003710 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003711 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003712 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003713 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003714
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003715 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003716 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003717 }
3718 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003719 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003720 /* FIXME: for the sake of completeness, we should also support
3721 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003722 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003723 int len;
3724 char pn[INET6_ADDRSTRLEN];
3725 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003726 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003727 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003728
Willy Tarreau59234e92008-11-30 23:51:27 +01003729 /* Note: we rely on the backend to get the header name to be used for
3730 * x-forwarded-for, because the header is really meant for the backends.
3731 * However, if the backend did not specify any option, we have to rely
3732 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003733 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003734 if (s->be->fwdfor_hdr_len) {
3735 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003736 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003737 } else {
3738 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003739 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003740 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003741 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003742
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003743 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003744 goto return_bad_req;
3745 }
3746 }
3747
3748 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003749 * 10: add X-Original-To if either the frontend or the backend
3750 * asks for it.
3751 */
3752 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3753
3754 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003755 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003756 /* Add an X-Original-To header unless the destination IP is
3757 * in the 'except' network range.
3758 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003759 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003760
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003761 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003762 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003763 (((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 +02003764 != s->fe->except_to.s_addr) &&
3765 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003766 (((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 +02003767 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003768 int len;
3769 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003770 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003771
3772 /* Note: we rely on the backend to get the header name to be used for
3773 * x-original-to, because the header is really meant for the backends.
3774 * However, if the backend did not specify any option, we have to rely
3775 * on the frontend's header name.
3776 */
3777 if (s->be->orgto_hdr_len) {
3778 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003779 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003780 } else {
3781 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003782 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003783 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003784 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003785
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003786 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003787 goto return_bad_req;
3788 }
3789 }
3790 }
3791
Willy Tarreau50fc7772012-11-11 22:19:57 +01003792 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3793 * If an "Upgrade" token is found, the header is left untouched in order not to have
3794 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3795 * "Upgrade" is present in the Connection header.
3796 */
3797 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3798 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3799 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003800 unsigned int want_flags = 0;
3801
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003802 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003803 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3804 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3805 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003806 want_flags |= TX_CON_CLO_SET;
3807 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003808 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3809 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3810 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003811 want_flags |= TX_CON_KAL_SET;
3812 }
3813
3814 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003815 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003816 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003817
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003818
Willy Tarreau522d6c02009-12-06 18:49:18 +01003819 /* If we have no server assigned yet and we're balancing on url_param
3820 * with a POST request, we may be interested in checking the body for
3821 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003822 */
3823 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3824 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003825 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003826 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003827 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003828 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003829 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003830
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003831 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003832 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003833#ifdef TCP_QUICKACK
3834 /* We expect some data from the client. Unless we know for sure
3835 * we already have a full request, we have to re-enable quick-ack
3836 * in case we previously disabled it, otherwise we might cause
3837 * the client to delay further data.
3838 */
3839 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003840 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003841 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003842 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003843#endif
3844 }
Willy Tarreau03945942009-12-22 16:50:27 +01003845
Willy Tarreau59234e92008-11-30 23:51:27 +01003846 /*************************************************************
3847 * OK, that's finished for the headers. We have done what we *
3848 * could. Let's switch to the DATA state. *
3849 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003850 req->analyse_exp = TICK_ETERNITY;
3851 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003852
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003853 /* if the server closes the connection, we want to immediately react
3854 * and close the socket to save packets and syscalls.
3855 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003856 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3857 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003858
Willy Tarreau59234e92008-11-30 23:51:27 +01003859 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003860 /* OK let's go on with the BODY now */
3861 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003862
Willy Tarreau59234e92008-11-30 23:51:27 +01003863 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003864 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003865 /* we detected a parsing error. We want to archive this request
3866 * in the dedicated proxy area for later troubleshooting.
3867 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003868 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003869 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003870
Willy Tarreau59234e92008-11-30 23:51:27 +01003871 txn->req.msg_state = HTTP_MSG_ERROR;
3872 txn->status = 400;
3873 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003874 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003875
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003876 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003877 if (s->listener->counters)
3878 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003879
Willy Tarreau59234e92008-11-30 23:51:27 +01003880 if (!(s->flags & SN_ERR_MASK))
3881 s->flags |= SN_ERR_PRXCOND;
3882 if (!(s->flags & SN_FINST_MASK))
3883 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003884 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003885}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003886
Willy Tarreau60b85b02008-11-30 23:28:40 +01003887/* This function is an analyser which processes the HTTP tarpit. It always
3888 * returns zero, at the beginning because it prevents any other processing
3889 * from occurring, and at the end because it terminates the request.
3890 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003891int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003892{
3893 struct http_txn *txn = &s->txn;
3894
3895 /* This connection is being tarpitted. The CLIENT side has
3896 * already set the connect expiration date to the right
3897 * timeout. We just have to check that the client is still
3898 * there and that the timeout has not expired.
3899 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003900 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003901 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003902 !tick_is_expired(req->analyse_exp, now_ms))
3903 return 0;
3904
3905 /* We will set the queue timer to the time spent, just for
3906 * logging purposes. We fake a 500 server error, so that the
3907 * attacker will not suspect his connection has been tarpitted.
3908 * It will not cause trouble to the logs because we can exclude
3909 * the tarpitted connections by filtering on the 'PT' status flags.
3910 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01003911 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
3912
3913 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003914 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02003915 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01003916
3917 req->analysers = 0;
3918 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003919
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003920 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003921 if (s->listener->counters)
3922 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01003923
Willy Tarreau60b85b02008-11-30 23:28:40 +01003924 if (!(s->flags & SN_ERR_MASK))
3925 s->flags |= SN_ERR_PRXCOND;
3926 if (!(s->flags & SN_FINST_MASK))
3927 s->flags |= SN_FINST_T;
3928 return 0;
3929}
3930
Willy Tarreaud34af782008-11-30 23:36:37 +01003931/* This function is an analyser which processes the HTTP request body. It looks
3932 * for parameters to be used for the load balancing algorithm (url_param). It
3933 * must only be called after the standard HTTP request processing has occurred,
3934 * because it expects the request to be parsed. It returns zero if it needs to
3935 * read more data, or 1 once it has completed its analysis.
3936 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003937int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01003938{
Willy Tarreau522d6c02009-12-06 18:49:18 +01003939 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01003940 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01003941 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01003942
3943 /* We have to parse the HTTP request body to find any required data.
3944 * "balance url_param check_post" should have been the only way to get
3945 * into this. We were brought here after HTTP header analysis, so all
3946 * related structures are ready.
3947 */
3948
Willy Tarreau522d6c02009-12-06 18:49:18 +01003949 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
3950 goto missing_data;
3951
3952 if (msg->msg_state < HTTP_MSG_100_SENT) {
3953 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
3954 * send an HTTP/1.1 100 Continue intermediate response.
3955 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003956 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01003957 struct hdr_ctx ctx;
3958 ctx.idx = 0;
3959 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02003960 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003961 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02003962 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003963 }
3964 }
3965 msg->msg_state = HTTP_MSG_100_SENT;
3966 }
3967
3968 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003969 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02003970 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02003971 * is still null. We must save the body in msg->next because it
3972 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01003973 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01003974 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01003975
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003976 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01003977 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
3978 else
3979 msg->msg_state = HTTP_MSG_DATA;
3980 }
3981
3982 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01003983 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01003984 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01003985 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01003986 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01003987 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01003988
Willy Tarreau115acb92009-12-26 13:56:06 +01003989 if (!ret)
3990 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003991 else if (ret < 0) {
3992 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003993 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02003994 }
Willy Tarreaud34af782008-11-30 23:36:37 +01003995 }
3996
Willy Tarreaud98cf932009-12-27 22:54:55 +01003997 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01003998 * We have the first data byte is in msg->sov. We're waiting for at
3999 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004000 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004001
Willy Tarreau124d9912011-03-01 20:30:48 +01004002 if (msg->body_len < limit)
4003 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004004
Willy Tarreau9b28e032012-10-12 23:49:43 +02004005 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004006 goto http_end;
4007
4008 missing_data:
4009 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004010 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004011 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004012 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004013 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004014
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004015 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004016 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004017 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004018
4019 if (!(s->flags & SN_ERR_MASK))
4020 s->flags |= SN_ERR_CLITO;
4021 if (!(s->flags & SN_FINST_MASK))
4022 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004023 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004024 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004025
4026 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004027 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004028 /* Not enough data. We'll re-use the http-request
4029 * timeout here. Ideally, we should set the timeout
4030 * relative to the accept() date. We just set the
4031 * request timeout once at the beginning of the
4032 * request.
4033 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004034 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004035 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004036 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004037 return 0;
4038 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004039
4040 http_end:
4041 /* The situation will not evolve, so let's give up on the analysis. */
4042 s->logs.tv_request = now; /* update the request timer to reflect full request */
4043 req->analysers &= ~an_bit;
4044 req->analyse_exp = TICK_ETERNITY;
4045 return 1;
4046
4047 return_bad_req: /* let's centralize all bad requests */
4048 txn->req.msg_state = HTTP_MSG_ERROR;
4049 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004050 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004051
Willy Tarreau79ebac62010-06-07 13:47:49 +02004052 if (!(s->flags & SN_ERR_MASK))
4053 s->flags |= SN_ERR_PRXCOND;
4054 if (!(s->flags & SN_FINST_MASK))
4055 s->flags |= SN_FINST_R;
4056
Willy Tarreau522d6c02009-12-06 18:49:18 +01004057 return_err_msg:
4058 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004059 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004060 if (s->listener->counters)
4061 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004062 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004063}
4064
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004065/* send a server's name with an outgoing request over an established connection.
4066 * Note: this function is designed to be called once the request has been scheduled
4067 * for being forwarded. This is the reason why it rewinds the buffer before
4068 * proceeding.
4069 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004070int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004071
4072 struct hdr_ctx ctx;
4073
Mark Lamourinec2247f02012-01-04 13:02:01 -05004074 char *hdr_name = be->server_id_hdr_name;
4075 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004076 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004077 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004078 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004079
William Lallemandd9e90662012-01-30 17:27:17 +01004080 ctx.idx = 0;
4081
Willy Tarreau9b28e032012-10-12 23:49:43 +02004082 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004083 if (old_o) {
4084 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004085 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004086 }
4087
Willy Tarreau9b28e032012-10-12 23:49:43 +02004088 old_i = chn->buf->i;
4089 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 -05004090 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004091 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004092 }
4093
4094 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004095 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004096 memcpy(hdr_val, hdr_name, hdr_name_len);
4097 hdr_val += hdr_name_len;
4098 *hdr_val++ = ':';
4099 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004100 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4101 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004102
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004103 if (old_o) {
4104 /* If this was a forwarded request, we must readjust the amount of
4105 * data to be forwarded in order to take into account the size
4106 * variations.
4107 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004108 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004109 }
4110
Mark Lamourinec2247f02012-01-04 13:02:01 -05004111 return 0;
4112}
4113
Willy Tarreau610ecce2010-01-04 21:15:02 +01004114/* Terminate current transaction and prepare a new one. This is very tricky
4115 * right now but it works.
4116 */
4117void http_end_txn_clean_session(struct session *s)
4118{
4119 /* FIXME: We need a more portable way of releasing a backend's and a
4120 * server's connections. We need a safer way to reinitialize buffer
4121 * flags. We also need a more accurate method for computing per-request
4122 * data.
4123 */
4124 http_silent_debug(__LINE__, s);
4125
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004126 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004127 si_shutr(s->req->cons);
4128 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004129
4130 http_silent_debug(__LINE__, s);
4131
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004132 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004133 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004134 if (unlikely(s->srv_conn))
4135 sess_change_server(s, NULL);
4136 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004137
4138 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4139 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004140 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004141
4142 if (s->txn.status) {
4143 int n;
4144
4145 n = s->txn.status / 100;
4146 if (n < 1 || n > 5)
4147 n = 0;
4148
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004149 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004150 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004151 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004152 s->fe->fe_counters.p.http.comp_rsp++;
4153 }
Willy Tarreau24657792010-02-26 10:30:28 +01004154 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004155 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004156 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004157 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004158 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004159 s->be->be_counters.p.http.comp_rsp++;
4160 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004161 }
4162
4163 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004164 s->logs.bytes_in -= s->req->buf->i;
4165 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004166
4167 /* let's do a final log if we need it */
4168 if (s->logs.logwait &&
4169 !(s->flags & SN_MONITOR) &&
4170 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4171 s->do_log(s);
4172 }
4173
4174 s->logs.accept_date = date; /* user-visible date for logging */
4175 s->logs.tv_accept = now; /* corrected date for internal use */
4176 tv_zero(&s->logs.tv_request);
4177 s->logs.t_queue = -1;
4178 s->logs.t_connect = -1;
4179 s->logs.t_data = -1;
4180 s->logs.t_close = 0;
4181 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4182 s->logs.srv_queue_size = 0; /* we will get this number soon */
4183
Willy Tarreau9b28e032012-10-12 23:49:43 +02004184 s->logs.bytes_in = s->req->total = s->req->buf->i;
4185 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004186
4187 if (s->pend_pos)
4188 pendconn_free(s->pend_pos);
4189
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004190 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004191 if (s->flags & SN_CURR_SESS) {
4192 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004193 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004194 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004195 if (may_dequeue_tasks(objt_server(s->target), s->be))
4196 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004197 }
4198
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004199 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004200
4201 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004202 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4203 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004204 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004205 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004206 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004207 s->req->cons->err_loc = NULL;
4208 s->req->cons->exp = TICK_ETERNITY;
4209 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004210 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4211 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 +02004212 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 +01004213 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004214
4215 if (s->flags & SN_COMP_READY)
4216 s->comp_algo->end(&s->comp_ctx);
4217 s->comp_algo = NULL;
4218 s->flags &= ~SN_COMP_READY;
4219
Willy Tarreau610ecce2010-01-04 21:15:02 +01004220 s->txn.meth = 0;
4221 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004222 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004223 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004224 s->req->cons->flags |= SI_FL_INDEP_STR;
4225
Willy Tarreau96e31212011-05-30 18:10:30 +02004226 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004227 s->req->flags |= CF_NEVER_WAIT;
4228 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004229 }
4230
Willy Tarreau610ecce2010-01-04 21:15:02 +01004231 /* if the request buffer is not empty, it means we're
4232 * about to process another request, so send pending
4233 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004234 * Just don't do this if the buffer is close to be full,
4235 * because the request will wait for it to flush a little
4236 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004237 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004238 if (s->req->buf->i) {
4239 if (s->rep->buf->o &&
4240 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4241 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004242 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004243 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004244
4245 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004246 channel_auto_read(s->req);
4247 channel_auto_close(s->req);
4248 channel_auto_read(s->rep);
4249 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004250
Willy Tarreau342b11c2010-11-24 16:22:09 +01004251 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004252 s->rep->analysers = 0;
4253
4254 http_silent_debug(__LINE__, s);
4255}
4256
4257
4258/* This function updates the request state machine according to the response
4259 * state machine and buffer flags. It returns 1 if it changes anything (flag
4260 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4261 * it is only used to find when a request/response couple is complete. Both
4262 * this function and its equivalent should loop until both return zero. It
4263 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4264 */
4265int http_sync_req_state(struct session *s)
4266{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004267 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004268 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004269 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004270 unsigned int old_state = txn->req.msg_state;
4271
4272 http_silent_debug(__LINE__, s);
4273 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4274 return 0;
4275
4276 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004277 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004278 * We can shut the read side unless we want to abort_on_close,
4279 * or we have a POST request. The issue with POST requests is
4280 * that some browsers still send a CRLF after the request, and
4281 * this CRLF must be read so that it does not remain in the kernel
4282 * buffers, otherwise a close could cause an RST on some systems
4283 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004284 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004285 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004286 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004287
Willy Tarreau40f151a2012-12-20 12:10:09 +01004288 /* if the server closes the connection, we want to immediately react
4289 * and close the socket to save packets and syscalls.
4290 */
4291 chn->cons->flags |= SI_FL_NOHALF;
4292
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4294 goto wait_other_side;
4295
4296 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4297 /* The server has not finished to respond, so we
4298 * don't want to move in order not to upset it.
4299 */
4300 goto wait_other_side;
4301 }
4302
4303 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4304 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004305 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004306 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004307 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004308 goto wait_other_side;
4309 }
4310
4311 /* When we get here, it means that both the request and the
4312 * response have finished receiving. Depending on the connection
4313 * mode, we'll have to wait for the last bytes to leave in either
4314 * direction, and sometimes for a close to be effective.
4315 */
4316
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004317 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4318 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004319 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4320 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004321 }
4322 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4323 /* Option forceclose is set, or either side wants to close,
4324 * let's enforce it now that we're not expecting any new
4325 * data to come. The caller knows the session is complete
4326 * once both states are CLOSED.
4327 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004328 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4329 channel_shutr_now(chn);
4330 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004331 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004332 }
4333 else {
4334 /* The last possible modes are keep-alive and tunnel. Since tunnel
4335 * mode does not set the body analyser, we can't reach this place
4336 * in tunnel mode, so we're left with keep-alive only.
4337 * This mode is currently not implemented, we switch to tunnel mode.
4338 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004339 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004340 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004341 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004342 }
4343
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004344 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004345 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004346 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004347
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004348 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004349 txn->req.msg_state = HTTP_MSG_CLOSING;
4350 goto http_msg_closing;
4351 }
4352 else {
4353 txn->req.msg_state = HTTP_MSG_CLOSED;
4354 goto http_msg_closed;
4355 }
4356 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004357 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004358 }
4359
4360 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4361 http_msg_closing:
4362 /* nothing else to forward, just waiting for the output buffer
4363 * to be empty and for the shutw_now to take effect.
4364 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004365 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004366 txn->req.msg_state = HTTP_MSG_CLOSED;
4367 goto http_msg_closed;
4368 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004369 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004370 txn->req.msg_state = HTTP_MSG_ERROR;
4371 goto wait_other_side;
4372 }
4373 }
4374
4375 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4376 http_msg_closed:
4377 goto wait_other_side;
4378 }
4379
4380 wait_other_side:
4381 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004382 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004383}
4384
4385
4386/* This function updates the response state machine according to the request
4387 * state machine and buffer flags. It returns 1 if it changes anything (flag
4388 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4389 * it is only used to find when a request/response couple is complete. Both
4390 * this function and its equivalent should loop until both return zero. It
4391 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4392 */
4393int http_sync_res_state(struct session *s)
4394{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004395 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004396 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004397 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004398 unsigned int old_state = txn->rsp.msg_state;
4399
4400 http_silent_debug(__LINE__, s);
4401 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4402 return 0;
4403
4404 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4405 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004406 * still monitor the server connection for a possible close
4407 * while the request is being uploaded, so we don't disable
4408 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004409 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004410 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004411
4412 if (txn->req.msg_state == HTTP_MSG_ERROR)
4413 goto wait_other_side;
4414
4415 if (txn->req.msg_state < HTTP_MSG_DONE) {
4416 /* The client seems to still be sending data, probably
4417 * because we got an error response during an upload.
4418 * We have the choice of either breaking the connection
4419 * or letting it pass through. Let's do the later.
4420 */
4421 goto wait_other_side;
4422 }
4423
4424 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4425 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004426 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004427 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004428 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004429 goto wait_other_side;
4430 }
4431
4432 /* When we get here, it means that both the request and the
4433 * response have finished receiving. Depending on the connection
4434 * mode, we'll have to wait for the last bytes to leave in either
4435 * direction, and sometimes for a close to be effective.
4436 */
4437
4438 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4439 /* Server-close mode : shut read and wait for the request
4440 * side to close its output buffer. The caller will detect
4441 * when we're in DONE and the other is in CLOSED and will
4442 * catch that for the final cleanup.
4443 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004444 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4445 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004446 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004447 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4448 /* Option forceclose is set, or either side wants to close,
4449 * let's enforce it now that we're not expecting any new
4450 * data to come. The caller knows the session is complete
4451 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004452 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004453 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4454 channel_shutr_now(chn);
4455 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004456 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004457 }
4458 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004459 /* The last possible modes are keep-alive and tunnel. Since tunnel
4460 * mode does not set the body analyser, we can't reach this place
4461 * in tunnel mode, so we're left with keep-alive only.
4462 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004463 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004464 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004465 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004466 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004467 }
4468
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004469 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004470 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004471 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004472 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4473 goto http_msg_closing;
4474 }
4475 else {
4476 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4477 goto http_msg_closed;
4478 }
4479 }
4480 goto wait_other_side;
4481 }
4482
4483 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4484 http_msg_closing:
4485 /* nothing else to forward, just waiting for the output buffer
4486 * to be empty and for the shutw_now to take effect.
4487 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004488 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004489 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4490 goto http_msg_closed;
4491 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004492 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004493 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004494 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004495 if (objt_server(s->target))
4496 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004497 goto wait_other_side;
4498 }
4499 }
4500
4501 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4502 http_msg_closed:
4503 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004504 bi_erase(chn);
4505 channel_auto_close(chn);
4506 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004507 goto wait_other_side;
4508 }
4509
4510 wait_other_side:
4511 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004512 /* We force the response to leave immediately if we're waiting for the
4513 * other side, since there is no pending shutdown to push it out.
4514 */
4515 if (!channel_is_empty(chn))
4516 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004517 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004518}
4519
4520
4521/* Resync the request and response state machines. Return 1 if either state
4522 * changes.
4523 */
4524int http_resync_states(struct session *s)
4525{
4526 struct http_txn *txn = &s->txn;
4527 int old_req_state = txn->req.msg_state;
4528 int old_res_state = txn->rsp.msg_state;
4529
4530 http_silent_debug(__LINE__, s);
4531 http_sync_req_state(s);
4532 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004533 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004534 if (!http_sync_res_state(s))
4535 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004536 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004537 if (!http_sync_req_state(s))
4538 break;
4539 }
4540 http_silent_debug(__LINE__, s);
4541 /* OK, both state machines agree on a compatible state.
4542 * There are a few cases we're interested in :
4543 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4544 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4545 * directions, so let's simply disable both analysers.
4546 * - HTTP_MSG_CLOSED on the response only means we must abort the
4547 * request.
4548 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4549 * with server-close mode means we've completed one request and we
4550 * must re-initialize the server connection.
4551 */
4552
4553 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4554 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4555 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4556 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4557 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004558 channel_auto_close(s->req);
4559 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004560 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004561 channel_auto_close(s->rep);
4562 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004563 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004564 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4565 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004566 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004567 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004568 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004569 channel_auto_close(s->rep);
4570 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004571 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004572 channel_abort(s->req);
4573 channel_auto_close(s->req);
4574 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004575 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004576 }
4577 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4578 txn->rsp.msg_state == HTTP_MSG_DONE &&
4579 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4580 /* server-close: terminate this server connection and
4581 * reinitialize a fresh-new transaction.
4582 */
4583 http_end_txn_clean_session(s);
4584 }
4585
4586 http_silent_debug(__LINE__, s);
4587 return txn->req.msg_state != old_req_state ||
4588 txn->rsp.msg_state != old_res_state;
4589}
4590
Willy Tarreaud98cf932009-12-27 22:54:55 +01004591/* This function is an analyser which forwards request body (including chunk
4592 * sizes if any). It is called as soon as we must forward, even if we forward
4593 * zero byte. The only situation where it must not be called is when we're in
4594 * tunnel mode and we want to forward till the close. It's used both to forward
4595 * remaining data and to resync after end of body. It expects the msg_state to
4596 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4597 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004598 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004599 * bytes of pending data + the headers if not already done (between sol and sov).
4600 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004601 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004602int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004603{
4604 struct http_txn *txn = &s->txn;
4605 struct http_msg *msg = &s->txn.req;
4606
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004607 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4608 return 0;
4609
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004610 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004611 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004612 /* Output closed while we were sending data. We must abort and
4613 * wake the other side up.
4614 */
4615 msg->msg_state = HTTP_MSG_ERROR;
4616 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004617 return 1;
4618 }
4619
Willy Tarreau4fe41902010-06-07 22:27:41 +02004620 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004621 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004622
4623 /* Note that we don't have to send 100-continue back because we don't
4624 * need the data to complete our job, and it's up to the server to
4625 * decide whether to return 100, 417 or anything else in return of
4626 * an "Expect: 100-continue" header.
4627 */
4628
4629 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004630 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004631 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004632 * is still null. We must save the body in msg->next because it
4633 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004634 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004635 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004636
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004637 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004638 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004639 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004640 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004641 }
4642
Willy Tarreaud98cf932009-12-27 22:54:55 +01004643 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004644 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004645
Willy Tarreau610ecce2010-01-04 21:15:02 +01004646 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004647 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004648 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004649 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004650 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004651 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004652 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004653 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004654 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004655
Willy Tarreaucaabe412010-01-03 23:08:28 +01004656 if (msg->msg_state == HTTP_MSG_DATA) {
4657 /* must still forward */
4658 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004659 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004660
4661 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004662 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004663 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004664 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004665 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004666 }
4667 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004668 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004669 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004670 * TRAILERS state.
4671 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004672 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004673
Willy Tarreau54d23df2012-10-25 19:04:45 +02004674 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004675 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004676 else if (ret < 0) {
4677 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004678 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004679 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004680 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004681 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004682 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004683 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004684 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004685 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004686 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004687
4688 if (ret == 0)
4689 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004690 else if (ret < 0) {
4691 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004692 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004693 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004694 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004695 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004696 /* we're in MSG_CHUNK_SIZE now */
4697 }
4698 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004699 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004700
4701 if (ret == 0)
4702 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004703 else if (ret < 0) {
4704 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004705 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004706 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004707 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004708 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004709 /* we're in HTTP_MSG_DONE now */
4710 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004711 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004712 int old_state = msg->msg_state;
4713
Willy Tarreau610ecce2010-01-04 21:15:02 +01004714 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004715 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004716 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4717 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004718 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004719 if (http_resync_states(s)) {
4720 /* some state changes occurred, maybe the analyser
4721 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004722 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004723 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004724 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004725 /* request errors are most likely due to
4726 * the server aborting the transfer.
4727 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004728 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004729 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004730 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004731 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004732 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004733 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004734 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004735 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004736
4737 /* If "option abortonclose" is set on the backend, we
4738 * want to monitor the client's connection and forward
4739 * any shutdown notification to the server, which will
4740 * decide whether to close or to go on processing the
4741 * request.
4742 */
4743 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004744 channel_auto_read(req);
4745 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004746 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004747 else if (s->txn.meth == HTTP_METH_POST) {
4748 /* POST requests may require to read extra CRLF
4749 * sent by broken browsers and which could cause
4750 * an RST to be sent upon close on some systems
4751 * (eg: Linux).
4752 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004753 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004754 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004755
Willy Tarreau610ecce2010-01-04 21:15:02 +01004756 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004757 }
4758 }
4759
Willy Tarreaud98cf932009-12-27 22:54:55 +01004760 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004761 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004762 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004763 if (!(s->flags & SN_ERR_MASK))
4764 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004765 if (!(s->flags & SN_FINST_MASK)) {
4766 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4767 s->flags |= SN_FINST_H;
4768 else
4769 s->flags |= SN_FINST_D;
4770 }
4771
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004772 s->fe->fe_counters.cli_aborts++;
4773 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004774 if (objt_server(s->target))
4775 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004776
4777 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004778 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004779
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004780 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004781 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004782 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004783
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004784 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004785 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004786 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004787 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004788 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004789
Willy Tarreau5c620922011-05-11 19:56:11 +02004790 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004791 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004792 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004793 * modes are already handled by the stream sock layer. We must not do
4794 * this in content-length mode because it could present the MSG_MORE
4795 * flag with the last block of forwarded data, which would cause an
4796 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004797 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004798 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004799 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004800
Willy Tarreau610ecce2010-01-04 21:15:02 +01004801 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004802 return 0;
4803
Willy Tarreaud98cf932009-12-27 22:54:55 +01004804 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004805 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004806 if (s->listener->counters)
4807 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004808 return_bad_req_stats_ok:
4809 txn->req.msg_state = HTTP_MSG_ERROR;
4810 if (txn->status) {
4811 /* Note: we don't send any error if some data were already sent */
4812 stream_int_retnclose(req->prod, NULL);
4813 } else {
4814 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004815 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004816 }
4817 req->analysers = 0;
4818 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004819
4820 if (!(s->flags & SN_ERR_MASK))
4821 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004822 if (!(s->flags & SN_FINST_MASK)) {
4823 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4824 s->flags |= SN_FINST_H;
4825 else
4826 s->flags |= SN_FINST_D;
4827 }
4828 return 0;
4829
4830 aborted_xfer:
4831 txn->req.msg_state = HTTP_MSG_ERROR;
4832 if (txn->status) {
4833 /* Note: we don't send any error if some data were already sent */
4834 stream_int_retnclose(req->prod, NULL);
4835 } else {
4836 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004837 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004838 }
4839 req->analysers = 0;
4840 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4841
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004842 s->fe->fe_counters.srv_aborts++;
4843 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004844 if (objt_server(s->target))
4845 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004846
4847 if (!(s->flags & SN_ERR_MASK))
4848 s->flags |= SN_ERR_SRVCL;
4849 if (!(s->flags & SN_FINST_MASK)) {
4850 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4851 s->flags |= SN_FINST_H;
4852 else
4853 s->flags |= SN_FINST_D;
4854 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004855 return 0;
4856}
4857
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004858/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4859 * processing can continue on next analysers, or zero if it either needs more
4860 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4861 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4862 * when it has nothing left to do, and may remove any analyser when it wants to
4863 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004864 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004865int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004866{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004867 struct http_txn *txn = &s->txn;
4868 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004869 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004870 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004871 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004872 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004873
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004874 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 +02004875 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004876 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004877 rep,
4878 rep->rex, rep->wex,
4879 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004880 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004881 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004882
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004883 /*
4884 * Now parse the partial (or complete) lines.
4885 * We will check the response syntax, and also join multi-line
4886 * headers. An index of all the lines will be elaborated while
4887 * parsing.
4888 *
4889 * For the parsing, we use a 28 states FSM.
4890 *
4891 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004892 * rep->buf->p = beginning of response
4893 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4894 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004895 * msg->eol = end of current header or line (LF or CRLF)
4896 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004897 */
4898
Willy Tarreau83e3af02009-12-28 17:39:57 +01004899 /* There's a protected area at the end of the buffer for rewriting
4900 * purposes. We don't want to start to parse the request if the
4901 * protected area is affected, because we may have to move processed
4902 * data later, which is much more complicated.
4903 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004904 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02004905 if (unlikely(channel_full(rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004906 bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
4907 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) {
4908 if (rep->buf->o) {
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004909 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004910 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01004911 goto abort_response;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004912 channel_dont_close(rep);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004913 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01004914 return 0;
4915 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02004916 if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite)
4917 buffer_slow_realign(msg->chn->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004918 }
4919
Willy Tarreau9b28e032012-10-12 23:49:43 +02004920 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01004921 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01004922 }
4923
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004924 /* 1: we might have to print this header in debug mode */
4925 if (unlikely((global.mode & MODE_DEBUG) &&
4926 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01004927 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004928 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004929
Willy Tarreau9b28e032012-10-12 23:49:43 +02004930 sol = rep->buf->p;
4931 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004932 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004933
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004934 sol += hdr_idx_first_pos(&txn->hdr_idx);
4935 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004936
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004937 while (cur_idx) {
4938 eol = sol + txn->hdr_idx.v[cur_idx].len;
4939 debug_hdr("srvhdr", s, sol, eol);
4940 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
4941 cur_idx = txn->hdr_idx.v[cur_idx].next;
4942 }
4943 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004944
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004945 /*
4946 * Now we quickly check if we have found a full valid response.
4947 * If not so, we check the FD and buffer states before leaving.
4948 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01004949 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004950 * responses are checked first.
4951 *
4952 * Depending on whether the client is still there or not, we
4953 * may send an error response back or not. Note that normally
4954 * we should only check for HTTP status there, and check I/O
4955 * errors somewhere else.
4956 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004957
Willy Tarreau655dce92009-11-08 13:10:58 +01004958 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004959 /* Invalid response */
4960 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
4961 /* we detected a parsing error. We want to archive this response
4962 * in the dedicated proxy area for later troubleshooting.
4963 */
4964 hdr_response_bad:
4965 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004966 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004967
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004968 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004969 if (objt_server(s->target)) {
4970 objt_server(s->target)->counters.failed_resp++;
4971 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01004972 }
Willy Tarreau64648412010-03-05 10:41:54 +01004973 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004974 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004975 rep->analysers = 0;
4976 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01004977 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004978 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02004979 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004980
4981 if (!(s->flags & SN_ERR_MASK))
4982 s->flags |= SN_ERR_PRXCOND;
4983 if (!(s->flags & SN_FINST_MASK))
4984 s->flags |= SN_FINST_H;
4985
4986 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02004987 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02004988
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004989 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004990 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02004991 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02004992 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004993 goto hdr_response_bad;
4994 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02004995
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004996 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004997 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004998 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004999 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005000
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005001 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005002 if (objt_server(s->target)) {
5003 objt_server(s->target)->counters.failed_resp++;
5004 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005005 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005006
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005007 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005008 rep->analysers = 0;
5009 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005010 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005011 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005012 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005013
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005014 if (!(s->flags & SN_ERR_MASK))
5015 s->flags |= SN_ERR_SRVCL;
5016 if (!(s->flags & SN_FINST_MASK))
5017 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005018 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005019 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005021 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005022 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005023 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005024 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005025
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005026 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005027 if (objt_server(s->target)) {
5028 objt_server(s->target)->counters.failed_resp++;
5029 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005030 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005031
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005032 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005033 rep->analysers = 0;
5034 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005035 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005036 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005037 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005038
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005039 if (!(s->flags & SN_ERR_MASK))
5040 s->flags |= SN_ERR_SRVTO;
5041 if (!(s->flags & SN_FINST_MASK))
5042 s->flags |= SN_FINST_H;
5043 return 0;
5044 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005045
Willy Tarreauf003d372012-11-26 13:35:37 +01005046 /* client abort with an abortonclose */
5047 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5048 s->fe->fe_counters.cli_aborts++;
5049 s->be->be_counters.cli_aborts++;
5050 if (objt_server(s->target))
5051 objt_server(s->target)->counters.cli_aborts++;
5052
5053 rep->analysers = 0;
5054 channel_auto_close(rep);
5055
5056 txn->status = 400;
5057 bi_erase(rep);
5058 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5059
5060 if (!(s->flags & SN_ERR_MASK))
5061 s->flags |= SN_ERR_CLICL;
5062 if (!(s->flags & SN_FINST_MASK))
5063 s->flags |= SN_FINST_H;
5064
5065 /* process_session() will take care of the error */
5066 return 0;
5067 }
5068
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005069 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005070 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005071 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005072 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005073
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005074 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005075 if (objt_server(s->target)) {
5076 objt_server(s->target)->counters.failed_resp++;
5077 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005078 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005079
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005080 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005081 rep->analysers = 0;
5082 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005083 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005084 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005085 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005086
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005087 if (!(s->flags & SN_ERR_MASK))
5088 s->flags |= SN_ERR_SRVCL;
5089 if (!(s->flags & SN_FINST_MASK))
5090 s->flags |= SN_FINST_H;
5091 return 0;
5092 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005093
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005094 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005095 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005096 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005097 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005098
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005099 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005100 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005101 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005102
5103 if (!(s->flags & SN_ERR_MASK))
5104 s->flags |= SN_ERR_CLICL;
5105 if (!(s->flags & SN_FINST_MASK))
5106 s->flags |= SN_FINST_H;
5107
5108 /* process_session() will take care of the error */
5109 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005110 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005111
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005112 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005113 return 0;
5114 }
5115
5116 /* More interesting part now : we know that we have a complete
5117 * response which at least looks like HTTP. We have an indicator
5118 * of each header's length, so we can parse them quickly.
5119 */
5120
5121 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005122 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005123
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005124 /*
5125 * 1: get the status code
5126 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005127 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005128 if (n < 1 || n > 5)
5129 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005130 /* when the client triggers a 4xx from the server, it's most often due
5131 * to a missing object or permission. These events should be tracked
5132 * because if they happen often, it may indicate a brute force or a
5133 * vulnerability scan.
5134 */
5135 if (n == 4)
5136 session_inc_http_err_ctr(s);
5137
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005138 if (objt_server(s->target))
5139 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005140
Willy Tarreau5b154472009-12-21 20:11:07 +01005141 /* check if the response is HTTP/1.1 or above */
5142 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005143 ((rep->buf->p[5] > '1') ||
5144 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005145 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005146
5147 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005148 txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_HDR_CONN_UPG|TX_CON_CLO_SET|TX_CON_KAL_SET);
Willy Tarreau5b154472009-12-21 20:11:07 +01005149
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005150 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005151 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005152
Willy Tarreau9b28e032012-10-12 23:49:43 +02005153 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005154
Willy Tarreau39650402010-03-15 19:44:39 +01005155 /* Adjust server's health based on status code. Note: status codes 501
5156 * and 505 are triggered on demand by client request, so we must not
5157 * count them as server failures.
5158 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005159 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005160 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005161 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005162 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005163 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005164 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005165
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005166 /*
5167 * 2: check for cacheability.
5168 */
5169
5170 switch (txn->status) {
5171 case 200:
5172 case 203:
5173 case 206:
5174 case 300:
5175 case 301:
5176 case 410:
5177 /* RFC2616 @13.4:
5178 * "A response received with a status code of
5179 * 200, 203, 206, 300, 301 or 410 MAY be stored
5180 * by a cache (...) unless a cache-control
5181 * directive prohibits caching."
5182 *
5183 * RFC2616 @9.5: POST method :
5184 * "Responses to this method are not cacheable,
5185 * unless the response includes appropriate
5186 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005187 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005188 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005189 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005190 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5191 break;
5192 default:
5193 break;
5194 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005195
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005196 /*
5197 * 3: we may need to capture headers
5198 */
5199 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005200 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005201 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005202 txn->rsp.cap, s->fe->rsp_cap);
5203
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005204 /* 4: determine the transfer-length.
5205 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5206 * the presence of a message-body in a RESPONSE and its transfer length
5207 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005208 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005209 * All responses to the HEAD request method MUST NOT include a
5210 * message-body, even though the presence of entity-header fields
5211 * might lead one to believe they do. All 1xx (informational), 204
5212 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5213 * message-body. All other responses do include a message-body,
5214 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005215 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005216 * 1. Any response which "MUST NOT" include a message-body (such as the
5217 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5218 * always terminated by the first empty line after the header fields,
5219 * regardless of the entity-header fields present in the message.
5220 *
5221 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5222 * the "chunked" transfer-coding (Section 6.2) is used, the
5223 * transfer-length is defined by the use of this transfer-coding.
5224 * If a Transfer-Encoding header field is present and the "chunked"
5225 * transfer-coding is not present, the transfer-length is defined by
5226 * the sender closing the connection.
5227 *
5228 * 3. If a Content-Length header field is present, its decimal value in
5229 * OCTETs represents both the entity-length and the transfer-length.
5230 * If a message is received with both a Transfer-Encoding header
5231 * field and a Content-Length header field, the latter MUST be ignored.
5232 *
5233 * 4. If the message uses the media type "multipart/byteranges", and
5234 * the transfer-length is not otherwise specified, then this self-
5235 * delimiting media type defines the transfer-length. This media
5236 * type MUST NOT be used unless the sender knows that the recipient
5237 * can parse it; the presence in a request of a Range header with
5238 * multiple byte-range specifiers from a 1.1 client implies that the
5239 * client can parse multipart/byteranges responses.
5240 *
5241 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005242 */
5243
5244 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005245 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005246 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005247 * FIXME: should we parse anyway and return an error on chunked encoding ?
5248 */
5249 if (txn->meth == HTTP_METH_HEAD ||
5250 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005251 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005252 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005253 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005254 goto skip_content_length;
5255 }
5256
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005257 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005258 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005259 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005260 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005261 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005262 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5263 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005264 /* bad transfer-encoding (chunked followed by something else) */
5265 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005266 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005267 break;
5268 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005269 }
5270
5271 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5272 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005273 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005274 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005275 signed long long cl;
5276
Willy Tarreauad14f752011-09-02 20:33:27 +02005277 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005278 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005279 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005280 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005281
Willy Tarreauad14f752011-09-02 20:33:27 +02005282 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005283 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005284 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005285 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005286
Willy Tarreauad14f752011-09-02 20:33:27 +02005287 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005288 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005289 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005290 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005291
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005292 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005293 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005294 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005295 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005296
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005297 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005298 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005299 }
5300
William Lallemand82fe75c2012-10-23 10:25:10 +02005301 if (s->fe->comp || s->be->comp)
5302 select_compression_response_header(s, rep->buf);
5303
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005304 /* FIXME: we should also implement the multipart/byterange method.
5305 * For now on, we resort to close mode in this case (unknown length).
5306 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005307skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005308
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005309 /* end of job, return OK */
5310 rep->analysers &= ~an_bit;
5311 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005312 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005313 return 1;
5314}
5315
5316/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005317 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5318 * and updates t->rep->analysers. It might make sense to explode it into several
5319 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005320 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005321int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005322{
5323 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005324 struct http_msg *msg = &txn->rsp;
5325 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005326 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005327
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005328 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 +02005329 now_ms, __FUNCTION__,
5330 t,
5331 rep,
5332 rep->rex, rep->wex,
5333 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005334 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005335 rep->analysers);
5336
Willy Tarreau655dce92009-11-08 13:10:58 +01005337 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005338 return 0;
5339
5340 rep->analysers &= ~an_bit;
5341 rep->analyse_exp = TICK_ETERNITY;
5342
Willy Tarreau5b154472009-12-21 20:11:07 +01005343 /* Now we have to check if we need to modify the Connection header.
5344 * This is more difficult on the response than it is on the request,
5345 * because we can have two different HTTP versions and we don't know
5346 * how the client will interprete a response. For instance, let's say
5347 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5348 * HTTP/1.1 response without any header. Maybe it will bound itself to
5349 * HTTP/1.0 because it only knows about it, and will consider the lack
5350 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5351 * the lack of header as a keep-alive. Thus we will use two flags
5352 * indicating how a request MAY be understood by the client. In case
5353 * of multiple possibilities, we'll fix the header to be explicit. If
5354 * ambiguous cases such as both close and keepalive are seen, then we
5355 * will fall back to explicit close. Note that we won't take risks with
5356 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005357 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005358 */
5359
Willy Tarreaudc008c52010-02-01 16:20:08 +01005360 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5361 txn->status == 101)) {
5362 /* Either we've established an explicit tunnel, or we're
5363 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005364 * to understand the next protocols. We have to switch to tunnel
5365 * mode, so that we transfer the request and responses then let
5366 * this protocol pass unmodified. When we later implement specific
5367 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005368 * header which contains information about that protocol for
5369 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005370 */
5371 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5372 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005373 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5374 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5375 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005376 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005377
Willy Tarreau60466522010-01-18 19:08:45 +01005378 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005379 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005380 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5381 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005382
Willy Tarreau60466522010-01-18 19:08:45 +01005383 /* now adjust header transformations depending on current state */
5384 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5385 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5386 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005387 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005388 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005389 }
Willy Tarreau60466522010-01-18 19:08:45 +01005390 else { /* SCL / KAL */
5391 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005392 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005393 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005394 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005395
Willy Tarreau60466522010-01-18 19:08:45 +01005396 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005397 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005398
Willy Tarreau60466522010-01-18 19:08:45 +01005399 /* Some keep-alive responses are converted to Server-close if
5400 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005401 */
Willy Tarreau60466522010-01-18 19:08:45 +01005402 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5403 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005404 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005405 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005406 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005407 }
5408
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005409 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005410 /*
5411 * 3: we will have to evaluate the filters.
5412 * As opposed to version 1.2, now they will be evaluated in the
5413 * filters order and not in the header order. This means that
5414 * each filter has to be validated among all headers.
5415 *
5416 * Filters are tried with ->be first, then with ->fe if it is
5417 * different from ->be.
5418 */
5419
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005420 cur_proxy = t->be;
5421 while (1) {
5422 struct proxy *rule_set = cur_proxy;
5423
5424 /* try headers filters */
5425 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005426 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005427 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005428 if (objt_server(t->target)) {
5429 objt_server(t->target)->counters.failed_resp++;
5430 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005431 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005432 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005433 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005434 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005435 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005436 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005437 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005438 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005439 if (!(t->flags & SN_ERR_MASK))
5440 t->flags |= SN_ERR_PRXCOND;
5441 if (!(t->flags & SN_FINST_MASK))
5442 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005443 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005444 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005445 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005446
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005447 /* has the response been denied ? */
5448 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005449 if (objt_server(t->target))
5450 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005451
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005452 t->be->be_counters.denied_resp++;
5453 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005454 if (t->listener->counters)
5455 t->listener->counters->denied_resp++;
5456
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005457 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005458 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005459
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005460 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005461 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005462 if (txn->status < 200)
5463 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005464 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005465 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005466 ret = acl_pass(ret);
5467 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5468 ret = !ret;
5469 if (!ret)
5470 continue;
5471 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005472 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005473 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005474 }
5475
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005476 /* check whether we're already working on the frontend */
5477 if (cur_proxy == t->fe)
5478 break;
5479 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005480 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005481
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005482 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005483 * We may be facing a 100-continue response, in which case this
5484 * is not the right response, and we're waiting for the next one.
5485 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005486 * next one.
5487 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005488 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005489 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005490 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005491 msg->msg_state = HTTP_MSG_RPBEFORE;
5492 txn->status = 0;
5493 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5494 return 1;
5495 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005496 else if (unlikely(txn->status < 200))
5497 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005498
5499 /* we don't have any 1xx status code now */
5500
5501 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005502 * 4: check for server cookie.
5503 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005504 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5505 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005506 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005507
Willy Tarreaubaaee002006-06-26 02:48:02 +02005508
Willy Tarreaua15645d2007-03-18 16:22:39 +01005509 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005510 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005511 */
Willy Tarreau67402132012-05-31 20:40:20 +02005512 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005513 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005514
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005515 /*
5516 * 6: add server cookie in the response if needed
5517 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005518 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005519 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005520 (!(t->flags & SN_DIRECT) ||
5521 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5522 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5523 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5524 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005525 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005526 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005527 /* the server is known, it's not the one the client requested, or the
5528 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005529 * insert a set-cookie here, except if we want to insert only on POST
5530 * requests and this one isn't. Note that servers which don't have cookies
5531 * (eg: some backup servers) will return a full cookie removal request.
5532 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005533 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005534 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005535 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5536 t->be->cookie_name);
5537 }
5538 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005539 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005540
5541 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5542 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005543 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5544 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5545 trash.len += 5;
5546
Willy Tarreauef4f3912010-10-07 21:00:29 +02005547 if (t->be->cookie_maxlife) {
5548 /* emit first_date, which is either the original one or
5549 * the current date.
5550 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005551 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005552 s30tob64(txn->cookie_first_date ?
5553 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005554 (date.tv_sec+3) >> 2, trash.str + trash.len);
5555 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005556 }
5557 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005558 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005559 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005560
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005561 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005562 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005563
Willy Tarreau4992dd22012-05-31 21:02:17 +02005564 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005565 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005566
5567 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005568 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005569
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005570 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005571 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005572
Willy Tarreauf1348312010-10-07 15:54:11 +02005573 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005574 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005575 /* the server did not change, only the date was updated */
5576 txn->flags |= TX_SCK_UPDATED;
5577 else
5578 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005579
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005580 /* Here, we will tell an eventual cache on the client side that we don't
5581 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5582 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5583 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5584 */
Willy Tarreau67402132012-05-31 20:40:20 +02005585 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005586
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005587 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5588
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005589 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005590 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005591 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005592 }
5593 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005594
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005595 /*
5596 * 7: check if result will be cacheable with a cookie.
5597 * We'll block the response if security checks have caught
5598 * nasty things such as a cacheable cookie.
5599 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005600 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5601 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005602 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005603
5604 /* we're in presence of a cacheable response containing
5605 * a set-cookie header. We'll block it as requested by
5606 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005607 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005608 if (objt_server(t->target))
5609 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005610
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005611 t->be->be_counters.denied_resp++;
5612 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005613 if (t->listener->counters)
5614 t->listener->counters->denied_resp++;
5615
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005616 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005617 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005618 send_log(t->be, LOG_ALERT,
5619 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005620 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005621 goto return_srv_prx_502;
5622 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005623
5624 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005625 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005626 * If an "Upgrade" token is found, the header is left untouched in order
5627 * not to have to deal with some client bugs : some of them fail an upgrade
5628 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005629 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005630 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5631 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5632 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005633 unsigned int want_flags = 0;
5634
5635 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5636 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5637 /* we want a keep-alive response here. Keep-alive header
5638 * required if either side is not 1.1.
5639 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005640 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005641 want_flags |= TX_CON_KAL_SET;
5642 }
5643 else {
5644 /* we want a close response here. Close header required if
5645 * the server is 1.1, regardless of the client.
5646 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005647 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005648 want_flags |= TX_CON_CLO_SET;
5649 }
5650
5651 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005652 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005653 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005654
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005655 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005656 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005657 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005658 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005659
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005660 /*************************************************************
5661 * OK, that's finished for the headers. We have done what we *
5662 * could. Let's switch to the DATA state. *
5663 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005664
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005665 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005666
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005667 /* if the user wants to log as soon as possible, without counting
5668 * bytes from the server, then this is the right moment. We have
5669 * to temporarily assign bytes_out to log what we currently have.
5670 */
5671 if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) {
5672 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5673 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005674 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005675 t->logs.bytes_out = 0;
5676 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005677
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005678 /* Note: we must not try to cheat by jumping directly to DATA,
5679 * otherwise we would not let the client side wake up.
5680 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005681
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005682 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005683 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005684 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005685}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005686
Willy Tarreaud98cf932009-12-27 22:54:55 +01005687/* This function is an analyser which forwards response body (including chunk
5688 * sizes if any). It is called as soon as we must forward, even if we forward
5689 * zero byte. The only situation where it must not be called is when we're in
5690 * tunnel mode and we want to forward till the close. It's used both to forward
5691 * remaining data and to resync after end of body. It expects the msg_state to
5692 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5693 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005694 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005695 * bytes of pending data + the headers if not already done (between sol and sov).
5696 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005697 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005698int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005699{
5700 struct http_txn *txn = &s->txn;
5701 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005702 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005703 static struct buffer *tmpbuf = NULL;
5704 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005705 int consumed_data = 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005706
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005707 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5708 return 0;
5709
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005710 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005711 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005712 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005713 /* Output closed while we were sending data. We must abort and
5714 * wake the other side up.
5715 */
5716 msg->msg_state = HTTP_MSG_ERROR;
5717 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005718 return 1;
5719 }
5720
Willy Tarreau4fe41902010-06-07 22:27:41 +02005721 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005722 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005723
William Lallemand82fe75c2012-10-23 10:25:10 +02005724 /* this is the first time we need the compression buffer */
5725 if (s->comp_algo != NULL && tmpbuf == NULL) {
5726 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5727 goto aborted_xfer; /* no memory */
5728 }
5729
Willy Tarreaud98cf932009-12-27 22:54:55 +01005730 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005731 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005732 * rep->buf.p still points to the beginning of the message and msg->sol
5733 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005734 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005735 channel_forward(res, msg->sov);
5736 msg->next = 0;
5737 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005738
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005739 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005740 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005741 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005742 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005743 }
5744
William Lallemand82fe75c2012-10-23 10:25:10 +02005745 if (s->comp_algo != NULL) {
5746 int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
5747 if (ret < 0)
5748 goto missing_data; /* not enough spaces in buffers */
5749 compressing = 1;
5750 }
5751
Willy Tarreaud98cf932009-12-27 22:54:55 +01005752 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005753 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005754 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005755 if (s->comp_algo == NULL) {
5756 bytes = msg->sov - msg->sol;
5757 if (msg->chunk_len || bytes) {
5758 msg->sol = msg->sov;
5759 msg->next -= bytes; /* will be forwarded */
5760 msg->chunk_len += bytes;
5761 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5762 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005763 }
5764
Willy Tarreaucaabe412010-01-03 23:08:28 +01005765 if (msg->msg_state == HTTP_MSG_DATA) {
William Lallemandbf3ae612012-11-19 12:35:37 +01005766 int ret;
5767
Willy Tarreaucaabe412010-01-03 23:08:28 +01005768 /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005769 if (compressing) {
5770 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5771 if (ret < 0)
5772 goto aborted_xfer;
5773 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005774
5775 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005776 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005777
5778 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005779 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005780 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005781 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005782 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005783 if (compressing && consumed_data) {
5784 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5785 compressing = 0;
5786 }
5787 }
Willy Tarreaucaabe412010-01-03 23:08:28 +01005788 }
5789 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01005790 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005791 * set ->sov and ->next to point to the body and switch to DATA or
5792 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005793 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005794 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005795
Willy Tarreau54d23df2012-10-25 19:04:45 +02005796 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005797 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005798 else if (ret < 0) {
5799 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005800 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005801 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005802 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005803 if (compressing) {
5804 if (likely(msg->chunk_len > 0)) {
5805 /* skipping data if we are in compression mode */
5806 b_adv(res->buf, msg->next);
5807 msg->next = 0;
5808 msg->sov = 0;
5809 msg->sol = 0;
5810 } else {
5811 if (consumed_data) {
5812 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5813 compressing = 0;
5814 }
5815 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005816 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005817 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005818 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02005819 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01005820 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02005821 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005822
Willy Tarreau54d23df2012-10-25 19:04:45 +02005823 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005824 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005825 else if (ret < 0) {
5826 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02005827 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005828 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005829 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005830 /* skipping data in buffer for compression */
5831 if (compressing) {
5832 b_adv(res->buf, msg->next);
5833 msg->next = 0;
5834 msg->sov = 0;
5835 msg->sol = 0;
5836 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005837 /* we're in MSG_CHUNK_SIZE now */
5838 }
5839 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01005840 int ret = http_forward_trailers(msg);
Willy Tarreau5523b322009-12-29 12:05:52 +01005841
Willy Tarreaud98cf932009-12-27 22:54:55 +01005842 if (ret == 0)
5843 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005844 else if (ret < 0) {
5845 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005846 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005847 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005848 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005849 if (s->comp_algo != NULL) {
5850 /* forwarding trailers */
5851 channel_forward(res, msg->next);
5852 msg->next = 0;
5853 }
5854 /* we're in HTTP_MSG_DONE now */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005855 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005856 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005857 int old_state = msg->msg_state;
Willy Tarreau610ecce2010-01-04 21:15:02 +01005858 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02005859 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005860 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5861 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005862 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005863 if (http_resync_states(s)) {
5864 http_silent_debug(__LINE__, s);
5865 /* some state changes occurred, maybe the analyser
5866 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005867 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005868 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005869 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005870 /* response errors are most likely due to
5871 * the client aborting the transfer.
5872 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005873 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005874 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005875 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005876 http_capture_bad_message(&s->be->invalid_rep, s, msg, old_state, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005877 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005878 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005879 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005880 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005881 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005882 }
5883 }
5884
Willy Tarreaud98cf932009-12-27 22:54:55 +01005885 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005886 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005887 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5888 compressing = 0;
5889 }
Willy Tarreauf003d372012-11-26 13:35:37 +01005890
5891 if (res->flags & CF_SHUTW)
5892 goto aborted_xfer;
5893
5894 /* stop waiting for data if the input is closed before the end. If the
5895 * client side was already closed, it means that the client has aborted,
5896 * so we don't want to count this as a server abort. Otherwise it's a
5897 * server abort.
5898 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005899 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01005900 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
5901 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01005902 if (!(s->flags & SN_ERR_MASK))
5903 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005904 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005905 if (objt_server(s->target))
5906 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005907 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01005908 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005909
Willy Tarreau40dba092010-03-04 18:14:51 +01005910 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005911 if (!s->req->analysers)
5912 goto return_bad_res;
5913
Willy Tarreauea953162012-05-18 23:41:28 +02005914 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005915 if (s->comp_algo == NULL) {
5916 bytes = msg->sov - msg->sol;
5917 if (msg->chunk_len || bytes) {
5918 msg->sol = msg->sov;
5919 msg->next -= bytes; /* will be forwarded */
5920 msg->chunk_len += bytes;
5921 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5922 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01005923 }
5924
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005925 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005926 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005927 * Similarly, with keep-alive on the client side, we don't want to forward a
5928 * close.
5929 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005930 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005931 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5932 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005933 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005934
Willy Tarreau5c620922011-05-11 19:56:11 +02005935 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005936 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02005937 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01005938 * modes are already handled by the stream sock layer. We must not do
5939 * this in content-length mode because it could present the MSG_MORE
5940 * flag with the last block of forwarded data, which would cause an
5941 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02005942 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02005943 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005944 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02005945
Willy Tarreaud98cf932009-12-27 22:54:55 +01005946 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01005947 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005948 return 0;
5949
Willy Tarreau40dba092010-03-04 18:14:51 +01005950 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005951 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005952 if (objt_server(s->target))
5953 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005954
5955 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01005956 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01005957 /* don't send any error message as we're in the body */
5958 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005959 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005960 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005961 if (objt_server(s->target))
5962 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005963
5964 if (!(s->flags & SN_ERR_MASK))
5965 s->flags |= SN_ERR_PRXCOND;
5966 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01005967 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005968 return 0;
5969
5970 aborted_xfer:
5971 txn->rsp.msg_state = HTTP_MSG_ERROR;
5972 /* don't send any error message as we're in the body */
5973 stream_int_retnclose(res->cons, NULL);
5974 res->analysers = 0;
5975 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
5976
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005977 s->fe->fe_counters.cli_aborts++;
5978 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005979 if (objt_server(s->target))
5980 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005981
5982 if (!(s->flags & SN_ERR_MASK))
5983 s->flags |= SN_ERR_CLICL;
5984 if (!(s->flags & SN_FINST_MASK))
5985 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005986 return 0;
5987}
5988
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005989/* Iterate the same filter through all request headers.
5990 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005991 * Since it can manage the switch to another backend, it updates the per-proxy
5992 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01005993 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005994int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01005995{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01005996 char term;
5997 char *cur_ptr, *cur_end, *cur_next;
5998 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01005999 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006000 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006001 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006002
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006003 last_hdr = 0;
6004
Willy Tarreau9b28e032012-10-12 23:49:43 +02006005 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006006 old_idx = 0;
6007
6008 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006009 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006010 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006011 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006012 (exp->action == ACT_ALLOW ||
6013 exp->action == ACT_DENY ||
6014 exp->action == ACT_TARPIT))
6015 return 0;
6016
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006017 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006018 if (!cur_idx)
6019 break;
6020
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006021 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006022 cur_ptr = cur_next;
6023 cur_end = cur_ptr + cur_hdr->len;
6024 cur_next = cur_end + cur_hdr->cr + 1;
6025
6026 /* Now we have one header between cur_ptr and cur_end,
6027 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006028 */
6029
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006030 /* The annoying part is that pattern matching needs
6031 * that we modify the contents to null-terminate all
6032 * strings before testing them.
6033 */
6034
6035 term = *cur_end;
6036 *cur_end = '\0';
6037
6038 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6039 switch (exp->action) {
6040 case ACT_SETBE:
6041 /* It is not possible to jump a second time.
6042 * FIXME: should we return an HTTP/500 here so that
6043 * the admin knows there's a problem ?
6044 */
6045 if (t->be != t->fe)
6046 break;
6047
6048 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006049 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006050 last_hdr = 1;
6051 break;
6052
6053 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006054 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006055 last_hdr = 1;
6056 break;
6057
6058 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006059 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006060 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006061
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006062 t->fe->fe_counters.denied_req++;
6063 if (t->fe != t->be)
6064 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006065 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006066 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006067
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006068 break;
6069
6070 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006071 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006072 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006073
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006074 t->fe->fe_counters.denied_req++;
6075 if (t->fe != t->be)
6076 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006077 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006078 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006079
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006080 break;
6081
6082 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006083 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6084 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006085 /* FIXME: if the user adds a newline in the replacement, the
6086 * index will not be recalculated for now, and the new line
6087 * will not be counted as a new header.
6088 */
6089
6090 cur_end += delta;
6091 cur_next += delta;
6092 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006093 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006094 break;
6095
6096 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006097 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006098 cur_next += delta;
6099
Willy Tarreaufa355d42009-11-29 18:12:29 +01006100 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006101 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6102 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006103 cur_hdr->len = 0;
6104 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006105 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006106 break;
6107
6108 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006109 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006110 if (cur_end)
6111 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006113 /* keep the link from this header to next one in case of later
6114 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006115 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006116 old_idx = cur_idx;
6117 }
6118 return 0;
6119}
6120
6121
6122/* Apply the filter to the request line.
6123 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6124 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006125 * Since it can manage the switch to another backend, it updates the per-proxy
6126 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006127 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006128int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006129{
6130 char term;
6131 char *cur_ptr, *cur_end;
6132 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006133 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006134 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006135
Willy Tarreau3d300592007-03-18 18:34:41 +01006136 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006137 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006138 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006139 (exp->action == ACT_ALLOW ||
6140 exp->action == ACT_DENY ||
6141 exp->action == ACT_TARPIT))
6142 return 0;
6143 else if (exp->action == ACT_REMOVE)
6144 return 0;
6145
6146 done = 0;
6147
Willy Tarreau9b28e032012-10-12 23:49:43 +02006148 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006149 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006150
6151 /* Now we have the request line between cur_ptr and cur_end */
6152
6153 /* The annoying part is that pattern matching needs
6154 * that we modify the contents to null-terminate all
6155 * strings before testing them.
6156 */
6157
6158 term = *cur_end;
6159 *cur_end = '\0';
6160
6161 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6162 switch (exp->action) {
6163 case ACT_SETBE:
6164 /* It is not possible to jump a second time.
6165 * FIXME: should we return an HTTP/500 here so that
6166 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006167 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006168 if (t->be != t->fe)
6169 break;
6170
6171 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006172 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006173 done = 1;
6174 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006175
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006176 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006177 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006178 done = 1;
6179 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006180
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006181 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006182 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006183
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006184 t->fe->fe_counters.denied_req++;
6185 if (t->fe != t->be)
6186 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006187 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006188 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006190 done = 1;
6191 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006192
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006193 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006194 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006195
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006196 t->fe->fe_counters.denied_req++;
6197 if (t->fe != t->be)
6198 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006199 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006200 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006201
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006202 done = 1;
6203 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006204
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006205 case ACT_REPLACE:
6206 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006207 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6208 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006209 /* FIXME: if the user adds a newline in the replacement, the
6210 * index will not be recalculated for now, and the new line
6211 * will not be counted as a new header.
6212 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006213
Willy Tarreaufa355d42009-11-29 18:12:29 +01006214 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006215 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006216 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006217 HTTP_MSG_RQMETH,
6218 cur_ptr, cur_end + 1,
6219 NULL, NULL);
6220 if (unlikely(!cur_end))
6221 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006222
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006223 /* we have a full request and we know that we have either a CR
6224 * or an LF at <ptr>.
6225 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006226 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6227 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006228 /* there is no point trying this regex on headers */
6229 return 1;
6230 }
6231 }
6232 *cur_end = term; /* restore the string terminator */
6233 return done;
6234}
Willy Tarreau97de6242006-12-27 17:18:38 +01006235
Willy Tarreau58f10d72006-12-04 02:26:12 +01006236
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006238/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006239 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006240 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006241 * unparsable request. Since it can manage the switch to another backend, it
6242 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006243 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006244int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006245{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006246 struct http_txn *txn = &s->txn;
6247 struct hdr_exp *exp;
6248
6249 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006250 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006251
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006252 /*
6253 * The interleaving of transformations and verdicts
6254 * makes it difficult to decide to continue or stop
6255 * the evaluation.
6256 */
6257
Willy Tarreau6c123b12010-01-28 20:22:06 +01006258 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6259 break;
6260
Willy Tarreau3d300592007-03-18 18:34:41 +01006261 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006262 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006263 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006264 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006265
6266 /* if this filter had a condition, evaluate it now and skip to
6267 * next filter if the condition does not match.
6268 */
6269 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006270 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006271 ret = acl_pass(ret);
6272 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6273 ret = !ret;
6274
6275 if (!ret)
6276 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006277 }
6278
6279 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006280 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006281 if (unlikely(ret < 0))
6282 return -1;
6283
6284 if (likely(ret == 0)) {
6285 /* The filter did not match the request, it can be
6286 * iterated through all headers.
6287 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006288 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006289 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006290 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006291 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006292}
6293
6294
Willy Tarreaua15645d2007-03-18 16:22:39 +01006295
Willy Tarreau58f10d72006-12-04 02:26:12 +01006296/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006297 * Try to retrieve the server associated to the appsession.
6298 * If the server is found, it's assigned to the session.
6299 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006300void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006301 struct http_txn *txn = &t->txn;
6302 appsess *asession = NULL;
6303 char *sessid_temp = NULL;
6304
Cyril Bontéb21570a2009-11-29 20:04:48 +01006305 if (len > t->be->appsession_len) {
6306 len = t->be->appsession_len;
6307 }
6308
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006309 if (t->be->options2 & PR_O2_AS_REQL) {
6310 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006311 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006312 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006313 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006314 }
6315
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006316 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006317 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6318 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6319 return;
6320 }
6321
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006322 memcpy(txn->sessid, buf, len);
6323 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006324 }
6325
6326 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6327 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6328 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6329 return;
6330 }
6331
Cyril Bontéb21570a2009-11-29 20:04:48 +01006332 memcpy(sessid_temp, buf, len);
6333 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006334
6335 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6336 /* free previously allocated memory */
6337 pool_free2(apools.sessid, sessid_temp);
6338
6339 if (asession != NULL) {
6340 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6341 if (!(t->be->options2 & PR_O2_AS_REQL))
6342 asession->request_count++;
6343
6344 if (asession->serverid != NULL) {
6345 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006346
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006347 while (srv) {
6348 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006349 if ((srv->state & SRV_RUNNING) ||
6350 (t->be->options & PR_O_PERSIST) ||
6351 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006352 /* we found the server and it's usable */
6353 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006354 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006355 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006356 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006357
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006358 break;
6359 } else {
6360 txn->flags &= ~TX_CK_MASK;
6361 txn->flags |= TX_CK_DOWN;
6362 }
6363 }
6364 srv = srv->next;
6365 }
6366 }
6367 }
6368}
6369
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006370/* Find the end of a cookie value contained between <s> and <e>. It works the
6371 * same way as with headers above except that the semi-colon also ends a token.
6372 * See RFC2965 for more information. Note that it requires a valid header to
6373 * return a valid result.
6374 */
6375char *find_cookie_value_end(char *s, const char *e)
6376{
6377 int quoted, qdpair;
6378
6379 quoted = qdpair = 0;
6380 for (; s < e; s++) {
6381 if (qdpair) qdpair = 0;
6382 else if (quoted) {
6383 if (*s == '\\') qdpair = 1;
6384 else if (*s == '"') quoted = 0;
6385 }
6386 else if (*s == '"') quoted = 1;
6387 else if (*s == ',' || *s == ';') return s;
6388 }
6389 return s;
6390}
6391
6392/* Delete a value in a header between delimiters <from> and <next> in buffer
6393 * <buf>. The number of characters displaced is returned, and the pointer to
6394 * the first delimiter is updated if required. The function tries as much as
6395 * possible to respect the following principles :
6396 * - replace <from> delimiter by the <next> one unless <from> points to a
6397 * colon, in which case <next> is simply removed
6398 * - set exactly one space character after the new first delimiter, unless
6399 * there are not enough characters in the block being moved to do so.
6400 * - remove unneeded spaces before the previous delimiter and after the new
6401 * one.
6402 *
6403 * It is the caller's responsibility to ensure that :
6404 * - <from> points to a valid delimiter or the colon ;
6405 * - <next> points to a valid delimiter or the final CR/LF ;
6406 * - there are non-space chars before <from> ;
6407 * - there is a CR/LF at or after <next>.
6408 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006409int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006410{
6411 char *prev = *from;
6412
6413 if (*prev == ':') {
6414 /* We're removing the first value, preserve the colon and add a
6415 * space if possible.
6416 */
6417 if (!http_is_crlf[(unsigned char)*next])
6418 next++;
6419 prev++;
6420 if (prev < next)
6421 *prev++ = ' ';
6422
6423 while (http_is_spht[(unsigned char)*next])
6424 next++;
6425 } else {
6426 /* Remove useless spaces before the old delimiter. */
6427 while (http_is_spht[(unsigned char)*(prev-1)])
6428 prev--;
6429 *from = prev;
6430
6431 /* copy the delimiter and if possible a space if we're
6432 * not at the end of the line.
6433 */
6434 if (!http_is_crlf[(unsigned char)*next]) {
6435 *prev++ = *next++;
6436 if (prev + 1 < next)
6437 *prev++ = ' ';
6438 while (http_is_spht[(unsigned char)*next])
6439 next++;
6440 }
6441 }
6442 return buffer_replace2(buf, prev, next, NULL, 0);
6443}
6444
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006445/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006446 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006447 * desirable to call it only when needed. This code is quite complex because
6448 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6449 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006450 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006451void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006452{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006453 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006454 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006455 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006456 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6457 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006458
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006459 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006460 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006461 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006462
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006463 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006464 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006465 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006466
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006467 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006468 hdr_beg = hdr_next;
6469 hdr_end = hdr_beg + cur_hdr->len;
6470 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006471
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006472 /* We have one full header between hdr_beg and hdr_end, and the
6473 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006474 * "Cookie:" headers.
6475 */
6476
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006477 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006478 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006479 old_idx = cur_idx;
6480 continue;
6481 }
6482
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006483 del_from = NULL; /* nothing to be deleted */
6484 preserve_hdr = 0; /* assume we may kill the whole header */
6485
Willy Tarreau58f10d72006-12-04 02:26:12 +01006486 /* Now look for cookies. Conforming to RFC2109, we have to support
6487 * attributes whose name begin with a '$', and associate them with
6488 * the right cookie, if we want to delete this cookie.
6489 * So there are 3 cases for each cookie read :
6490 * 1) it's a special attribute, beginning with a '$' : ignore it.
6491 * 2) it's a server id cookie that we *MAY* want to delete : save
6492 * some pointers on it (last semi-colon, beginning of cookie...)
6493 * 3) it's an application cookie : we *MAY* have to delete a previous
6494 * "special" cookie.
6495 * At the end of loop, if a "special" cookie remains, we may have to
6496 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006497 * *MUST* delete it.
6498 *
6499 * Note: RFC2965 is unclear about the processing of spaces around
6500 * the equal sign in the ATTR=VALUE form. A careful inspection of
6501 * the RFC explicitly allows spaces before it, and not within the
6502 * tokens (attrs or values). An inspection of RFC2109 allows that
6503 * too but section 10.1.3 lets one think that spaces may be allowed
6504 * after the equal sign too, resulting in some (rare) buggy
6505 * implementations trying to do that. So let's do what servers do.
6506 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6507 * allowed quoted strings in values, with any possible character
6508 * after a backslash, including control chars and delimitors, which
6509 * causes parsing to become ambiguous. Browsers also allow spaces
6510 * within values even without quotes.
6511 *
6512 * We have to keep multiple pointers in order to support cookie
6513 * removal at the beginning, middle or end of header without
6514 * corrupting the header. All of these headers are valid :
6515 *
6516 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6517 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6518 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6519 * | | | | | | | | |
6520 * | | | | | | | | hdr_end <--+
6521 * | | | | | | | +--> next
6522 * | | | | | | +----> val_end
6523 * | | | | | +-----------> val_beg
6524 * | | | | +--------------> equal
6525 * | | | +----------------> att_end
6526 * | | +---------------------> att_beg
6527 * | +--------------------------> prev
6528 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006529 */
6530
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006531 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6532 /* Iterate through all cookies on this line */
6533
6534 /* find att_beg */
6535 att_beg = prev + 1;
6536 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6537 att_beg++;
6538
6539 /* find att_end : this is the first character after the last non
6540 * space before the equal. It may be equal to hdr_end.
6541 */
6542 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006543
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006544 while (equal < hdr_end) {
6545 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006546 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006547 if (http_is_spht[(unsigned char)*equal++])
6548 continue;
6549 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006550 }
6551
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006552 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6553 * is between <att_beg> and <equal>, both may be identical.
6554 */
6555
6556 /* look for end of cookie if there is an equal sign */
6557 if (equal < hdr_end && *equal == '=') {
6558 /* look for the beginning of the value */
6559 val_beg = equal + 1;
6560 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6561 val_beg++;
6562
6563 /* find the end of the value, respecting quotes */
6564 next = find_cookie_value_end(val_beg, hdr_end);
6565
6566 /* make val_end point to the first white space or delimitor after the value */
6567 val_end = next;
6568 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6569 val_end--;
6570 } else {
6571 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006572 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006573
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006574 /* We have nothing to do with attributes beginning with '$'. However,
6575 * they will automatically be removed if a header before them is removed,
6576 * since they're supposed to be linked together.
6577 */
6578 if (*att_beg == '$')
6579 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006580
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006581 /* Ignore cookies with no equal sign */
6582 if (equal == next) {
6583 /* This is not our cookie, so we must preserve it. But if we already
6584 * scheduled another cookie for removal, we cannot remove the
6585 * complete header, but we can remove the previous block itself.
6586 */
6587 preserve_hdr = 1;
6588 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006589 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006590 val_end += delta;
6591 next += delta;
6592 hdr_end += delta;
6593 hdr_next += delta;
6594 cur_hdr->len += delta;
6595 http_msg_move_end(&txn->req, delta);
6596 prev = del_from;
6597 del_from = NULL;
6598 }
6599 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006600 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006601
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006602 /* if there are spaces around the equal sign, we need to
6603 * strip them otherwise we'll get trouble for cookie captures,
6604 * or even for rewrites. Since this happens extremely rarely,
6605 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006606 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006607 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6608 int stripped_before = 0;
6609 int stripped_after = 0;
6610
6611 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006612 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006613 equal += stripped_before;
6614 val_beg += stripped_before;
6615 }
6616
6617 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006618 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006619 val_beg += stripped_after;
6620 stripped_before += stripped_after;
6621 }
6622
6623 val_end += stripped_before;
6624 next += stripped_before;
6625 hdr_end += stripped_before;
6626 hdr_next += stripped_before;
6627 cur_hdr->len += stripped_before;
6628 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006629 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006630 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006631
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006632 /* First, let's see if we want to capture this cookie. We check
6633 * that we don't already have a client side cookie, because we
6634 * can only capture one. Also as an optimisation, we ignore
6635 * cookies shorter than the declared name.
6636 */
6637 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6638 (val_end - att_beg >= t->fe->capture_namelen) &&
6639 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6640 int log_len = val_end - att_beg;
6641
6642 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6643 Alert("HTTP logging : out of memory.\n");
6644 } else {
6645 if (log_len > t->fe->capture_len)
6646 log_len = t->fe->capture_len;
6647 memcpy(txn->cli_cookie, att_beg, log_len);
6648 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006649 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006650 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006651
Willy Tarreaubca99692010-10-06 19:25:55 +02006652 /* Persistence cookies in passive, rewrite or insert mode have the
6653 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006654 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006655 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006656 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006657 * For cookies in prefix mode, the form is :
6658 *
6659 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006660 */
6661 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6662 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6663 struct server *srv = t->be->srv;
6664 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006665
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006666 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6667 * have the server ID between val_beg and delim, and the original cookie between
6668 * delim+1 and val_end. Otherwise, delim==val_end :
6669 *
6670 * Cookie: NAME=SRV; # in all but prefix modes
6671 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6672 * | || || | |+-> next
6673 * | || || | +--> val_end
6674 * | || || +---------> delim
6675 * | || |+------------> val_beg
6676 * | || +-------------> att_end = equal
6677 * | |+-----------------> att_beg
6678 * | +------------------> prev
6679 * +-------------------------> hdr_beg
6680 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006681
Willy Tarreau67402132012-05-31 20:40:20 +02006682 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 for (delim = val_beg; delim < val_end; delim++)
6684 if (*delim == COOKIE_DELIM)
6685 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006686 } else {
6687 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006688 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006689 /* Now check if the cookie contains a date field, which would
6690 * appear after a vertical bar ('|') just after the server name
6691 * and before the delimiter.
6692 */
6693 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6694 if (vbar1) {
6695 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006696 * right is the last seen date. It is a base64 encoded
6697 * 30-bit value representing the UNIX date since the
6698 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006699 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006700 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006701 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006702 if (val_end - vbar1 >= 5) {
6703 val = b64tos30(vbar1);
6704 if (val > 0)
6705 txn->cookie_last_date = val << 2;
6706 }
6707 /* look for a second vertical bar */
6708 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6709 if (vbar1 && (val_end - vbar1 > 5)) {
6710 val = b64tos30(vbar1 + 1);
6711 if (val > 0)
6712 txn->cookie_first_date = val << 2;
6713 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006714 }
6715 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006716
Willy Tarreauf64d1412010-10-07 20:06:11 +02006717 /* if the cookie has an expiration date and the proxy wants to check
6718 * it, then we do that now. We first check if the cookie is too old,
6719 * then only if it has expired. We detect strict overflow because the
6720 * time resolution here is not great (4 seconds). Cookies with dates
6721 * in the future are ignored if their offset is beyond one day. This
6722 * allows an admin to fix timezone issues without expiring everyone
6723 * and at the same time avoids keeping unwanted side effects for too
6724 * long.
6725 */
6726 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006727 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6728 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006729 txn->flags &= ~TX_CK_MASK;
6730 txn->flags |= TX_CK_OLD;
6731 delim = val_beg; // let's pretend we have not found the cookie
6732 txn->cookie_first_date = 0;
6733 txn->cookie_last_date = 0;
6734 }
6735 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006736 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6737 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006738 txn->flags &= ~TX_CK_MASK;
6739 txn->flags |= TX_CK_EXPIRED;
6740 delim = val_beg; // let's pretend we have not found the cookie
6741 txn->cookie_first_date = 0;
6742 txn->cookie_last_date = 0;
6743 }
6744
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006745 /* Here, we'll look for the first running server which supports the cookie.
6746 * This allows to share a same cookie between several servers, for example
6747 * to dedicate backup servers to specific servers only.
6748 * However, to prevent clients from sticking to cookie-less backup server
6749 * when they have incidentely learned an empty cookie, we simply ignore
6750 * empty cookies and mark them as invalid.
6751 * The same behaviour is applied when persistence must be ignored.
6752 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006753 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006754 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006755
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006756 while (srv) {
6757 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6758 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6759 if ((srv->state & SRV_RUNNING) ||
6760 (t->be->options & PR_O_PERSIST) ||
6761 (t->flags & SN_FORCE_PRST)) {
6762 /* we found the server and we can use it */
6763 txn->flags &= ~TX_CK_MASK;
6764 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6765 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006766 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006767 break;
6768 } else {
6769 /* we found a server, but it's down,
6770 * mark it as such and go on in case
6771 * another one is available.
6772 */
6773 txn->flags &= ~TX_CK_MASK;
6774 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006775 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006776 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006777 srv = srv->next;
6778 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006779
Willy Tarreauf64d1412010-10-07 20:06:11 +02006780 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006781 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006782 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006783 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6784 txn->flags |= TX_CK_UNUSED;
6785 else
6786 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006787 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006788
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006789 /* depending on the cookie mode, we may have to either :
6790 * - delete the complete cookie if we're in insert+indirect mode, so that
6791 * the server never sees it ;
6792 * - remove the server id from the cookie value, and tag the cookie as an
6793 * application cookie so that it does not get accidentely removed later,
6794 * if we're in cookie prefix mode
6795 */
Willy Tarreau67402132012-05-31 20:40:20 +02006796 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006797 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006798
Willy Tarreau9b28e032012-10-12 23:49:43 +02006799 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006800 val_end += delta;
6801 next += delta;
6802 hdr_end += delta;
6803 hdr_next += delta;
6804 cur_hdr->len += delta;
6805 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006806
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006807 del_from = NULL;
6808 preserve_hdr = 1; /* we want to keep this cookie */
6809 }
6810 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006811 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006812 del_from = prev;
6813 }
6814 } else {
6815 /* This is not our cookie, so we must preserve it. But if we already
6816 * scheduled another cookie for removal, we cannot remove the
6817 * complete header, but we can remove the previous block itself.
6818 */
6819 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006820
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006821 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006822 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006823 if (att_beg >= del_from)
6824 att_beg += delta;
6825 if (att_end >= del_from)
6826 att_end += delta;
6827 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006828 val_end += delta;
6829 next += delta;
6830 hdr_end += delta;
6831 hdr_next += delta;
6832 cur_hdr->len += delta;
6833 http_msg_move_end(&txn->req, delta);
6834 prev = del_from;
6835 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006836 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006837 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006838
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006839 /* Look for the appsession cookie unless persistence must be ignored */
6840 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6841 int cmp_len, value_len;
6842 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006843
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006844 if (t->be->options2 & PR_O2_AS_PFX) {
6845 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6846 value_begin = att_beg + t->be->appsession_name_len;
6847 value_len = val_end - att_beg - t->be->appsession_name_len;
6848 } else {
6849 cmp_len = att_end - att_beg;
6850 value_begin = val_beg;
6851 value_len = val_end - val_beg;
6852 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006853
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006854 /* let's see if the cookie is our appcookie */
6855 if (cmp_len == t->be->appsession_name_len &&
6856 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6857 manage_client_side_appsession(t, value_begin, value_len);
6858 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006859 }
6860
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006861 /* continue with next cookie on this header line */
6862 att_beg = next;
6863 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006864
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006865 /* There are no more cookies on this line.
6866 * We may still have one (or several) marked for deletion at the
6867 * end of the line. We must do this now in two ways :
6868 * - if some cookies must be preserved, we only delete from the
6869 * mark to the end of line ;
6870 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006871 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006872 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006873 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006874 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006875 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006876 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006877 cur_hdr->len += delta;
6878 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006879 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006880
6881 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006882 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6883 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006884 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006885 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006886 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006887 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006888 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006889 }
6890
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006892 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006893 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006894}
6895
6896
Willy Tarreaua15645d2007-03-18 16:22:39 +01006897/* Iterate the same filter through all response headers contained in <rtr>.
6898 * Returns 1 if this filter can be stopped upon return, otherwise 0.
6899 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006900int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01006901{
6902 char term;
6903 char *cur_ptr, *cur_end, *cur_next;
6904 int cur_idx, old_idx, last_hdr;
6905 struct http_txn *txn = &t->txn;
6906 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006907 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006908
6909 last_hdr = 0;
6910
Willy Tarreau9b28e032012-10-12 23:49:43 +02006911 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006912 old_idx = 0;
6913
6914 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006915 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01006916 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006917 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01006918 (exp->action == ACT_ALLOW ||
6919 exp->action == ACT_DENY))
6920 return 0;
6921
6922 cur_idx = txn->hdr_idx.v[old_idx].next;
6923 if (!cur_idx)
6924 break;
6925
6926 cur_hdr = &txn->hdr_idx.v[cur_idx];
6927 cur_ptr = cur_next;
6928 cur_end = cur_ptr + cur_hdr->len;
6929 cur_next = cur_end + cur_hdr->cr + 1;
6930
6931 /* Now we have one header between cur_ptr and cur_end,
6932 * and the next header starts at cur_next.
6933 */
6934
6935 /* The annoying part is that pattern matching needs
6936 * that we modify the contents to null-terminate all
6937 * strings before testing them.
6938 */
6939
6940 term = *cur_end;
6941 *cur_end = '\0';
6942
6943 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6944 switch (exp->action) {
6945 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006946 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006947 last_hdr = 1;
6948 break;
6949
6950 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006951 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006952 last_hdr = 1;
6953 break;
6954
6955 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006956 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6957 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006958 /* FIXME: if the user adds a newline in the replacement, the
6959 * index will not be recalculated for now, and the new line
6960 * will not be counted as a new header.
6961 */
6962
6963 cur_end += delta;
6964 cur_next += delta;
6965 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006966 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006967 break;
6968
6969 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006970 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006971 cur_next += delta;
6972
Willy Tarreaufa355d42009-11-29 18:12:29 +01006973 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01006974 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6975 txn->hdr_idx.used--;
6976 cur_hdr->len = 0;
6977 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006978 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01006979 break;
6980
6981 }
6982 }
6983 if (cur_end)
6984 *cur_end = term; /* restore the string terminator */
6985
6986 /* keep the link from this header to next one in case of later
6987 * removal of next header.
6988 */
6989 old_idx = cur_idx;
6990 }
6991 return 0;
6992}
6993
6994
6995/* Apply the filter to the status line in the response buffer <rtr>.
6996 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6997 * or -1 if a replacement resulted in an invalid status line.
6998 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006999int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007000{
7001 char term;
7002 char *cur_ptr, *cur_end;
7003 int done;
7004 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007005 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007006
7007
Willy Tarreau3d300592007-03-18 18:34:41 +01007008 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007009 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007010 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007011 (exp->action == ACT_ALLOW ||
7012 exp->action == ACT_DENY))
7013 return 0;
7014 else if (exp->action == ACT_REMOVE)
7015 return 0;
7016
7017 done = 0;
7018
Willy Tarreau9b28e032012-10-12 23:49:43 +02007019 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007020 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007021
7022 /* Now we have the status line between cur_ptr and cur_end */
7023
7024 /* The annoying part is that pattern matching needs
7025 * that we modify the contents to null-terminate all
7026 * strings before testing them.
7027 */
7028
7029 term = *cur_end;
7030 *cur_end = '\0';
7031
7032 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7033 switch (exp->action) {
7034 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007035 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007036 done = 1;
7037 break;
7038
7039 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007040 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007041 done = 1;
7042 break;
7043
7044 case ACT_REPLACE:
7045 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007046 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7047 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007048 /* FIXME: if the user adds a newline in the replacement, the
7049 * index will not be recalculated for now, and the new line
7050 * will not be counted as a new header.
7051 */
7052
Willy Tarreaufa355d42009-11-29 18:12:29 +01007053 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007054 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007055 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007056 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007057 cur_ptr, cur_end + 1,
7058 NULL, NULL);
7059 if (unlikely(!cur_end))
7060 return -1;
7061
7062 /* we have a full respnse and we know that we have either a CR
7063 * or an LF at <ptr>.
7064 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007065 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007066 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007067 /* there is no point trying this regex on headers */
7068 return 1;
7069 }
7070 }
7071 *cur_end = term; /* restore the string terminator */
7072 return done;
7073}
7074
7075
7076
7077/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007078 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007079 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7080 * unparsable response.
7081 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007082int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007083{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007084 struct http_txn *txn = &s->txn;
7085 struct hdr_exp *exp;
7086
7087 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007088 int ret;
7089
7090 /*
7091 * The interleaving of transformations and verdicts
7092 * makes it difficult to decide to continue or stop
7093 * the evaluation.
7094 */
7095
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007096 if (txn->flags & TX_SVDENY)
7097 break;
7098
Willy Tarreau3d300592007-03-18 18:34:41 +01007099 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007100 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7101 exp->action == ACT_PASS)) {
7102 exp = exp->next;
7103 continue;
7104 }
7105
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007106 /* if this filter had a condition, evaluate it now and skip to
7107 * next filter if the condition does not match.
7108 */
7109 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007110 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007111 ret = acl_pass(ret);
7112 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7113 ret = !ret;
7114 if (!ret)
7115 continue;
7116 }
7117
Willy Tarreaua15645d2007-03-18 16:22:39 +01007118 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007119 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007120 if (unlikely(ret < 0))
7121 return -1;
7122
7123 if (likely(ret == 0)) {
7124 /* The filter did not match the response, it can be
7125 * iterated through all headers.
7126 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007127 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007128 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007129 }
7130 return 0;
7131}
7132
7133
Willy Tarreaua15645d2007-03-18 16:22:39 +01007134/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007135 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007136 * desirable to call it only when needed. This function is also used when we
7137 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007138 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007139void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007140{
7141 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007142 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007143 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007144 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007145 char *hdr_beg, *hdr_end, *hdr_next;
7146 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007147
Willy Tarreaua15645d2007-03-18 16:22:39 +01007148 /* Iterate through the headers.
7149 * we start with the start line.
7150 */
7151 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007152 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007153
7154 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7155 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007156 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007157
7158 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007159 hdr_beg = hdr_next;
7160 hdr_end = hdr_beg + cur_hdr->len;
7161 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007162
Willy Tarreau24581ba2010-08-31 22:39:35 +02007163 /* We have one full header between hdr_beg and hdr_end, and the
7164 * next header starts at hdr_next. We're only interested in
7165 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007166 */
7167
Willy Tarreau24581ba2010-08-31 22:39:35 +02007168 is_cookie2 = 0;
7169 prev = hdr_beg + 10;
7170 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007171 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007172 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7173 if (!val) {
7174 old_idx = cur_idx;
7175 continue;
7176 }
7177 is_cookie2 = 1;
7178 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007179 }
7180
Willy Tarreau24581ba2010-08-31 22:39:35 +02007181 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7182 * <prev> points to the colon.
7183 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007184 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185
Willy Tarreau24581ba2010-08-31 22:39:35 +02007186 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7187 * check-cache is enabled) and we are not interested in checking
7188 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007189 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007190 if (t->be->cookie_name == NULL &&
7191 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007192 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007193 return;
7194
Willy Tarreau24581ba2010-08-31 22:39:35 +02007195 /* OK so now we know we have to process this response cookie.
7196 * The format of the Set-Cookie header is slightly different
7197 * from the format of the Cookie header in that it does not
7198 * support the comma as a cookie delimiter (thus the header
7199 * cannot be folded) because the Expires attribute described in
7200 * the original Netscape's spec may contain an unquoted date
7201 * with a comma inside. We have to live with this because
7202 * many browsers don't support Max-Age and some browsers don't
7203 * support quoted strings. However the Set-Cookie2 header is
7204 * clean.
7205 *
7206 * We have to keep multiple pointers in order to support cookie
7207 * removal at the beginning, middle or end of header without
7208 * corrupting the header (in case of set-cookie2). A special
7209 * pointer, <scav> points to the beginning of the set-cookie-av
7210 * fields after the first semi-colon. The <next> pointer points
7211 * either to the end of line (set-cookie) or next unquoted comma
7212 * (set-cookie2). All of these headers are valid :
7213 *
7214 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7215 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7216 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7217 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7218 * | | | | | | | | | |
7219 * | | | | | | | | +-> next hdr_end <--+
7220 * | | | | | | | +------------> scav
7221 * | | | | | | +--------------> val_end
7222 * | | | | | +--------------------> val_beg
7223 * | | | | +----------------------> equal
7224 * | | | +------------------------> att_end
7225 * | | +----------------------------> att_beg
7226 * | +------------------------------> prev
7227 * +-----------------------------------------> hdr_beg
7228 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007229
Willy Tarreau24581ba2010-08-31 22:39:35 +02007230 for (; prev < hdr_end; prev = next) {
7231 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007232
Willy Tarreau24581ba2010-08-31 22:39:35 +02007233 /* find att_beg */
7234 att_beg = prev + 1;
7235 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7236 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007237
Willy Tarreau24581ba2010-08-31 22:39:35 +02007238 /* find att_end : this is the first character after the last non
7239 * space before the equal. It may be equal to hdr_end.
7240 */
7241 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242
Willy Tarreau24581ba2010-08-31 22:39:35 +02007243 while (equal < hdr_end) {
7244 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7245 break;
7246 if (http_is_spht[(unsigned char)*equal++])
7247 continue;
7248 att_end = equal;
7249 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007250
Willy Tarreau24581ba2010-08-31 22:39:35 +02007251 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7252 * is between <att_beg> and <equal>, both may be identical.
7253 */
7254
7255 /* look for end of cookie if there is an equal sign */
7256 if (equal < hdr_end && *equal == '=') {
7257 /* look for the beginning of the value */
7258 val_beg = equal + 1;
7259 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7260 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007261
Willy Tarreau24581ba2010-08-31 22:39:35 +02007262 /* find the end of the value, respecting quotes */
7263 next = find_cookie_value_end(val_beg, hdr_end);
7264
7265 /* make val_end point to the first white space or delimitor after the value */
7266 val_end = next;
7267 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7268 val_end--;
7269 } else {
7270 /* <equal> points to next comma, semi-colon or EOL */
7271 val_beg = val_end = next = equal;
7272 }
7273
7274 if (next < hdr_end) {
7275 /* Set-Cookie2 supports multiple cookies, and <next> points to
7276 * a colon or semi-colon before the end. So skip all attr-value
7277 * pairs and look for the next comma. For Set-Cookie, since
7278 * commas are permitted in values, skip to the end.
7279 */
7280 if (is_cookie2)
7281 next = find_hdr_value_end(next, hdr_end);
7282 else
7283 next = hdr_end;
7284 }
7285
7286 /* Now everything is as on the diagram above */
7287
7288 /* Ignore cookies with no equal sign */
7289 if (equal == val_end)
7290 continue;
7291
7292 /* If there are spaces around the equal sign, we need to
7293 * strip them otherwise we'll get trouble for cookie captures,
7294 * or even for rewrites. Since this happens extremely rarely,
7295 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007296 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007297 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7298 int stripped_before = 0;
7299 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007300
Willy Tarreau24581ba2010-08-31 22:39:35 +02007301 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007302 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007303 equal += stripped_before;
7304 val_beg += stripped_before;
7305 }
7306
7307 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007308 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007309 val_beg += stripped_after;
7310 stripped_before += stripped_after;
7311 }
7312
7313 val_end += stripped_before;
7314 next += stripped_before;
7315 hdr_end += stripped_before;
7316 hdr_next += stripped_before;
7317 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007318 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007319 }
7320
7321 /* First, let's see if we want to capture this cookie. We check
7322 * that we don't already have a server side cookie, because we
7323 * can only capture one. Also as an optimisation, we ignore
7324 * cookies shorter than the declared name.
7325 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007326 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007327 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007328 (val_end - att_beg >= t->fe->capture_namelen) &&
7329 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7330 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007331 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007332 Alert("HTTP logging : out of memory.\n");
7333 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007334 else {
7335 if (log_len > t->fe->capture_len)
7336 log_len = t->fe->capture_len;
7337 memcpy(txn->srv_cookie, att_beg, log_len);
7338 txn->srv_cookie[log_len] = 0;
7339 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007340 }
7341
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007342 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007343 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007344 if (!(t->flags & SN_IGNORE_PRST) &&
7345 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7346 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007347 /* assume passive cookie by default */
7348 txn->flags &= ~TX_SCK_MASK;
7349 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007350
7351 /* If the cookie is in insert mode on a known server, we'll delete
7352 * this occurrence because we'll insert another one later.
7353 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007354 * a direct access.
7355 */
Willy Tarreau67402132012-05-31 20:40:20 +02007356 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007357 /* The "preserve" flag was set, we don't want to touch the
7358 * server's cookie.
7359 */
7360 }
Willy Tarreau67402132012-05-31 20:40:20 +02007361 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7362 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007363 /* this cookie must be deleted */
7364 if (*prev == ':' && next == hdr_end) {
7365 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007366 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007367 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7368 txn->hdr_idx.used--;
7369 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007370 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007371 hdr_next += delta;
7372 http_msg_move_end(&txn->rsp, delta);
7373 /* note: while both invalid now, <next> and <hdr_end>
7374 * are still equal, so the for() will stop as expected.
7375 */
7376 } else {
7377 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007378 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007379 next = prev;
7380 hdr_end += delta;
7381 hdr_next += delta;
7382 cur_hdr->len += delta;
7383 http_msg_move_end(&txn->rsp, delta);
7384 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007385 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007386 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007387 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007388 }
Willy Tarreau67402132012-05-31 20:40:20 +02007389 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007390 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007391 * with this server since we know it.
7392 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007393 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007394 next += delta;
7395 hdr_end += delta;
7396 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007397 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007398 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007399
Willy Tarreauf1348312010-10-07 15:54:11 +02007400 txn->flags &= ~TX_SCK_MASK;
7401 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007402 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007403 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007404 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007405 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007406 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007407 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007408 next += delta;
7409 hdr_end += delta;
7410 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007411 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007412 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007413
Willy Tarreau827aee92011-03-10 16:55:02 +01007414 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007415 txn->flags &= ~TX_SCK_MASK;
7416 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007417 }
7418 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007419 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7420 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007421 int cmp_len, value_len;
7422 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007423
Cyril Bontéb21570a2009-11-29 20:04:48 +01007424 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007425 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7426 value_begin = att_beg + t->be->appsession_name_len;
7427 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007428 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007429 cmp_len = att_end - att_beg;
7430 value_begin = val_beg;
7431 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007432 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007433
Cyril Bonté17530c32010-04-06 21:11:10 +02007434 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007435 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7436 /* free a possibly previously allocated memory */
7437 pool_free2(apools.sessid, txn->sessid);
7438
Cyril Bontéb21570a2009-11-29 20:04:48 +01007439 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007440 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007441 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7442 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7443 return;
7444 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007445 memcpy(txn->sessid, value_begin, value_len);
7446 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007447 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007448 }
7449 /* that's done for this cookie, check the next one on the same
7450 * line when next != hdr_end (only if is_cookie2).
7451 */
7452 }
7453 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007454 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007455 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007456
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007457 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007458 appsess *asession = NULL;
7459 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007460 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007461 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007462 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007463 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7464 Alert("Not enough Memory process_srv():asession:calloc().\n");
7465 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7466 return;
7467 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007468 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7469
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007470 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7471 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7472 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007473 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007474 return;
7475 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007476 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007477 asession->sessid[t->be->appsession_len] = 0;
7478
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007479 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007480 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007481 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007482 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007483 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007484 return;
7485 }
7486 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007487 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007488
7489 asession->request_count = 0;
7490 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7491 }
7492
7493 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7494 asession->request_count++;
7495 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007496}
7497
7498
Willy Tarreaua15645d2007-03-18 16:22:39 +01007499/*
7500 * Check if response is cacheable or not. Updates t->flags.
7501 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007502void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007503{
7504 struct http_txn *txn = &t->txn;
7505 char *p1, *p2;
7506
7507 char *cur_ptr, *cur_end, *cur_next;
7508 int cur_idx;
7509
Willy Tarreau5df51872007-11-25 16:20:08 +01007510 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007511 return;
7512
7513 /* Iterate through the headers.
7514 * we start with the start line.
7515 */
7516 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007517 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007518
7519 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7520 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007521 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007522
7523 cur_hdr = &txn->hdr_idx.v[cur_idx];
7524 cur_ptr = cur_next;
7525 cur_end = cur_ptr + cur_hdr->len;
7526 cur_next = cur_end + cur_hdr->cr + 1;
7527
7528 /* We have one full header between cur_ptr and cur_end, and the
7529 * next header starts at cur_next. We're only interested in
7530 * "Cookie:" headers.
7531 */
7532
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007533 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7534 if (val) {
7535 if ((cur_end - (cur_ptr + val) >= 8) &&
7536 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7537 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7538 return;
7539 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007540 }
7541
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007542 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7543 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007544 continue;
7545
7546 /* OK, right now we know we have a cache-control header at cur_ptr */
7547
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007548 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007549
7550 if (p1 >= cur_end) /* no more info */
7551 continue;
7552
7553 /* p1 is at the beginning of the value */
7554 p2 = p1;
7555
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007556 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007557 p2++;
7558
7559 /* we have a complete value between p1 and p2 */
7560 if (p2 < cur_end && *p2 == '=') {
7561 /* we have something of the form no-cache="set-cookie" */
7562 if ((cur_end - p1 >= 21) &&
7563 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7564 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007565 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007566 continue;
7567 }
7568
7569 /* OK, so we know that either p2 points to the end of string or to a comma */
7570 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7571 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7572 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7573 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007574 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007575 return;
7576 }
7577
7578 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007579 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007580 continue;
7581 }
7582 }
7583}
7584
7585
Willy Tarreau58f10d72006-12-04 02:26:12 +01007586/*
7587 * Try to retrieve a known appsession in the URI, then the associated server.
7588 * If the server is found, it's assigned to the session.
7589 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007590void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007591{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007592 char *end_params, *first_param, *cur_param, *next_param;
7593 char separator;
7594 int value_len;
7595
7596 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007597
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007598 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007599 (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 +01007600 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007601 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007602
Cyril Bontéb21570a2009-11-29 20:04:48 +01007603 first_param = NULL;
7604 switch (mode) {
7605 case PR_O2_AS_M_PP:
7606 first_param = memchr(begin, ';', len);
7607 break;
7608 case PR_O2_AS_M_QS:
7609 first_param = memchr(begin, '?', len);
7610 break;
7611 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007612
Cyril Bontéb21570a2009-11-29 20:04:48 +01007613 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007614 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007615 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007616
Cyril Bontéb21570a2009-11-29 20:04:48 +01007617 switch (mode) {
7618 case PR_O2_AS_M_PP:
7619 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7620 end_params = (char *) begin + len;
7621 }
7622 separator = ';';
7623 break;
7624 case PR_O2_AS_M_QS:
7625 end_params = (char *) begin + len;
7626 separator = '&';
7627 break;
7628 default:
7629 /* unknown mode, shouldn't happen */
7630 return;
7631 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007632
Cyril Bontéb21570a2009-11-29 20:04:48 +01007633 cur_param = next_param = end_params;
7634 while (cur_param > first_param) {
7635 cur_param--;
7636 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7637 /* let's see if this is the appsession parameter */
7638 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7639 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7640 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7641 /* Cool... it's the right one */
7642 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7643 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7644 if (value_len > 0) {
7645 manage_client_side_appsession(t, cur_param, value_len);
7646 }
7647 break;
7648 }
7649 next_param = cur_param;
7650 }
7651 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007652#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007653 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007654 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007655#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007656}
7657
Willy Tarreaub2513902006-12-17 14:52:38 +01007658/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007659 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007660 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007661 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007662 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007663 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007664 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007665 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007666 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007667int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007668{
7669 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007670 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007671 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007672 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007673
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007674 if (!uri_auth)
7675 return 0;
7676
Cyril Bonté70be45d2010-10-12 00:14:35 +02007677 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007678 return 0;
7679
Willy Tarreau295a8372011-03-10 11:25:07 +01007680 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007681 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007682 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007683
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007684 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007685 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007686 return 0;
7687
Willy Tarreau3a215be2012-03-09 21:39:51 +01007688 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007689 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007690 return 0;
7691
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007692 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007693 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007694 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007695 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007696 break;
7697 }
7698 h++;
7699 }
7700
7701 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007702 h = uri + uri_auth->uri_len;
7703 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007704 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007705 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007706 break;
7707 }
7708 h++;
7709 }
7710 }
7711
Willy Tarreau3a215be2012-03-09 21:39:51 +01007712 h = uri + uri_auth->uri_len;
7713 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007714 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007715 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007716 break;
7717 }
7718 h++;
7719 }
7720
Willy Tarreau3a215be2012-03-09 21:39:51 +01007721 h = uri + uri_auth->uri_len;
7722 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007723 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007724 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007725 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007726 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007727 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7728 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7729 si->applet.ctx.stats.st_code = i;
7730 break;
7731 }
7732 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007733 break;
7734 }
7735 h++;
7736 }
Willy Tarreaub2513902006-12-17 14:52:38 +01007737 return 1;
7738}
7739
Willy Tarreau4076a152009-04-02 15:18:36 +02007740/*
7741 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007742 * By default it tries to report the error position as msg->err_pos. However if
7743 * this one is not set, it will then report msg->next, which is the last known
7744 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007745 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007746 */
7747void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007748 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007749 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007750{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007751 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007752 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007753
Willy Tarreau9b28e032012-10-12 23:49:43 +02007754 es->len = MIN(chn->buf->i, sizeof(es->buf));
7755 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007756 len1 = MIN(len1, es->len);
7757 len2 = es->len - len1; /* remaining data if buffer wraps */
7758
Willy Tarreau9b28e032012-10-12 23:49:43 +02007759 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007760 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007761 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007762
Willy Tarreau4076a152009-04-02 15:18:36 +02007763 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007764 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007765 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007766 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007767
Willy Tarreau4076a152009-04-02 15:18:36 +02007768 es->when = date; // user-visible date
7769 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007770 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007771 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007772 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007773 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007774 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007775 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007776 es->s_flags = s->flags;
7777 es->t_flags = s->txn.flags;
7778 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007779 es->b_out = chn->buf->o;
7780 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007781 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007782 es->m_clen = msg->chunk_len;
7783 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007784}
Willy Tarreaub2513902006-12-17 14:52:38 +01007785
Willy Tarreau294c4732011-12-16 21:35:50 +01007786/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7787 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7788 * performed over the whole headers. Otherwise it must contain a valid header
7789 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7790 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7791 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7792 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7793 * -1.
7794 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007795 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007796unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007797 struct hdr_idx *idx, int occ,
7798 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007799{
Willy Tarreau294c4732011-12-16 21:35:50 +01007800 struct hdr_ctx local_ctx;
7801 char *ptr_hist[MAX_HDR_HISTORY];
7802 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007803 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007804 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007805
Willy Tarreau294c4732011-12-16 21:35:50 +01007806 if (!ctx) {
7807 local_ctx.idx = 0;
7808 ctx = &local_ctx;
7809 }
7810
Willy Tarreaubce70882009-09-07 11:51:47 +02007811 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007812 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007813 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007814 occ--;
7815 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007816 *vptr = ctx->line + ctx->val;
7817 *vlen = ctx->vlen;
7818 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007819 }
7820 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007821 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007822 }
7823
7824 /* negative occurrence, we scan all the list then walk back */
7825 if (-occ > MAX_HDR_HISTORY)
7826 return 0;
7827
Willy Tarreau294c4732011-12-16 21:35:50 +01007828 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007829 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007830 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7831 len_hist[hist_ptr] = ctx->vlen;
7832 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007833 hist_ptr = 0;
7834 found++;
7835 }
7836 if (-occ > found)
7837 return 0;
7838 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7839 * find occurrence -occ, so we have to check [hist_ptr+occ].
7840 */
7841 hist_ptr += occ;
7842 if (hist_ptr >= MAX_HDR_HISTORY)
7843 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007844 *vptr = ptr_hist[hist_ptr];
7845 *vlen = len_hist[hist_ptr];
7846 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007847}
7848
Willy Tarreaubaaee002006-06-26 02:48:02 +02007849/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007850 * Print a debug line with a header. Always stop at the first CR or LF char,
7851 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7852 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007853 */
7854void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7855{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007856 int max;
7857 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01007858 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
7859 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02007860
7861 for (max = 0; start + max < end; max++)
7862 if (start[max] == '\r' || start[max] == '\n')
7863 break;
7864
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007865 UBOUND(max, trash.size - trash.len - 3);
7866 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
7867 trash.str[trash.len++] = '\n';
7868 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007869}
7870
Willy Tarreau0937bc42009-12-22 15:03:09 +01007871/*
7872 * Initialize a new HTTP transaction for session <s>. It is assumed that all
7873 * the required fields are properly allocated and that we only need to (re)init
7874 * them. This should be used before processing any new request.
7875 */
7876void http_init_txn(struct session *s)
7877{
7878 struct http_txn *txn = &s->txn;
7879 struct proxy *fe = s->fe;
7880
7881 txn->flags = 0;
7882 txn->status = -1;
7883
William Lallemand5f232402012-04-05 18:02:55 +02007884 global.req_count++;
7885
Willy Tarreauf64d1412010-10-07 20:06:11 +02007886 txn->cookie_first_date = 0;
7887 txn->cookie_last_date = 0;
7888
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007889 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007890 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007891 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01007892 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02007893 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01007894 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01007895 txn->req.chunk_len = 0LL;
7896 txn->req.body_len = 0LL;
7897 txn->rsp.chunk_len = 0LL;
7898 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007899 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
7900 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02007901 txn->req.chn = s->req;
7902 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007903
7904 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007905
7906 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
7907 if (fe->options2 & PR_O2_REQBUG_OK)
7908 txn->req.err_pos = -1; /* let buggy requests pass */
7909
Willy Tarreau46023632010-01-07 22:51:47 +01007910 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007911 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
7912
Willy Tarreau46023632010-01-07 22:51:47 +01007913 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01007914 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
7915
7916 if (txn->hdr_idx.v)
7917 hdr_idx_init(&txn->hdr_idx);
7918}
7919
7920/* to be used at the end of a transaction */
7921void http_end_txn(struct session *s)
7922{
7923 struct http_txn *txn = &s->txn;
7924
7925 /* these ones will have been dynamically allocated */
7926 pool_free2(pool2_requri, txn->uri);
7927 pool_free2(pool2_capture, txn->cli_cookie);
7928 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007929 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01007930 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01007931
William Lallemanda73203e2012-03-12 12:48:57 +01007932 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007933 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007934 txn->uri = NULL;
7935 txn->srv_cookie = NULL;
7936 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01007937
7938 if (txn->req.cap) {
7939 struct cap_hdr *h;
7940 for (h = s->fe->req_cap; h; h = h->next)
7941 pool_free2(h->pool, txn->req.cap[h->index]);
7942 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
7943 }
7944
7945 if (txn->rsp.cap) {
7946 struct cap_hdr *h;
7947 for (h = s->fe->rsp_cap; h; h = h->next)
7948 pool_free2(h->pool, txn->rsp.cap[h->index]);
7949 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
7950 }
7951
Willy Tarreau0937bc42009-12-22 15:03:09 +01007952}
7953
7954/* to be used at the end of a transaction to prepare a new one */
7955void http_reset_txn(struct session *s)
7956{
7957 http_end_txn(s);
7958 http_init_txn(s);
7959
7960 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007961 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09007962 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007963 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01007964 /* re-init store persistence */
7965 s->store_count = 0;
7966
Willy Tarreau0937bc42009-12-22 15:03:09 +01007967 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007968
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02007969 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01007970
Willy Tarreau739cfba2010-01-25 23:11:14 +01007971 /* We must trim any excess data from the response buffer, because we
7972 * may have blocked an invalid response from a server that we don't
7973 * want to accidentely forward once we disable the analysers, nor do
7974 * we want those data to come along with next response. A typical
7975 * example of such data would be from a buggy server responding to
7976 * a HEAD with some data, or sending more than the advertised
7977 * content-length.
7978 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007979 if (unlikely(s->rep->buf->i))
7980 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01007981
Willy Tarreau0937bc42009-12-22 15:03:09 +01007982 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02007983 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007984
Willy Tarreaud04e8582010-05-31 12:31:35 +02007985 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007986 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01007987
7988 s->req->rex = TICK_ETERNITY;
7989 s->req->wex = TICK_ETERNITY;
7990 s->req->analyse_exp = TICK_ETERNITY;
7991 s->rep->rex = TICK_ETERNITY;
7992 s->rep->wex = TICK_ETERNITY;
7993 s->rep->analyse_exp = TICK_ETERNITY;
7994}
Willy Tarreau58f10d72006-12-04 02:26:12 +01007995
Willy Tarreauff011f22011-01-06 17:51:27 +01007996void free_http_req_rules(struct list *r) {
7997 struct http_req_rule *tr, *pr;
7998
7999 list_for_each_entry_safe(pr, tr, r, list) {
8000 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008001 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008002 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008003
8004 free(pr);
8005 }
8006}
8007
8008struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8009{
8010 struct http_req_rule *rule;
8011 int cur_arg;
8012
8013 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8014 if (!rule) {
8015 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
8016 return NULL;
8017 }
8018
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008019 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008020 rule->action = HTTP_REQ_ACT_ALLOW;
8021 cur_arg = 1;
8022 } else if (!strcmp(args[0], "deny")) {
8023 rule->action = HTTP_REQ_ACT_DENY;
8024 cur_arg = 1;
8025 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008026 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008027 cur_arg = 1;
8028
8029 while(*args[cur_arg]) {
8030 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008031 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008032 cur_arg+=2;
8033 continue;
8034 } else
8035 break;
8036 }
Willy Tarreau20b0de52012-12-24 15:45:22 +01008037 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8038 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8039 cur_arg = 1;
8040
8041 if (!*args[cur_arg] || !*args[cur_arg+1] || *args[cur_arg+2]) {
8042 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8043 file, linenum, args[0]);
8044 return NULL;
8045 }
8046
8047 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8048 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8049 LIST_INIT(&rule->arg.hdr_add.fmt);
8050 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, PR_MODE_HTTP);
8051 cur_arg += 2;
Willy Tarreauff011f22011-01-06 17:51:27 +01008052 } else {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008053 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'add-header', 'set-header', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008054 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreauff011f22011-01-06 17:51:27 +01008055 return NULL;
8056 }
8057
8058 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8059 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008060 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008061
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008062 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8063 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8064 file, linenum, args[0], errmsg);
8065 free(errmsg);
Willy Tarreauff011f22011-01-06 17:51:27 +01008066 return NULL;
8067 }
8068 rule->cond = cond;
8069 }
8070 else if (*args[cur_arg]) {
8071 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8072 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8073 file, linenum, args[0], args[cur_arg]);
8074 return NULL;
8075 }
8076
8077 return rule;
8078}
8079
Willy Tarreau8797c062007-05-07 00:55:35 +02008080/************************************************************************/
8081/* The code below is dedicated to ACL parsing and matching */
8082/************************************************************************/
8083
8084
Willy Tarreau14174bc2012-04-16 14:34:04 +02008085/* This function ensures that the prerequisites for an L7 fetch are ready,
8086 * which means that a request or response is ready. If some data is missing,
8087 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008088 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8089 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008090 *
8091 * The function returns :
8092 * 0 if some data is missing or if the requested data cannot be fetched
8093 * -1 if it is certain that we'll never have any HTTP message there
8094 * 1 if an HTTP message is ready
8095 */
8096static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008097acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008098 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008099{
8100 struct http_txn *txn = l7;
8101 struct http_msg *msg = &txn->req;
8102
8103 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8104 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8105 */
8106
8107 if (unlikely(!s || !txn))
8108 return 0;
8109
8110 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008111 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008112
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008113 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008114 if (unlikely(!s->req))
8115 return 0;
8116
8117 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008118 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008119 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008120 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008121 return -1;
8122 }
8123
8124 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008125 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008126 http_msg_analyzer(msg, &txn->hdr_idx);
8127
8128 /* Still no valid request ? */
8129 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008130 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008131 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008132 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008133 return -1;
8134 }
8135 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008136 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008137 return 0;
8138 }
8139
8140 /* OK we just got a valid HTTP request. We have some minor
8141 * preparation to perform so that further checks can rely
8142 * on HTTP tests.
8143 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008144 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008145 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8146 s->flags |= SN_REDIRECTABLE;
8147
8148 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008149 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008150 return -1;
8151 }
8152 }
8153
Willy Tarreau24e32d82012-04-23 23:55:44 +02008154 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008155 return 0; /* data might have moved and indexes changed */
8156
8157 /* otherwise everything's ready for the request */
8158 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008159 else {
8160 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02008161 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8162 return 0;
8163 }
8164
8165 /* everything's OK */
8166 return 1;
8167}
Willy Tarreau8797c062007-05-07 00:55:35 +02008168
Willy Tarreauc0239e02012-04-16 14:42:55 +02008169#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008170 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 +02008171
Willy Tarreau24e32d82012-04-23 23:55:44 +02008172#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008173 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 +02008174
Willy Tarreau8797c062007-05-07 00:55:35 +02008175
8176/* 1. Check on METHOD
8177 * We use the pre-parsed method if it is known, and store its number as an
8178 * integer. If it is unknown, we use the pointer and the length.
8179 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008180static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008181{
8182 int len, meth;
8183
Willy Tarreauae8b7962007-06-09 23:10:04 +02008184 len = strlen(*text);
8185 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008186
8187 pattern->val.i = meth;
8188 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008189 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008190 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008191 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008192 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008193 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008194 pattern->len = len;
8195 }
8196 return 1;
8197}
8198
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008199/* This function fetches the method of current HTTP request and stores
8200 * it in the global pattern struct as a chunk. There are two possibilities :
8201 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8202 * in <len> and <ptr> is NULL ;
8203 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8204 * <len> to its length.
8205 * This is intended to be used with acl_match_meth() only.
8206 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008207static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008208acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008209 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008210{
8211 int meth;
8212 struct http_txn *txn = l7;
8213
Willy Tarreau24e32d82012-04-23 23:55:44 +02008214 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008215
Willy Tarreau8797c062007-05-07 00:55:35 +02008216 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008217 smp->type = SMP_T_UINT;
8218 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008219 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008220 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8221 /* ensure the indexes are not affected */
8222 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008223 smp->type = SMP_T_CSTR;
8224 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008225 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008226 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008227 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008228 return 1;
8229}
8230
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008231/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008232static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008233{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008234 int icase;
8235
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008236
Willy Tarreauf853c462012-04-23 18:53:56 +02008237 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008238 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008239 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008240 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008241 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008242 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008243
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008244 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8245 if (pattern->val.i != HTTP_METH_OTHER)
8246 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008247
8248 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008249 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008250 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008251
8252 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008253 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8254 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008255 return ACL_PAT_FAIL;
8256 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008257}
8258
8259/* 2. Check on Request/Status Version
8260 * We simply compare strings here.
8261 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008262static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008263{
Willy Tarreauae8b7962007-06-09 23:10:04 +02008264 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008265 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008266 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008267 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008268 }
Willy Tarreauae8b7962007-06-09 23:10:04 +02008269 pattern->len = strlen(*text);
Willy Tarreau8797c062007-05-07 00:55:35 +02008270 return 1;
8271}
8272
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008273static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008274acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008275 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008276{
8277 struct http_txn *txn = l7;
8278 char *ptr;
8279 int len;
8280
Willy Tarreauc0239e02012-04-16 14:42:55 +02008281 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008282
Willy Tarreau8797c062007-05-07 00:55:35 +02008283 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008284 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008285
8286 while ((len-- > 0) && (*ptr++ != '/'));
8287 if (len <= 0)
8288 return 0;
8289
Willy Tarreauf853c462012-04-23 18:53:56 +02008290 smp->type = SMP_T_CSTR;
8291 smp->data.str.str = ptr;
8292 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008293
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008294 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008295 return 1;
8296}
8297
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008298static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008299acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008300 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008301{
8302 struct http_txn *txn = l7;
8303 char *ptr;
8304 int len;
8305
Willy Tarreauc0239e02012-04-16 14:42:55 +02008306 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008307
Willy Tarreauf26b2522012-12-14 08:33:14 +01008308 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8309 return 0;
8310
Willy Tarreau8797c062007-05-07 00:55:35 +02008311 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008312 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008313
8314 while ((len-- > 0) && (*ptr++ != '/'));
8315 if (len <= 0)
8316 return 0;
8317
Willy Tarreauf853c462012-04-23 18:53:56 +02008318 smp->type = SMP_T_CSTR;
8319 smp->data.str.str = ptr;
8320 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008321
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008322 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008323 return 1;
8324}
8325
8326/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008327static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008328acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008329 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008330{
8331 struct http_txn *txn = l7;
8332 char *ptr;
8333 int len;
8334
Willy Tarreauc0239e02012-04-16 14:42:55 +02008335 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008336
Willy Tarreauf26b2522012-12-14 08:33:14 +01008337 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8338 return 0;
8339
Willy Tarreau8797c062007-05-07 00:55:35 +02008340 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008341 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008342
Willy Tarreauf853c462012-04-23 18:53:56 +02008343 smp->type = SMP_T_UINT;
8344 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008345 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008346 return 1;
8347}
8348
8349/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008350static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008351smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008352 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008353{
8354 struct http_txn *txn = l7;
8355
Willy Tarreauc0239e02012-04-16 14:42:55 +02008356 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008357
Willy Tarreauf853c462012-04-23 18:53:56 +02008358 smp->type = SMP_T_CSTR;
8359 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008360 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008361 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008362 return 1;
8363}
8364
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008365static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008366smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008367 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008368{
8369 struct http_txn *txn = l7;
8370
Willy Tarreauc0239e02012-04-16 14:42:55 +02008371 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008372
8373 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008374 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8375 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008376 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008377 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008378 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008379
8380 /*
8381 * If we are parsing url in frontend space, we prepare backend stage
8382 * to not parse again the same url ! optimization lazyness...
8383 */
8384 if (px->options & PR_O_HTTP_PROXY)
8385 l4->flags |= SN_ADDR_SET;
8386
Willy Tarreau37406352012-04-23 16:16:37 +02008387 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008388 return 1;
8389}
8390
8391static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008392smp_fetch_url_port(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)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008394{
8395 struct http_txn *txn = l7;
8396
Willy Tarreauc0239e02012-04-16 14:42:55 +02008397 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008398
8399 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008400 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 +02008401 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008402 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008403
8404 if (px->options & PR_O_HTTP_PROXY)
8405 l4->flags |= SN_ADDR_SET;
8406
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008407 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008408 return 1;
8409}
8410
Willy Tarreau185b5c42012-04-26 15:11:51 +02008411/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8412 * Accepts an optional argument of type string containing the header field name,
8413 * and an optional argument of type signed or unsigned integer to request an
8414 * explicit occurrence of the header. Note that in the event of a missing name,
8415 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008416 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008417static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008418smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008419 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008420{
8421 struct http_txn *txn = l7;
8422 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008423 struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008424 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008425 int occ = 0;
8426 const char *name_str = NULL;
8427 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008428
Willy Tarreau185b5c42012-04-26 15:11:51 +02008429 if (args) {
8430 if (args[0].type != ARGT_STR)
8431 return 0;
8432 name_str = args[0].data.str.str;
8433 name_len = args[0].data.str.len;
8434
8435 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8436 occ = args[1].data.uint;
8437 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008438
Willy Tarreaue333ec92012-04-16 16:26:40 +02008439 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008440
Willy Tarreau185b5c42012-04-26 15:11:51 +02008441 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008442 /* search for header from the beginning */
8443 ctx->idx = 0;
8444
Willy Tarreau185b5c42012-04-26 15:11:51 +02008445 if (!occ && !(opt & SMP_OPT_ITERATE))
8446 /* no explicit occurrence and single fetch => last header by default */
8447 occ = -1;
8448
8449 if (!occ)
8450 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008451 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008452
Willy Tarreau185b5c42012-04-26 15:11:51 +02008453 smp->type = SMP_T_CSTR;
8454 smp->flags |= SMP_F_VOL_HDR;
8455 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 +02008456 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008457
Willy Tarreau37406352012-04-23 16:16:37 +02008458 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008459 return 0;
8460}
8461
Willy Tarreauc11416f2007-06-17 16:58:38 +02008462/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008463 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008464 */
8465static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008466smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008467 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008468{
8469 struct http_txn *txn = l7;
8470 struct hdr_idx *idx = &txn->hdr_idx;
8471 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008472 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008473 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008474
Willy Tarreau24e32d82012-04-23 23:55:44 +02008475 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008476 return 0;
8477
Willy Tarreaue333ec92012-04-16 16:26:40 +02008478 CHECK_HTTP_MESSAGE_FIRST();
8479
Willy Tarreau33a7e692007-06-10 19:45:56 +02008480 ctx.idx = 0;
8481 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008482 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 +02008483 cnt++;
8484
Willy Tarreauf853c462012-04-23 18:53:56 +02008485 smp->type = SMP_T_UINT;
8486 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008487 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008488 return 1;
8489}
8490
Willy Tarreau185b5c42012-04-26 15:11:51 +02008491/* Fetch an HTTP header's integer value. The integer value is returned. It
8492 * takes a mandatory argument of type string and an optional one of type int
8493 * to designate a specific occurrence. It returns an unsigned integer, which
8494 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008495 */
8496static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008497smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008498 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008499{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008500 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008501
Willy Tarreauf853c462012-04-23 18:53:56 +02008502 if (ret > 0) {
8503 smp->type = SMP_T_UINT;
8504 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8505 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008506
Willy Tarreaud53e2422012-04-16 17:21:11 +02008507 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008508}
8509
Cyril Bonté69fa9922012-10-25 00:01:06 +02008510/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8511 * and an optional one of type int to designate a specific occurrence.
8512 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008513 */
8514static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008515smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008516 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008517{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008518 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008519
Willy Tarreau185b5c42012-04-26 15:11:51 +02008520 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008521 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8522 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008523 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008524 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01008525 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02008526 if (smp->data.str.len < temp->size - 1) {
8527 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8528 temp->str[smp->data.str.len] = '\0';
8529 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8530 smp->type = SMP_T_IPV6;
8531 break;
8532 }
8533 }
8534 }
8535
Willy Tarreaud53e2422012-04-16 17:21:11 +02008536 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008537 if (!(smp->flags & SMP_F_NOT_LAST))
8538 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008539 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008540 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008541}
8542
Willy Tarreau737b0c12007-06-10 21:28:46 +02008543/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8544 * the first '/' after the possible hostname, and ends before the possible '?'.
8545 */
8546static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008547smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008548 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008549{
8550 struct http_txn *txn = l7;
8551 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008552
Willy Tarreauc0239e02012-04-16 14:42:55 +02008553 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008554
Willy Tarreau9b28e032012-10-12 23:49:43 +02008555 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008556 ptr = http_get_path(txn);
8557 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008558 return 0;
8559
8560 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008561 smp->type = SMP_T_CSTR;
8562 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008563
8564 while (ptr < end && *ptr != '?')
8565 ptr++;
8566
Willy Tarreauf853c462012-04-23 18:53:56 +02008567 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008568 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008569 return 1;
8570}
8571
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008572/* This produces a concatenation of the first occurrence of the Host header
8573 * followed by the path component if it begins with a slash ('/'). This means
8574 * that '*' will not be added, resulting in exactly the first Host entry.
8575 * If no Host header is found, then the path is returned as-is. The returned
8576 * value is stored in the trash so it does not need to be marked constant.
8577 */
8578static int
8579smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8580 const struct arg *args, struct sample *smp)
8581{
8582 struct http_txn *txn = l7;
8583 char *ptr, *end, *beg;
8584 struct hdr_ctx ctx;
8585
8586 CHECK_HTTP_MESSAGE_FIRST();
8587
8588 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008589 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 +02008590 !ctx.vlen)
8591 return smp_fetch_path(px, l4, l7, opt, args, smp);
8592
8593 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008594 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008595 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008596 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008597 smp->data.str.len = ctx.vlen;
8598
8599 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008600 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 +02008601 beg = http_get_path(txn);
8602 if (!beg)
8603 beg = end;
8604
8605 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8606
8607 if (beg < ptr && *beg == '/') {
8608 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8609 smp->data.str.len += ptr - beg;
8610 }
8611
8612 smp->flags = SMP_F_VOL_1ST;
8613 return 1;
8614}
8615
Willy Tarreauab1f7b72012-12-09 13:38:54 +01008616/* This produces a 32-bit hash of the concatenation of the first occurrence of
8617 * the Host header followed by the path component if it begins with a slash ('/').
8618 * This means that '*' will not be added, resulting in exactly the first Host
8619 * entry. If no Host header is found, then the path is used. The resulting value
8620 * is hashed using the url hash followed by a full avalanche hash and provides a
8621 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
8622 * high-traffic sites without having to store whole paths.
8623 */
8624static int
8625smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8626 const struct arg *args, struct sample *smp)
8627{
8628 struct http_txn *txn = l7;
8629 struct hdr_ctx ctx;
8630 unsigned int hash = 0;
8631 char *ptr, *beg, *end;
8632 int len;
8633
8634 CHECK_HTTP_MESSAGE_FIRST();
8635
8636 ctx.idx = 0;
8637 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
8638 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8639 ptr = ctx.line + ctx.val;
8640 len = ctx.vlen;
8641 while (len--)
8642 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
8643 }
8644
8645 /* now retrieve the path */
8646 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
8647 beg = http_get_path(txn);
8648 if (!beg)
8649 beg = end;
8650
8651 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8652
8653 if (beg < ptr && *beg == '/') {
8654 while (beg < ptr)
8655 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
8656 }
8657 hash = full_hash(hash);
8658
8659 smp->type = SMP_T_UINT;
8660 smp->data.uint = hash;
8661 smp->flags = SMP_F_VOL_1ST;
8662 return 1;
8663}
8664
Willy Tarreau4a550602012-12-09 14:53:32 +01008665/* This concatenates the source address with the 32-bit hash of the Host and
8666 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
8667 * per-url counters. The result is a binary block from 8 to 20 bytes depending
8668 * on the source address length. The URL hash is stored before the address so
8669 * that in environments where IPv6 is insignificant, truncating the output to
8670 * 8 bytes would still work.
8671 */
8672static int
8673smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8674 const struct arg *args, struct sample *smp)
8675{
Willy Tarreau47ca5452012-12-23 20:22:19 +01008676 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01008677
8678 if (!smp_fetch_base32(px, l4, l7, opt, args, smp))
8679 return 0;
8680
Willy Tarreau47ca5452012-12-23 20:22:19 +01008681 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01008682 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
8683 temp->len += sizeof(smp->data.uint);
8684
8685 switch (l4->si[0].conn->addr.from.ss_family) {
8686 case AF_INET:
8687 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
8688 temp->len += 4;
8689 break;
8690 case AF_INET6:
8691 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
8692 temp->len += 16;
8693 break;
8694 default:
8695 return 0;
8696 }
8697
8698 smp->data.str = *temp;
8699 smp->type = SMP_T_BIN;
8700 return 1;
8701}
8702
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008703static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008704acl_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008705 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008706{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008707 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8708 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8709 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008710
Willy Tarreau24e32d82012-04-23 23:55:44 +02008711 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008712
Willy Tarreauf853c462012-04-23 18:53:56 +02008713 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008714 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02008715 return 1;
8716}
8717
Willy Tarreau7f18e522010-10-22 20:04:13 +02008718/* return a valid test if the current request is the first one on the connection */
8719static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008720acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008721 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02008722{
8723 if (!s)
8724 return 0;
8725
Willy Tarreauf853c462012-04-23 18:53:56 +02008726 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02008727 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02008728 return 1;
8729}
8730
Willy Tarreau34db1082012-04-19 17:16:54 +02008731/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008732static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008733acl_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008734 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008735{
8736
Willy Tarreau24e32d82012-04-23 23:55:44 +02008737 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008738 return 0;
8739
Willy Tarreauc0239e02012-04-16 14:42:55 +02008740 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008741
Willy Tarreauc0239e02012-04-16 14:42:55 +02008742 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008743 return 0;
8744
Willy Tarreauf853c462012-04-23 18:53:56 +02008745 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02008746 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 +01008747 return 1;
8748}
Willy Tarreau8797c062007-05-07 00:55:35 +02008749
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02008750/* Accepts exactly 1 argument of type userlist */
8751static int
8752acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8753 const struct arg *args, struct sample *smp)
8754{
8755
8756 if (!args || args->type != ARGT_USR)
8757 return 0;
8758
8759 CHECK_HTTP_MESSAGE_FIRST();
8760
8761 if (!get_http_auth(l4))
8762 return 0;
8763
8764 /* acl_match_auth() will need several information at once */
8765 smp->ctx.a[0] = args->data.usr; /* user list */
8766 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
8767 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
8768
8769 /* if the user does not belong to the userlist or has a wrong password,
8770 * report that it unconditionally does not match. Otherwise we return
8771 * a non-zero integer which will be ignored anyway since all the params
8772 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
8773 */
8774 smp->type = SMP_T_BOOL;
8775 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
8776 if (smp->data.uint)
8777 smp->type = SMP_T_UINT;
8778
8779 return 1;
8780}
8781
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008782/* Try to find the next occurrence of a cookie name in a cookie header value.
8783 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
8784 * the cookie value is returned into *value and *value_l, and the function
8785 * returns a pointer to the next pointer to search from if the value was found.
8786 * Otherwise if the cookie was not found, NULL is returned and neither value
8787 * nor value_l are touched. The input <hdr> string should first point to the
8788 * header's value, and the <hdr_end> pointer must point to the first character
8789 * not part of the value. <list> must be non-zero if value may represent a list
8790 * of values (cookie headers). This makes it faster to abort parsing when no
8791 * list is expected.
8792 */
8793static char *
8794extract_cookie_value(char *hdr, const char *hdr_end,
8795 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02008796 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008797{
8798 char *equal, *att_end, *att_beg, *val_beg, *val_end;
8799 char *next;
8800
8801 /* we search at least a cookie name followed by an equal, and more
8802 * generally something like this :
8803 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
8804 */
8805 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
8806 /* Iterate through all cookies on this line */
8807
8808 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
8809 att_beg++;
8810
8811 /* find att_end : this is the first character after the last non
8812 * space before the equal. It may be equal to hdr_end.
8813 */
8814 equal = att_end = att_beg;
8815
8816 while (equal < hdr_end) {
8817 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
8818 break;
8819 if (http_is_spht[(unsigned char)*equal++])
8820 continue;
8821 att_end = equal;
8822 }
8823
8824 /* here, <equal> points to '=', a delimitor or the end. <att_end>
8825 * is between <att_beg> and <equal>, both may be identical.
8826 */
8827
8828 /* look for end of cookie if there is an equal sign */
8829 if (equal < hdr_end && *equal == '=') {
8830 /* look for the beginning of the value */
8831 val_beg = equal + 1;
8832 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
8833 val_beg++;
8834
8835 /* find the end of the value, respecting quotes */
8836 next = find_cookie_value_end(val_beg, hdr_end);
8837
8838 /* make val_end point to the first white space or delimitor after the value */
8839 val_end = next;
8840 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
8841 val_end--;
8842 } else {
8843 val_beg = val_end = next = equal;
8844 }
8845
8846 /* We have nothing to do with attributes beginning with '$'. However,
8847 * they will automatically be removed if a header before them is removed,
8848 * since they're supposed to be linked together.
8849 */
8850 if (*att_beg == '$')
8851 continue;
8852
8853 /* Ignore cookies with no equal sign */
8854 if (equal == next)
8855 continue;
8856
8857 /* Now we have the cookie name between att_beg and att_end, and
8858 * its value between val_beg and val_end.
8859 */
8860
8861 if (att_end - att_beg == cookie_name_l &&
8862 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
8863 /* let's return this value and indicate where to go on from */
8864 *value = val_beg;
8865 *value_l = val_end - val_beg;
8866 return next + 1;
8867 }
8868
8869 /* Set-Cookie headers only have the name in the first attr=value part */
8870 if (!list)
8871 break;
8872 }
8873
8874 return NULL;
8875}
8876
Willy Tarreaue333ec92012-04-16 16:26:40 +02008877/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02008878 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
8879 * end-of-header-value, and smp->ctx.a[2] for the hdr_idx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02008880 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02008881 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02008882 * Accepts exactly 1 argument of type string. If the input options indicate
8883 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008884 */
8885static int
Willy Tarreau51539362012-05-08 12:46:28 +02008886smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8887 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008888{
8889 struct http_txn *txn = l7;
8890 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreau37406352012-04-23 16:16:37 +02008891 struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02008892 const struct http_msg *msg;
8893 const char *hdr_name;
8894 int hdr_name_len;
8895 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02008896 int occ = 0;
8897 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008898
Willy Tarreau24e32d82012-04-23 23:55:44 +02008899 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008900 return 0;
8901
Willy Tarreaue333ec92012-04-16 16:26:40 +02008902 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008903
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008904 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008905 msg = &txn->req;
8906 hdr_name = "Cookie";
8907 hdr_name_len = 6;
8908 } else {
8909 msg = &txn->rsp;
8910 hdr_name = "Set-Cookie";
8911 hdr_name_len = 10;
8912 }
8913
Willy Tarreau28376d62012-04-26 21:26:10 +02008914 if (!occ && !(opt & SMP_OPT_ITERATE))
8915 /* no explicit occurrence and single fetch => last cookie by default */
8916 occ = -1;
8917
8918 /* OK so basically here, either we want only one value and it's the
8919 * last one, or we want to iterate over all of them and we fetch the
8920 * next one.
8921 */
8922
Willy Tarreau9b28e032012-10-12 23:49:43 +02008923 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02008924 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008925 /* search for the header from the beginning, we must first initialize
8926 * the search parameters.
8927 */
Willy Tarreau37406352012-04-23 16:16:37 +02008928 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008929 ctx->idx = 0;
8930 }
8931
Willy Tarreau28376d62012-04-26 21:26:10 +02008932 smp->flags |= SMP_F_VOL_HDR;
8933
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008934 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02008935 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
8936 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008937 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
8938 goto out;
8939
Willy Tarreau24e32d82012-04-23 23:55:44 +02008940 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008941 continue;
8942
Willy Tarreau37406352012-04-23 16:16:37 +02008943 smp->ctx.a[0] = ctx->line + ctx->val;
8944 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008945 }
8946
Willy Tarreauf853c462012-04-23 18:53:56 +02008947 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02008948 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02008949 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008950 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02008951 &smp->data.str.str,
8952 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02008953 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02008954 found = 1;
8955 if (occ >= 0) {
8956 /* one value was returned into smp->data.str.{str,len} */
8957 smp->flags |= SMP_F_NOT_LAST;
8958 return 1;
8959 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008960 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008961 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008962 }
Willy Tarreau28376d62012-04-26 21:26:10 +02008963 /* all cookie headers and values were scanned. If we're looking for the
8964 * last occurrence, we may return it now.
8965 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008966 out:
Willy Tarreau37406352012-04-23 16:16:37 +02008967 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02008968 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008969}
8970
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008971/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02008972 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008973 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02008974 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008975 */
8976static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008977acl_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008978 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008979{
8980 struct http_txn *txn = l7;
8981 struct hdr_idx *idx = &txn->hdr_idx;
8982 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008983 const struct http_msg *msg;
8984 const char *hdr_name;
8985 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008986 int cnt;
8987 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008988 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02008989
Willy Tarreau24e32d82012-04-23 23:55:44 +02008990 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008991 return 0;
8992
Willy Tarreaue333ec92012-04-16 16:26:40 +02008993 CHECK_HTTP_MESSAGE_FIRST();
8994
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008995 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02008996 msg = &txn->req;
8997 hdr_name = "Cookie";
8998 hdr_name_len = 6;
8999 } else {
9000 msg = &txn->rsp;
9001 hdr_name = "Set-Cookie";
9002 hdr_name_len = 10;
9003 }
9004
Willy Tarreau9b28e032012-10-12 23:49:43 +02009005 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009006 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009007 ctx.idx = 0;
9008 cnt = 0;
9009
9010 while (1) {
9011 /* Note: val_beg == NULL every time we need to fetch a new header */
9012 if (!val_beg) {
9013 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9014 break;
9015
Willy Tarreau24e32d82012-04-23 23:55:44 +02009016 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009017 continue;
9018
9019 val_beg = ctx.line + ctx.val;
9020 val_end = val_beg + ctx.vlen;
9021 }
9022
Willy Tarreauf853c462012-04-23 18:53:56 +02009023 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009024 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009025 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009026 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009027 &smp->data.str.str,
9028 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009029 cnt++;
9030 }
9031 }
9032
Willy Tarreauf853c462012-04-23 18:53:56 +02009033 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009034 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009035 return 1;
9036}
9037
Willy Tarreau51539362012-05-08 12:46:28 +02009038/* Fetch an cookie's integer value. The integer value is returned. It
9039 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9040 */
9041static int
9042smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9043 const struct arg *args, struct sample *smp)
9044{
9045 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
9046
9047 if (ret > 0) {
9048 smp->type = SMP_T_UINT;
9049 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9050 }
9051
9052 return ret;
9053}
9054
Willy Tarreau8797c062007-05-07 00:55:35 +02009055/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009056/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009057/************************************************************************/
9058
David Cournapeau16023ee2010-12-23 20:55:41 +09009059/*
9060 * Given a path string and its length, find the position of beginning of the
9061 * query string. Returns NULL if no query string is found in the path.
9062 *
9063 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9064 *
9065 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9066 */
bedis4c75cca2012-10-05 08:38:24 +02009067static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009068{
9069 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009070
bedis4c75cca2012-10-05 08:38:24 +02009071 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009072 return p ? p + 1 : NULL;
9073}
9074
bedis4c75cca2012-10-05 08:38:24 +02009075static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009076{
bedis4c75cca2012-10-05 08:38:24 +02009077 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009078}
9079
9080/*
9081 * Given a url parameter, find the starting position of the first occurence,
9082 * or NULL if the parameter is not found.
9083 *
9084 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9085 * the function will return query_string+8.
9086 */
9087static char*
9088find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009089 char* url_param_name, size_t url_param_name_l,
9090 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009091{
9092 char *pos, *last;
9093
9094 pos = query_string;
9095 last = query_string + query_string_l - url_param_name_l - 1;
9096
9097 while (pos <= last) {
9098 if (pos[url_param_name_l] == '=') {
9099 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9100 return pos;
9101 pos += url_param_name_l + 1;
9102 }
bedis4c75cca2012-10-05 08:38:24 +02009103 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009104 pos++;
9105 pos++;
9106 }
9107 return NULL;
9108}
9109
9110/*
9111 * Given a url parameter name, returns its value and size into *value and
9112 * *value_l respectively, and returns non-zero. If the parameter is not found,
9113 * zero is returned and value/value_l are not touched.
9114 */
9115static int
9116find_url_param_value(char* path, size_t path_l,
9117 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009118 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009119{
9120 char *query_string, *qs_end;
9121 char *arg_start;
9122 char *value_start, *value_end;
9123
bedis4c75cca2012-10-05 08:38:24 +02009124 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009125 if (!query_string)
9126 return 0;
9127
9128 qs_end = path + path_l;
9129 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009130 url_param_name, url_param_name_l,
9131 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009132 if (!arg_start)
9133 return 0;
9134
9135 value_start = arg_start + url_param_name_l + 1;
9136 value_end = value_start;
9137
bedis4c75cca2012-10-05 08:38:24 +02009138 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009139 value_end++;
9140
9141 *value = value_start;
9142 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009143 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009144}
9145
9146static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009147smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9148 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09009149{
bedis4c75cca2012-10-05 08:38:24 +02009150 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009151 struct http_txn *txn = l7;
9152 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009153
bedis4c75cca2012-10-05 08:38:24 +02009154 if (!args || args[0].type != ARGT_STR ||
9155 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009156 return 0;
9157
9158 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009159
bedis4c75cca2012-10-05 08:38:24 +02009160 if (args[1].type)
9161 delim = *args[1].data.str.str;
9162
Willy Tarreau9b28e032012-10-12 23:49:43 +02009163 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009164 args->data.str.str, args->data.str.len,
9165 &smp->data.str.str, &smp->data.str.len,
9166 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009167 return 0;
9168
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009169 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009170 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009171 return 1;
9172}
9173
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009174/* Return the signed integer value for the specified url parameter (see url_param
9175 * above).
9176 */
9177static int
9178smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9179 const struct arg *args, struct sample *smp)
9180{
9181 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
9182
9183 if (ret > 0) {
9184 smp->type = SMP_T_UINT;
9185 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9186 }
9187
9188 return ret;
9189}
9190
Willy Tarreau185b5c42012-04-26 15:11:51 +02009191/* This function is used to validate the arguments passed to any "hdr" fetch
9192 * keyword. These keywords support an optional positive or negative occurrence
9193 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9194 * is assumed that the types are already the correct ones. Returns 0 on error,
9195 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9196 * error message in case of error, that the caller is responsible for freeing.
9197 * The initial location must either be freeable or NULL.
9198 */
9199static int val_hdr(struct arg *arg, char **err_msg)
9200{
9201 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009202 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009203 return 0;
9204 }
9205 return 1;
9206}
9207
Willy Tarreau4a568972010-05-12 08:08:50 +02009208/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009209/* All supported ACL keywords must be declared here. */
9210/************************************************************************/
9211
9212/* Note: must not be declared <const> as its list will be overwritten.
9213 * Please take care of keeping this list alphabetically sorted.
9214 */
9215static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009216 { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9217 { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9218 { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9219 { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9220 { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9221 { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9222 { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9223 { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
9224
Willy Tarreau51539362012-05-08 12:46:28 +02009225 { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
9226 { "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 +02009227 { "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 +02009228 { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9229 { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9230 { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9231 { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9232 { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9233 { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9234 { "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 +02009235
Willy Tarreau185b5c42012-04-26 15:11:51 +02009236 { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9237 { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9238 { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) },
9239 { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9240 { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9241 { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9242 { "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 },
9243 { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9244 { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9245 { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9246 { "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 +02009247
9248 { "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 +02009249 { "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 +02009250 { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9251
9252 { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 },
9253
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009254 { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9255 { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9256 { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9257 { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9258 { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9259 { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9260 { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9261 { "path_sub", acl_parse_str, smp_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009262
9263 { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 },
9264 { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9265 { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 },
9266
Willy Tarreau51539362012-05-08 12:46:28 +02009267 { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) },
9268 { "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 +02009269 { "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 +02009270 { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9271 { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9272 { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9273 { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9274 { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9275 { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9276 { "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 +02009277
Willy Tarreau185b5c42012-04-26 15:11:51 +02009278 { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr },
9279 { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9280 { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) },
9281 { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9282 { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9283 { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9284 { "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 },
9285 { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9286 { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9287 { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr },
9288 { "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 +02009289
9290 { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 },
9291
Willy Tarreau6812bcf2012-04-29 09:28:50 +02009292 { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9293 { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 },
9294 { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 },
9295 { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 },
9296 { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 },
9297 { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 },
9298 { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 },
9299 { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 },
9300 { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 },
9301 { "url_sub", acl_parse_str, smp_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009302
9303 { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9304 { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9305 { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9306 { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9307 { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9308 { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) },
9309 { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9310 { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) },
9311 { "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 +02009312 { "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 +02009313
9314 { NULL, NULL, NULL, NULL },
9315}};
9316
9317/************************************************************************/
9318/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009319/************************************************************************/
9320/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009321static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009322 { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9323 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreauab1f7b72012-12-09 13:38:54 +01009324 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreau4a550602012-12-09 14:53:32 +01009325 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_CAP_L7|SMP_CAP_REQ },
Willy Tarreau1b6c00c2012-10-05 22:41:26 +02009326 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9327 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9328 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ },
9329 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ },
9330 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ },
9331 { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES },
9332 { "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 +02009333 { NULL, NULL, 0, 0, 0 },
Willy Tarreau4a568972010-05-12 08:08:50 +02009334}};
9335
Willy Tarreau8797c062007-05-07 00:55:35 +02009336
9337__attribute__((constructor))
9338static void __http_protocol_init(void)
9339{
9340 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009341 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009342}
9343
9344
Willy Tarreau58f10d72006-12-04 02:26:12 +01009345/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009346 * Local variables:
9347 * c-indent-level: 8
9348 * c-basic-offset: 8
9349 * End:
9350 */