blob: d4e93e592a7b7bd5548e9320340515213bb3daa2 [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 Tarreaubc5aa192010-01-03 15:09:36 +010078 "Content-length: 0\r\n"
Willy Tarreaub463dfb2008-06-07 23:08:56 +020079 "Location: "; /* not terminated since it will be concatenated with the URL */
80
Willy Tarreau0f772532006-12-23 20:51:41 +010081const char *HTTP_302 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010082 "HTTP/1.1 302 Found\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010083 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010084 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010085 "Location: "; /* not terminated since it will be concatenated with the URL */
86
87/* same as 302 except that the browser MUST retry with the GET method */
88const char *HTTP_303 =
Willy Tarreaubc5aa192010-01-03 15:09:36 +010089 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010090 "Cache-Control: no-cache\r\n"
Willy Tarreaubc5aa192010-01-03 15:09:36 +010091 "Content-length: 0\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +010092 "Location: "; /* not terminated since it will be concatenated with the URL */
93
Yves Lafon3e8d1ae2013-03-11 11:06:05 -040094
95/* same as 302 except that the browser MUST retry with the same method */
96const char *HTTP_307 =
97 "HTTP/1.1 307 Temporary Redirect\r\n"
98 "Cache-Control: no-cache\r\n"
99 "Content-length: 0\r\n"
100 "Location: "; /* not terminated since it will be concatenated with the URL */
101
102/* same as 301 except that the browser MUST retry with the same method */
103const char *HTTP_308 =
104 "HTTP/1.1 308 Permanent Redirect\r\n"
105 "Content-length: 0\r\n"
106 "Location: "; /* not terminated since it will be concatenated with the URL */
107
Willy Tarreaubaaee002006-06-26 02:48:02 +0200108/* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */
109const char *HTTP_401_fmt =
110 "HTTP/1.0 401 Unauthorized\r\n"
111 "Cache-Control: no-cache\r\n"
112 "Connection: close\r\n"
Willy Tarreau791d66d2006-07-08 16:53:38 +0200113 "Content-Type: text/html\r\n"
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
115 "\r\n"
116 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
117
Willy Tarreau844a7e72010-01-31 21:46:18 +0100118const char *HTTP_407_fmt =
119 "HTTP/1.0 407 Unauthorized\r\n"
120 "Cache-Control: no-cache\r\n"
121 "Connection: close\r\n"
122 "Content-Type: text/html\r\n"
123 "Proxy-Authenticate: Basic realm=\"%s\"\r\n"
124 "\r\n"
125 "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n";
126
Willy Tarreau0f772532006-12-23 20:51:41 +0100127
128const int http_err_codes[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200129 [HTTP_ERR_200] = 200, /* used by "monitor-uri" */
Willy Tarreau0f772532006-12-23 20:51:41 +0100130 [HTTP_ERR_400] = 400,
131 [HTTP_ERR_403] = 403,
132 [HTTP_ERR_408] = 408,
133 [HTTP_ERR_500] = 500,
134 [HTTP_ERR_502] = 502,
135 [HTTP_ERR_503] = 503,
136 [HTTP_ERR_504] = 504,
137};
138
Willy Tarreau80587432006-12-24 17:47:20 +0100139static const char *http_err_msgs[HTTP_ERR_SIZE] = {
Willy Tarreauae94d4d2011-05-11 16:28:49 +0200140 [HTTP_ERR_200] =
141 "HTTP/1.0 200 OK\r\n"
142 "Cache-Control: no-cache\r\n"
143 "Connection: close\r\n"
144 "Content-Type: text/html\r\n"
145 "\r\n"
146 "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n",
147
Willy Tarreau0f772532006-12-23 20:51:41 +0100148 [HTTP_ERR_400] =
Willy Tarreau80587432006-12-24 17:47:20 +0100149 "HTTP/1.0 400 Bad request\r\n"
Willy Tarreau0f772532006-12-23 20:51:41 +0100150 "Cache-Control: no-cache\r\n"
151 "Connection: close\r\n"
152 "Content-Type: text/html\r\n"
153 "\r\n"
154 "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n",
155
156 [HTTP_ERR_403] =
157 "HTTP/1.0 403 Forbidden\r\n"
158 "Cache-Control: no-cache\r\n"
159 "Connection: close\r\n"
160 "Content-Type: text/html\r\n"
161 "\r\n"
162 "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n",
163
164 [HTTP_ERR_408] =
165 "HTTP/1.0 408 Request Time-out\r\n"
166 "Cache-Control: no-cache\r\n"
167 "Connection: close\r\n"
168 "Content-Type: text/html\r\n"
169 "\r\n"
170 "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n",
171
172 [HTTP_ERR_500] =
173 "HTTP/1.0 500 Server Error\r\n"
174 "Cache-Control: no-cache\r\n"
175 "Connection: close\r\n"
176 "Content-Type: text/html\r\n"
177 "\r\n"
178 "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n",
179
180 [HTTP_ERR_502] =
181 "HTTP/1.0 502 Bad Gateway\r\n"
182 "Cache-Control: no-cache\r\n"
183 "Connection: close\r\n"
184 "Content-Type: text/html\r\n"
185 "\r\n"
186 "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n",
187
188 [HTTP_ERR_503] =
189 "HTTP/1.0 503 Service Unavailable\r\n"
190 "Cache-Control: no-cache\r\n"
191 "Connection: close\r\n"
192 "Content-Type: text/html\r\n"
193 "\r\n"
194 "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n",
195
196 [HTTP_ERR_504] =
197 "HTTP/1.0 504 Gateway Time-out\r\n"
198 "Cache-Control: no-cache\r\n"
199 "Connection: close\r\n"
200 "Content-Type: text/html\r\n"
201 "\r\n"
202 "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n",
203
204};
205
Cyril Bonté19979e12012-04-04 12:57:21 +0200206/* status codes available for the stats admin page (strictly 4 chars length) */
207const char *stat_status_codes[STAT_STATUS_SIZE] = {
208 [STAT_STATUS_DENY] = "DENY",
209 [STAT_STATUS_DONE] = "DONE",
210 [STAT_STATUS_ERRP] = "ERRP",
211 [STAT_STATUS_EXCD] = "EXCD",
212 [STAT_STATUS_NONE] = "NONE",
213 [STAT_STATUS_PART] = "PART",
214 [STAT_STATUS_UNKN] = "UNKN",
215};
216
217
Willy Tarreau80587432006-12-24 17:47:20 +0100218/* We must put the messages here since GCC cannot initialize consts depending
219 * on strlen().
220 */
221struct chunk http_err_chunks[HTTP_ERR_SIZE];
222
Willy Tarreaua890d072013-04-02 12:01:06 +0200223/* this struct is used between calls to smp_fetch_hdr() or smp_fetch_cookie() */
224static struct hdr_ctx static_hdr_ctx;
225
Willy Tarreau42250582007-04-01 01:30:43 +0200226#define FD_SETS_ARE_BITFIELDS
227#ifdef FD_SETS_ARE_BITFIELDS
228/*
229 * This map is used with all the FD_* macros to check whether a particular bit
230 * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes
231 * which should be encoded. When FD_ISSET() returns non-zero, it means that the
232 * byte should be encoded. Be careful to always pass bytes from 0 to 255
233 * exclusively to the macros.
234 */
235fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
236fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))];
237
238#else
239#error "Check if your OS uses bitfields for fd_sets"
240#endif
241
Willy Tarreau80587432006-12-24 17:47:20 +0100242void init_proto_http()
243{
Willy Tarreau42250582007-04-01 01:30:43 +0200244 int i;
245 char *tmp;
Willy Tarreau80587432006-12-24 17:47:20 +0100246 int msg;
Willy Tarreau42250582007-04-01 01:30:43 +0200247
Willy Tarreau80587432006-12-24 17:47:20 +0100248 for (msg = 0; msg < HTTP_ERR_SIZE; msg++) {
249 if (!http_err_msgs[msg]) {
250 Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg);
251 abort();
252 }
253
254 http_err_chunks[msg].str = (char *)http_err_msgs[msg];
255 http_err_chunks[msg].len = strlen(http_err_msgs[msg]);
256 }
Willy Tarreau42250582007-04-01 01:30:43 +0200257
258 /* initialize the log header encoding map : '{|}"#' should be encoded with
259 * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ).
260 * URL encoding only requires '"', '#' to be encoded as well as non-
261 * printable characters above.
262 */
263 memset(hdr_encode_map, 0, sizeof(hdr_encode_map));
264 memset(url_encode_map, 0, sizeof(url_encode_map));
265 for (i = 0; i < 32; i++) {
266 FD_SET(i, hdr_encode_map);
267 FD_SET(i, url_encode_map);
268 }
269 for (i = 127; i < 256; i++) {
270 FD_SET(i, hdr_encode_map);
271 FD_SET(i, url_encode_map);
272 }
273
274 tmp = "\"#{|}";
275 while (*tmp) {
276 FD_SET(*tmp, hdr_encode_map);
277 tmp++;
278 }
279
280 tmp = "\"#";
281 while (*tmp) {
282 FD_SET(*tmp, url_encode_map);
283 tmp++;
284 }
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200285
286 /* memory allocations */
287 pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED);
William Lallemanda73203e2012-03-12 12:48:57 +0100288 pool2_uniqueid = create_pool("uniqueid", UNIQUEID_LEN, MEM_F_SHARED);
Willy Tarreau80587432006-12-24 17:47:20 +0100289}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200290
Willy Tarreau53b6c742006-12-17 13:37:46 +0100291/*
292 * We have 26 list of methods (1 per first letter), each of which can have
293 * up to 3 entries (2 valid, 1 null).
294 */
295struct http_method_desc {
296 http_meth_t meth;
297 int len;
298 const char text[8];
299};
300
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100301const struct http_method_desc http_methods[26][3] = {
Willy Tarreau53b6c742006-12-17 13:37:46 +0100302 ['C' - 'A'] = {
303 [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" },
304 },
305 ['D' - 'A'] = {
306 [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" },
307 },
308 ['G' - 'A'] = {
309 [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" },
310 },
311 ['H' - 'A'] = {
312 [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" },
313 },
314 ['P' - 'A'] = {
315 [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" },
316 [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" },
317 },
318 ['T' - 'A'] = {
319 [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" },
320 },
321 /* rest is empty like this :
322 * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" },
323 */
324};
325
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100326/* It is about twice as fast on recent architectures to lookup a byte in a
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200327 * table than to perform a boolean AND or OR between two tests. Refer to
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100328 * RFC2616 for those chars.
329 */
330
331const char http_is_spht[256] = {
332 [' '] = 1, ['\t'] = 1,
333};
334
335const char http_is_crlf[256] = {
336 ['\r'] = 1, ['\n'] = 1,
337};
338
339const char http_is_lws[256] = {
340 [' '] = 1, ['\t'] = 1,
341 ['\r'] = 1, ['\n'] = 1,
342};
343
344const char http_is_sep[256] = {
345 ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1,
346 ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1,
347 ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1,
348 ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1,
349 [' '] = 1, ['\t'] = 1, ['\\'] = 1,
350};
351
352const char http_is_ctl[256] = {
353 [0 ... 31] = 1,
354 [127] = 1,
355};
356
357/*
358 * A token is any ASCII char that is neither a separator nor a CTL char.
359 * Do not overwrite values in assignment since gcc-2.95 will not handle
360 * them correctly. Instead, define every non-CTL char's status.
361 */
362const char http_is_token[256] = {
363 [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1,
364 ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1,
365 ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1,
366 [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0,
367 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1,
368 ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1,
369 ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0,
370 ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0,
371 ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1,
372 ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1,
373 ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1,
374 ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1,
375 ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1,
376 ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1,
377 ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0,
378 ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1,
379 ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1,
380 ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1,
381 ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1,
382 ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1,
383 ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1,
384 ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1,
385 ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0,
386 ['|'] = 1, ['}'] = 0, ['~'] = 1,
387};
388
389
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100390/*
391 * An http ver_token is any ASCII which can be found in an HTTP version,
392 * which includes 'H', 'T', 'P', '/', '.' and any digit.
393 */
394const char http_is_ver_token[256] = {
395 ['.'] = 1, ['/'] = 1,
396 ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1,
397 ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1,
398 ['H'] = 1, ['P'] = 1, ['T'] = 1,
399};
400
401
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100402/*
Willy Tarreaue988a792010-01-04 21:13:14 +0100403 * Silent debug that outputs only in strace, using fd #-1. Trash is modified.
404 */
405#if defined(DEBUG_FSM)
406static void http_silent_debug(int line, struct session *s)
407{
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100408 chunk_printf(&trash,
409 "[%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",
410 line,
411 s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers,
412 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);
413 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100414
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100415 chunk_printf(&trash,
416 " %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",
417 line,
418 s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers,
419 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);
420 write(-1, trash.str, trash.len);
Willy Tarreaue988a792010-01-04 21:13:14 +0100421}
422#else
423#define http_silent_debug(l,s) do { } while (0)
424#endif
425
426/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100427 * Adds a header and its CRLF at the tail of the message's buffer, just before
428 * the last CRLF. Text length is measured first, so it cannot be NULL.
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100429 * The header is also automatically added to the index <hdr_idx>, and the end
430 * of headers is automatically adjusted. The number of bytes added is returned
431 * on success, otherwise <0 is returned indicating an error.
432 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100433int http_header_add_tail(struct http_msg *msg, struct hdr_idx *hdr_idx, const char *text)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100434{
435 int bytes, len;
436
437 len = strlen(text);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200438 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100439 if (!bytes)
440 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100441 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100442 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
443}
444
445/*
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100446 * Adds a header and its CRLF at the tail of the message's buffer, just before
447 * the last CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100448 * the buffer is only opened and the space reserved, but nothing is copied.
449 * The header is also automatically added to the index <hdr_idx>, and the end
450 * of headers is automatically adjusted. The number of bytes added is returned
451 * on success, otherwise <0 is returned indicating an error.
452 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100453int http_header_add_tail2(struct http_msg *msg,
454 struct hdr_idx *hdr_idx, const char *text, int len)
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100455{
456 int bytes;
457
Willy Tarreau9b28e032012-10-12 23:49:43 +0200458 bytes = buffer_insert_line2(msg->chn->buf, msg->chn->buf->p + msg->eoh, text, len);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100459 if (!bytes)
460 return -1;
Willy Tarreaufa355d42009-11-29 18:12:29 +0100461 http_msg_move_end(msg, bytes);
Willy Tarreau4af6f3a2007-03-18 22:36:26 +0100462 return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail);
463}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464
465/*
Willy Tarreauaa9dce32007-03-18 23:50:16 +0100466 * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon.
467 * If so, returns the position of the first non-space character relative to
468 * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries
469 * to return a pointer to the place after the first space. Returns 0 if the
470 * header name does not match. Checks are case-insensitive.
471 */
472int http_header_match2(const char *hdr, const char *end,
473 const char *name, int len)
474{
475 const char *val;
476
477 if (hdr + len >= end)
478 return 0;
479 if (hdr[len] != ':')
480 return 0;
481 if (strncasecmp(hdr, name, len) != 0)
482 return 0;
483 val = hdr + len + 1;
484 while (val < end && HTTP_IS_SPHT(*val))
485 val++;
486 if ((val >= end) && (len + 2 <= end - hdr))
487 return len + 2; /* we may replace starting from second space */
488 return val - hdr;
489}
490
Willy Tarreau68085d82010-01-18 14:54:04 +0100491/* Find the end of the header value contained between <s> and <e>. See RFC2616,
492 * par 2.2 for more information. Note that it requires a valid header to return
493 * a valid result. This works for headers defined as comma-separated lists.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200494 */
Willy Tarreau68085d82010-01-18 14:54:04 +0100495char *find_hdr_value_end(char *s, const char *e)
Willy Tarreau33a7e692007-06-10 19:45:56 +0200496{
497 int quoted, qdpair;
498
499 quoted = qdpair = 0;
500 for (; s < e; s++) {
501 if (qdpair) qdpair = 0;
Willy Tarreau0f7f51f2010-08-30 11:06:34 +0200502 else if (quoted) {
503 if (*s == '\\') qdpair = 1;
504 else if (*s == '"') quoted = 0;
505 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200506 else if (*s == '"') quoted = 1;
507 else if (*s == ',') return s;
508 }
509 return s;
510}
511
512/* Find the first or next occurrence of header <name> in message buffer <sol>
513 * using headers index <idx>, and return it in the <ctx> structure. This
514 * structure holds everything necessary to use the header and find next
515 * occurrence. If its <idx> member is 0, the header is searched from the
516 * beginning. Otherwise, the next occurrence is returned. The function returns
Willy Tarreau68085d82010-01-18 14:54:04 +0100517 * 1 when it finds a value, and 0 when there is no more. It is designed to work
518 * with headers defined as comma-separated lists. As a special case, if ctx->val
519 * is NULL when searching for a new values of a header, the current header is
520 * rescanned. This allows rescanning after a header deletion.
Willy Tarreau33a7e692007-06-10 19:45:56 +0200521 */
522int http_find_header2(const char *name, int len,
Willy Tarreau68085d82010-01-18 14:54:04 +0100523 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200524 struct hdr_ctx *ctx)
525{
Willy Tarreau68085d82010-01-18 14:54:04 +0100526 char *eol, *sov;
527 int cur_idx, old_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200528
Willy Tarreau68085d82010-01-18 14:54:04 +0100529 cur_idx = ctx->idx;
530 if (cur_idx) {
Willy Tarreau33a7e692007-06-10 19:45:56 +0200531 /* We have previously returned a value, let's search
532 * another one on the same line.
533 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200534 sol = ctx->line;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200535 ctx->del = ctx->val + ctx->vlen + ctx->tws;
Willy Tarreau68085d82010-01-18 14:54:04 +0100536 sov = sol + ctx->del;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200537 eol = sol + idx->v[cur_idx].len;
538
539 if (sov >= eol)
540 /* no more values in this header */
541 goto next_hdr;
542
Willy Tarreau68085d82010-01-18 14:54:04 +0100543 /* values remaining for this header, skip the comma but save it
544 * for later use (eg: for header deletion).
545 */
Willy Tarreau33a7e692007-06-10 19:45:56 +0200546 sov++;
547 while (sov < eol && http_is_lws[(unsigned char)*sov])
548 sov++;
549
550 goto return_hdr;
551 }
552
553 /* first request for this header */
554 sol += hdr_idx_first_pos(idx);
Willy Tarreau68085d82010-01-18 14:54:04 +0100555 old_idx = 0;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200556 cur_idx = hdr_idx_first_idx(idx);
Willy Tarreau33a7e692007-06-10 19:45:56 +0200557 while (cur_idx) {
558 eol = sol + idx->v[cur_idx].len;
559
Willy Tarreau1ad7c6d2007-06-10 21:42:55 +0200560 if (len == 0) {
561 /* No argument was passed, we want any header.
562 * To achieve this, we simply build a fake request. */
563 while (sol + len < eol && sol[len] != ':')
564 len++;
565 name = sol;
566 }
567
Willy Tarreau33a7e692007-06-10 19:45:56 +0200568 if ((len < eol - sol) &&
569 (sol[len] == ':') &&
570 (strncasecmp(sol, name, len) == 0)) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100571 ctx->del = len;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200572 sov = sol + len + 1;
573 while (sov < eol && http_is_lws[(unsigned char)*sov])
574 sov++;
Willy Tarreau68085d82010-01-18 14:54:04 +0100575
Willy Tarreau33a7e692007-06-10 19:45:56 +0200576 ctx->line = sol;
Willy Tarreau68085d82010-01-18 14:54:04 +0100577 ctx->prev = old_idx;
578 return_hdr:
Willy Tarreau33a7e692007-06-10 19:45:56 +0200579 ctx->idx = cur_idx;
580 ctx->val = sov - sol;
581
582 eol = find_hdr_value_end(sov, eol);
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200583 ctx->tws = 0;
Willy Tarreau275600b2011-09-16 08:11:26 +0200584 while (eol > sov && http_is_lws[(unsigned char)*(eol - 1)]) {
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200585 eol--;
586 ctx->tws++;
587 }
Willy Tarreau33a7e692007-06-10 19:45:56 +0200588 ctx->vlen = eol - sov;
589 return 1;
590 }
591 next_hdr:
592 sol = eol + idx->v[cur_idx].cr + 1;
Willy Tarreau68085d82010-01-18 14:54:04 +0100593 old_idx = cur_idx;
Willy Tarreau33a7e692007-06-10 19:45:56 +0200594 cur_idx = idx->v[cur_idx].next;
595 }
596 return 0;
597}
598
599int http_find_header(const char *name,
Willy Tarreau68085d82010-01-18 14:54:04 +0100600 char *sol, struct hdr_idx *idx,
Willy Tarreau33a7e692007-06-10 19:45:56 +0200601 struct hdr_ctx *ctx)
602{
603 return http_find_header2(name, strlen(name), sol, idx, ctx);
604}
605
Willy Tarreau68085d82010-01-18 14:54:04 +0100606/* Remove one value of a header. This only works on a <ctx> returned by one of
607 * the http_find_header functions. The value is removed, as well as surrounding
608 * commas if any. If the removed value was alone, the whole header is removed.
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100609 * The ctx is always updated accordingly, as well as the buffer and HTTP
Willy Tarreau68085d82010-01-18 14:54:04 +0100610 * message <msg>. The new index is returned. If it is zero, it means there is
611 * no more header, so any processing may stop. The ctx is always left in a form
612 * that can be handled by http_find_header2() to find next occurrence.
613 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +0100614int http_remove_header2(struct http_msg *msg, struct hdr_idx *idx, struct hdr_ctx *ctx)
Willy Tarreau68085d82010-01-18 14:54:04 +0100615{
616 int cur_idx = ctx->idx;
617 char *sol = ctx->line;
618 struct hdr_idx_elem *hdr;
619 int delta, skip_comma;
620
621 if (!cur_idx)
622 return 0;
623
624 hdr = &idx->v[cur_idx];
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200625 if (sol[ctx->del] == ':' && ctx->val + ctx->vlen + ctx->tws == hdr->len) {
Willy Tarreau68085d82010-01-18 14:54:04 +0100626 /* This was the only value of the header, we must now remove it entirely. */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200627 delta = buffer_replace2(msg->chn->buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0);
Willy Tarreau68085d82010-01-18 14:54:04 +0100628 http_msg_move_end(msg, delta);
629 idx->used--;
630 hdr->len = 0; /* unused entry */
631 idx->v[ctx->prev].next = idx->v[ctx->idx].next;
Willy Tarreau5c4784f2011-02-12 13:07:35 +0100632 if (idx->tail == ctx->idx)
633 idx->tail = ctx->prev;
Willy Tarreau68085d82010-01-18 14:54:04 +0100634 ctx->idx = ctx->prev; /* walk back to the end of previous header */
635 ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1;
636 ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */
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
641 /* This was not the only value of this header. We have to remove between
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200642 * ctx->del+1 and ctx->val+ctx->vlen+ctx->tws+1 included. If it is the
643 * last entry of the list, we remove the last separator.
Willy Tarreau68085d82010-01-18 14:54:04 +0100644 */
645
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200646 skip_comma = (ctx->val + ctx->vlen + ctx->tws == hdr->len) ? 0 : 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200647 delta = buffer_replace2(msg->chn->buf, sol + ctx->del + skip_comma,
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200648 sol + ctx->val + ctx->vlen + ctx->tws + skip_comma,
Willy Tarreau68085d82010-01-18 14:54:04 +0100649 NULL, 0);
650 hdr->len += delta;
651 http_msg_move_end(msg, delta);
652 ctx->val = ctx->del;
Willy Tarreau588bd4f2011-09-01 22:22:28 +0200653 ctx->tws = ctx->vlen = 0;
Willy Tarreau68085d82010-01-18 14:54:04 +0100654 return ctx->idx;
655}
656
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100657/* This function handles a server error at the stream interface level. The
658 * stream interface is assumed to be already in a closed state. An optional
659 * message is copied into the input buffer, and an HTTP status code stored.
660 * The error flags are set to the values in arguments. Any pending request
Willy Tarreau6f0aa472009-03-08 20:33:29 +0100661 * in this buffer will be lost.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662 */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100663static void http_server_error(struct session *t, struct stream_interface *si,
664 int err, int finst, int status, const struct chunk *msg)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200665{
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200666 channel_auto_read(si->ob);
667 channel_abort(si->ob);
668 channel_auto_close(si->ob);
669 channel_erase(si->ob);
670 channel_auto_close(si->ib);
671 channel_auto_read(si->ib);
Willy Tarreau0f772532006-12-23 20:51:41 +0100672 if (status > 0 && msg) {
Willy Tarreau3bac9ff2007-03-18 17:31:28 +0100673 t->txn.status = status;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +0200674 bo_inject(si->ib, msg->str, msg->len);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200675 }
676 if (!(t->flags & SN_ERR_MASK))
677 t->flags |= err;
678 if (!(t->flags & SN_FINST_MASK))
679 t->flags |= finst;
680}
681
Willy Tarreau80587432006-12-24 17:47:20 +0100682/* This function returns the appropriate error location for the given session
683 * and message.
684 */
685
Willy Tarreau783f2582012-09-04 12:19:04 +0200686struct chunk *http_error_message(struct session *s, int msgnum)
Willy Tarreau80587432006-12-24 17:47:20 +0100687{
Willy Tarreaue2e27a52007-04-01 00:01:37 +0200688 if (s->be->errmsg[msgnum].str)
689 return &s->be->errmsg[msgnum];
Willy Tarreau80587432006-12-24 17:47:20 +0100690 else if (s->fe->errmsg[msgnum].str)
691 return &s->fe->errmsg[msgnum];
692 else
693 return &http_err_chunks[msgnum];
694}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200695
Willy Tarreau53b6c742006-12-17 13:37:46 +0100696/*
697 * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text
698 * string), HTTP_METH_OTHER for unknown methods, or the identified method.
699 */
700static http_meth_t find_http_meth(const char *str, const int len)
701{
702 unsigned char m;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100703 const struct http_method_desc *h;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100704
705 m = ((unsigned)*str - 'A');
706
707 if (m < 26) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100708 for (h = http_methods[m]; h->len > 0; h++) {
709 if (unlikely(h->len != len))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100710 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100711 if (likely(memcmp(str, h->text, h->len) == 0))
Willy Tarreau53b6c742006-12-17 13:37:46 +0100712 return h->meth;
Willy Tarreau53b6c742006-12-17 13:37:46 +0100713 };
714 return HTTP_METH_OTHER;
715 }
716 return HTTP_METH_NONE;
717
718}
719
Willy Tarreau21d2af32008-02-14 20:25:24 +0100720/* Parse the URI from the given transaction (which is assumed to be in request
721 * phase) and look for the "/" beginning the PATH. If not found, return NULL.
722 * It is returned otherwise.
723 */
724static char *
725http_get_path(struct http_txn *txn)
726{
727 char *ptr, *end;
728
Willy Tarreau9b28e032012-10-12 23:49:43 +0200729 ptr = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau21d2af32008-02-14 20:25:24 +0100730 end = ptr + txn->req.sl.rq.u_l;
731
732 if (ptr >= end)
733 return NULL;
734
735 /* RFC2616, par. 5.1.2 :
736 * Request-URI = "*" | absuri | abspath | authority
737 */
738
739 if (*ptr == '*')
740 return NULL;
741
742 if (isalpha((unsigned char)*ptr)) {
743 /* this is a scheme as described by RFC3986, par. 3.1 */
744 ptr++;
745 while (ptr < end &&
746 (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.'))
747 ptr++;
748 /* skip '://' */
749 if (ptr == end || *ptr++ != ':')
750 return NULL;
751 if (ptr == end || *ptr++ != '/')
752 return NULL;
753 if (ptr == end || *ptr++ != '/')
754 return NULL;
755 }
756 /* skip [user[:passwd]@]host[:[port]] */
757
758 while (ptr < end && *ptr != '/')
759 ptr++;
760
761 if (ptr == end)
762 return NULL;
763
764 /* OK, we got the '/' ! */
765 return ptr;
766}
767
Willy Tarreau71241ab2012-12-27 11:30:54 +0100768/* Returns a 302 for a redirectable request that reaches a server working in
769 * in redirect mode. This may only be called just after the stream interface
770 * has moved to SI_ST_ASS. Unprocessable requests are left unchanged and will
771 * follow normal proxy processing. NOTE: this function is designed to support
772 * being called once data are scheduled for forwarding.
Willy Tarreauefb453c2008-10-26 20:49:47 +0100773 */
Willy Tarreau71241ab2012-12-27 11:30:54 +0100774void http_perform_server_redirect(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100775{
776 struct http_txn *txn;
Willy Tarreau827aee92011-03-10 16:55:02 +0100777 struct server *srv;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100778 char *path;
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200779 int len, rewind;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100780
781 /* 1: create the response header */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100782 trash.len = strlen(HTTP_302);
783 memcpy(trash.str, HTTP_302, trash.len);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100784
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100785 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100786
Willy Tarreauefb453c2008-10-26 20:49:47 +0100787 /* 2: add the server's prefix */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100788 if (trash.len + srv->rdr_len > trash.size)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100789 return;
790
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100791 /* special prefix "/" means don't change URL */
Willy Tarreau827aee92011-03-10 16:55:02 +0100792 if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100793 memcpy(trash.str + trash.len, srv->rdr_pfx, srv->rdr_len);
794 trash.len += srv->rdr_len;
Willy Tarreaudcb75c42010-01-10 00:24:22 +0100795 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100796
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200797 /* 3: add the request URI. Since it was already forwarded, we need
798 * to temporarily rewind the buffer.
799 */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100800 txn = &s->txn;
Willy Tarreau9b28e032012-10-12 23:49:43 +0200801 b_rew(s->req->buf, rewind = s->req->buf->o);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200802
Willy Tarreauefb453c2008-10-26 20:49:47 +0100803 path = http_get_path(txn);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200804 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 +0200805
Willy Tarreau9b28e032012-10-12 23:49:43 +0200806 b_adv(s->req->buf, rewind);
Willy Tarreaucde18fc2012-05-30 07:59:54 +0200807
Willy Tarreauefb453c2008-10-26 20:49:47 +0100808 if (!path)
809 return;
810
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100811 if (trash.len + len > trash.size - 4) /* 4 for CRLF-CRLF */
Willy Tarreauefb453c2008-10-26 20:49:47 +0100812 return;
813
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100814 memcpy(trash.str + trash.len, path, len);
815 trash.len += len;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100816
817 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100818 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
819 trash.len += 29;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100820 } else {
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100821 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
822 trash.len += 23;
Willy Tarreau88d349d2010-01-25 12:15:43 +0100823 }
Willy Tarreauefb453c2008-10-26 20:49:47 +0100824
825 /* prepare to return without error. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200826 si_shutr(si);
827 si_shutw(si);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100828 si->err_type = SI_ET_NONE;
829 si->err_loc = NULL;
830 si->state = SI_ST_CLO;
831
832 /* send the message */
Willy Tarreau19d14ef2012-10-29 16:51:55 +0100833 http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &trash);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100834
835 /* FIXME: we should increase a counter of redirects per server and per backend. */
Willy Tarreau4521ba62013-01-24 01:25:25 +0100836 srv_inc_sess_ctr(srv);
Willy Tarreauefb453c2008-10-26 20:49:47 +0100837}
838
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100839/* Return the error message corresponding to si->err_type. It is assumed
Willy Tarreauefb453c2008-10-26 20:49:47 +0100840 * that the server side is closed. Note that err_type is actually a
841 * bitmask, where almost only aborts may be cumulated with other
842 * values. We consider that aborted operations are more important
843 * than timeouts or errors due to the fact that nobody else in the
844 * logs might explain incomplete retries. All others should avoid
845 * being cumulated. It should normally not be possible to have multiple
846 * aborts at once, but just in case, the first one in sequence is reported.
847 */
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100848void http_return_srv_error(struct session *s, struct stream_interface *si)
Willy Tarreauefb453c2008-10-26 20:49:47 +0100849{
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100850 int err_type = si->err_type;
Willy Tarreauefb453c2008-10-26 20:49:47 +0100851
852 if (err_type & SI_ET_QUEUE_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100853 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200854 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100855 else if (err_type & SI_ET_CONN_ABRT)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100856 http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200857 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100858 else if (err_type & SI_ET_QUEUE_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100859 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200860 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100861 else if (err_type & SI_ET_QUEUE_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100862 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q,
Willy Tarreau783f2582012-09-04 12:19:04 +0200863 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100864 else if (err_type & SI_ET_CONN_TO)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100865 http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200866 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100867 else if (err_type & SI_ET_CONN_ERR)
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100868 http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200869 503, http_error_message(s, HTTP_ERR_503));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100870 else /* SI_ET_CONN_OTHER and others */
Willy Tarreau2d3d94c2008-11-30 20:20:08 +0100871 http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C,
Willy Tarreau783f2582012-09-04 12:19:04 +0200872 500, http_error_message(s, HTTP_ERR_500));
Willy Tarreauefb453c2008-10-26 20:49:47 +0100873}
874
Willy Tarreau42250582007-04-01 01:30:43 +0200875extern const char sess_term_cond[8];
876extern const char sess_fin_state[8];
877extern const char *monthname[12];
Willy Tarreau332f8bf2007-05-13 21:36:56 +0200878struct pool_head *pool2_requri;
Willy Tarreau193b8c62012-11-22 00:17:38 +0100879struct pool_head *pool2_capture = NULL;
William Lallemanda73203e2012-03-12 12:48:57 +0100880struct pool_head *pool2_uniqueid;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100881
Willy Tarreau117f59e2007-03-04 18:17:17 +0100882/*
883 * Capture headers from message starting at <som> according to header list
884 * <cap_hdr>, and fill the <idx> structure appropriately.
885 */
886void capture_headers(char *som, struct hdr_idx *idx,
887 char **cap, struct cap_hdr *cap_hdr)
888{
889 char *eol, *sol, *col, *sov;
890 int cur_idx;
891 struct cap_hdr *h;
892 int len;
893
894 sol = som + hdr_idx_first_pos(idx);
895 cur_idx = hdr_idx_first_idx(idx);
896
897 while (cur_idx) {
898 eol = sol + idx->v[cur_idx].len;
899
900 col = sol;
901 while (col < eol && *col != ':')
902 col++;
903
904 sov = col + 1;
905 while (sov < eol && http_is_lws[(unsigned char)*sov])
906 sov++;
907
908 for (h = cap_hdr; h; h = h->next) {
909 if ((h->namelen == col - sol) &&
910 (strncasecmp(sol, h->name, h->namelen) == 0)) {
911 if (cap[h->index] == NULL)
912 cap[h->index] =
Willy Tarreaucf7f3202007-05-13 22:46:04 +0200913 pool_alloc2(h->pool);
Willy Tarreau117f59e2007-03-04 18:17:17 +0100914
915 if (cap[h->index] == NULL) {
916 Alert("HTTP capture : out of memory.\n");
917 continue;
918 }
919
920 len = eol - sov;
921 if (len > h->len)
922 len = h->len;
923
924 memcpy(cap[h->index], sov, len);
925 cap[h->index][len]=0;
926 }
927 }
928 sol = eol + idx->v[cur_idx].cr + 1;
929 cur_idx = idx->v[cur_idx].next;
930 }
931}
932
933
Willy Tarreau42250582007-04-01 01:30:43 +0200934/* either we find an LF at <ptr> or we jump to <bad>.
935 */
936#define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0)
937
938/* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK,
939 * otherwise to <http_msg_ood> with <state> set to <st>.
940 */
941#define EAT_AND_JUMP_OR_RETURN(good, st) do { \
942 ptr++; \
943 if (likely(ptr < end)) \
944 goto good; \
945 else { \
946 state = (st); \
947 goto http_msg_ood; \
948 } \
949 } while (0)
950
951
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952/*
Willy Tarreaua15645d2007-03-18 16:22:39 +0100953 * This function parses a status line between <ptr> and <end>, starting with
Willy Tarreau8973c702007-01-21 23:58:29 +0100954 * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP,
955 * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others
956 * will give undefined results.
957 * Note that it is upon the caller's responsibility to ensure that ptr < end,
958 * and that msg->sol points to the beginning of the response.
959 * If a complete line is found (which implies that at least one CR or LF is
960 * found before <end>, the updated <ptr> is returned, otherwise NULL is
961 * returned indicating an incomplete line (which does not mean that parts have
962 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
963 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
964 * upon next call.
965 *
Willy Tarreau9cdde232007-05-02 20:58:19 +0200966 * This function was intentionally designed to be called from
Willy Tarreau8973c702007-01-21 23:58:29 +0100967 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
968 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +0200969 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreau8973c702007-01-21 23:58:29 +0100970 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +0200971const char *http_parse_stsline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +0100972 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +0100973 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreau8973c702007-01-21 23:58:29 +0100974{
Willy Tarreau9b28e032012-10-12 23:49:43 +0200975 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100976
Willy Tarreau8973c702007-01-21 23:58:29 +0100977 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +0100978 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200979 http_msg_rpver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +0100980 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8973c702007-01-21 23:58:29 +0100981 EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER);
982
983 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100984 msg->sl.st.v_l = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100985 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
986 }
Willy Tarreau7552c032009-03-01 11:10:40 +0100987 state = HTTP_MSG_ERROR;
988 break;
989
Willy Tarreau8973c702007-01-21 23:58:29 +0100990 case HTTP_MSG_RPVER_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +0200991 http_msg_rpver_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +0100992 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +0100993 msg->sl.st.c = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +0100994 goto http_msg_rpcode;
995 }
996 if (likely(HTTP_IS_SPHT(*ptr)))
997 EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP);
998 /* so it's a CR/LF, this is invalid */
Willy Tarreau7552c032009-03-01 11:10:40 +0100999 state = HTTP_MSG_ERROR;
1000 break;
Willy Tarreau8973c702007-01-21 23:58:29 +01001001
Willy Tarreau8973c702007-01-21 23:58:29 +01001002 case HTTP_MSG_RPCODE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001003 http_msg_rpcode:
Willy Tarreau8973c702007-01-21 23:58:29 +01001004 if (likely(!HTTP_IS_LWS(*ptr)))
1005 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE);
1006
1007 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001008 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001009 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1010 }
1011
1012 /* so it's a CR/LF, so there is no reason phrase */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001013 msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c;
Willy Tarreau8973c702007-01-21 23:58:29 +01001014 http_msg_rsp_reason:
1015 /* FIXME: should we support HTTP responses without any reason phrase ? */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001016 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001017 msg->sl.st.r_l = 0;
1018 goto http_msg_rpline_eol;
1019
Willy Tarreau8973c702007-01-21 23:58:29 +01001020 case HTTP_MSG_RPCODE_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001021 http_msg_rpcode_sp:
Willy Tarreau8973c702007-01-21 23:58:29 +01001022 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001023 msg->sl.st.r = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001024 goto http_msg_rpreason;
1025 }
1026 if (likely(HTTP_IS_SPHT(*ptr)))
1027 EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP);
1028 /* so it's a CR/LF, so there is no reason phrase */
1029 goto http_msg_rsp_reason;
1030
Willy Tarreau8973c702007-01-21 23:58:29 +01001031 case HTTP_MSG_RPREASON:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001032 http_msg_rpreason:
Willy Tarreau8973c702007-01-21 23:58:29 +01001033 if (likely(!HTTP_IS_CRLF(*ptr)))
1034 EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON);
Willy Tarreauea1175a2012-03-05 15:52:30 +01001035 msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r;
Willy Tarreau8973c702007-01-21 23:58:29 +01001036 http_msg_rpline_eol:
1037 /* We have seen the end of line. Note that we do not
1038 * necessarily have the \n yet, but at least we know that we
1039 * have EITHER \r OR \n, otherwise the response would not be
1040 * complete. We can then record the response length and return
1041 * to the caller which will be able to register it.
1042 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001043 msg->sl.st.l = ptr - msg_start - msg->sol;
Willy Tarreau8973c702007-01-21 23:58:29 +01001044 return ptr;
1045
1046#ifdef DEBUG_FULL
1047 default:
1048 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1049 exit(1);
1050#endif
1051 }
1052
1053 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001054 /* out of valid data */
Willy Tarreau8973c702007-01-21 23:58:29 +01001055 if (ret_state)
1056 *ret_state = state;
1057 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001058 *ret_ptr = ptr - msg_start;
Willy Tarreau8973c702007-01-21 23:58:29 +01001059 return NULL;
Willy Tarreau8973c702007-01-21 23:58:29 +01001060}
1061
Willy Tarreau8973c702007-01-21 23:58:29 +01001062/*
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001063 * This function parses a request line between <ptr> and <end>, starting with
1064 * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP,
1065 * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others
1066 * will give undefined results.
1067 * Note that it is upon the caller's responsibility to ensure that ptr < end,
1068 * and that msg->sol points to the beginning of the request.
1069 * If a complete line is found (which implies that at least one CR or LF is
1070 * found before <end>, the updated <ptr> is returned, otherwise NULL is
1071 * returned indicating an incomplete line (which does not mean that parts have
1072 * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are
1073 * non-NULL, they are fed with the new <ptr> and <state> values to be passed
1074 * upon next call.
1075 *
Willy Tarreau9cdde232007-05-02 20:58:19 +02001076 * This function was intentionally designed to be called from
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001077 * http_msg_analyzer() with the lowest overhead. It should integrate perfectly
1078 * within its state machine and use the same macros, hence the need for same
Willy Tarreau9cdde232007-05-02 20:58:19 +02001079 * labels and variable names. Note that msg->sol is left unchanged.
Willy Tarreaubaaee002006-06-26 02:48:02 +02001080 */
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001081const char *http_parse_reqline(struct http_msg *msg,
Willy Tarreaue69eada2008-01-27 00:34:10 +01001082 unsigned int state, const char *ptr, const char *end,
Willy Tarreaua458b672012-03-05 11:17:50 +01001083 unsigned int *ret_ptr, unsigned int *ret_state)
Willy Tarreaubaaee002006-06-26 02:48:02 +02001084{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001085 const char *msg_start = msg->chn->buf->p;
Willy Tarreau62f791e2012-03-09 11:32:30 +01001086
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001087 switch (state) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001088 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001089 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001090 if (likely(HTTP_IS_TOKEN(*ptr)))
1091 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001092
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001093 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001094 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001095 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1096 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001097
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001098 if (likely(HTTP_IS_CRLF(*ptr))) {
1099 /* HTTP 0.9 request */
Willy Tarreauea1175a2012-03-05 15:52:30 +01001100 msg->sl.rq.m_l = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001101 http_msg_req09_uri:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001102 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001103 http_msg_req09_uri_e:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001104 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001105 http_msg_req09_ver:
Willy Tarreauea1175a2012-03-05 15:52:30 +01001106 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001107 msg->sl.rq.v_l = 0;
1108 goto http_msg_rqline_eol;
1109 }
Willy Tarreau7552c032009-03-01 11:10:40 +01001110 state = HTTP_MSG_ERROR;
1111 break;
1112
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001113 case HTTP_MSG_RQMETH_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001114 http_msg_rqmeth_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001115 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001116 msg->sl.rq.u = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001117 goto http_msg_rquri;
1118 }
1119 if (likely(HTTP_IS_SPHT(*ptr)))
1120 EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP);
1121 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1122 goto http_msg_req09_uri;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001123
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001124 case HTTP_MSG_RQURI:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001125 http_msg_rquri:
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001126 if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001127 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001128
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001129 if (likely(HTTP_IS_SPHT(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001130 msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001131 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1132 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001133
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001134 if (likely((unsigned char)*ptr >= 128)) {
Willy Tarreau422246e2012-01-07 23:54:13 +01001135 /* non-ASCII chars are forbidden unless option
1136 * accept-invalid-http-request is enabled in the frontend.
1137 * In any case, we capture the faulty char.
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001138 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001139 if (msg->err_pos < -1)
1140 goto invalid_char;
1141 if (msg->err_pos == -1)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001142 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001143 EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI);
1144 }
1145
1146 if (likely(HTTP_IS_CRLF(*ptr))) {
1147 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1148 goto http_msg_req09_uri_e;
1149 }
1150
1151 /* OK forbidden chars, 0..31 or 127 */
Willy Tarreau422246e2012-01-07 23:54:13 +01001152 invalid_char:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001153 msg->err_pos = ptr - msg_start;
Willy Tarreau2e9506d2012-01-07 23:22:31 +01001154 state = HTTP_MSG_ERROR;
1155 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001156
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001157 case HTTP_MSG_RQURI_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001158 http_msg_rquri_sp:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001159 if (likely(!HTTP_IS_LWS(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001160 msg->sl.rq.v = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001161 goto http_msg_rqver;
1162 }
1163 if (likely(HTTP_IS_SPHT(*ptr)))
1164 EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP);
1165 /* so it's a CR/LF, meaning an HTTP 0.9 request */
1166 goto http_msg_req09_ver;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001167
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001168 case HTTP_MSG_RQVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001169 http_msg_rqver:
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001170 if (likely(HTTP_IS_VER_TOKEN(*ptr)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001171 EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER);
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001172
1173 if (likely(HTTP_IS_CRLF(*ptr))) {
Willy Tarreauea1175a2012-03-05 15:52:30 +01001174 msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001175 http_msg_rqline_eol:
1176 /* We have seen the end of line. Note that we do not
1177 * necessarily have the \n yet, but at least we know that we
1178 * have EITHER \r OR \n, otherwise the request would not be
1179 * complete. We can then record the request length and return
1180 * to the caller which will be able to register it.
1181 */
Willy Tarreau3a215be2012-03-09 21:39:51 +01001182 msg->sl.rq.l = ptr - msg_start - msg->sol;
Willy Tarreau4b89ad42007-03-04 18:13:58 +01001183 return ptr;
1184 }
1185
1186 /* neither an HTTP_VER token nor a CRLF */
Willy Tarreau7552c032009-03-01 11:10:40 +01001187 state = HTTP_MSG_ERROR;
1188 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001189
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001190#ifdef DEBUG_FULL
1191 default:
1192 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1193 exit(1);
1194#endif
1195 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01001196
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001197 http_msg_ood:
Willy Tarreau7552c032009-03-01 11:10:40 +01001198 /* out of valid data */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001199 if (ret_state)
1200 *ret_state = state;
1201 if (ret_ptr)
Willy Tarreaua458b672012-03-05 11:17:50 +01001202 *ret_ptr = ptr - msg_start;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001203 return NULL;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001204}
Willy Tarreau58f10d72006-12-04 02:26:12 +01001205
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001206/*
1207 * Returns the data from Authorization header. Function may be called more
1208 * than once so data is stored in txn->auth_data. When no header is found
1209 * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid
1210 * searching again for something we are unable to find anyway.
1211 */
1212
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001213char *get_http_auth_buff;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001214
1215int
1216get_http_auth(struct session *s)
1217{
1218
1219 struct http_txn *txn = &s->txn;
1220 struct chunk auth_method;
1221 struct hdr_ctx ctx;
1222 char *h, *p;
1223 int len;
1224
1225#ifdef DEBUG_AUTH
1226 printf("Auth for session %p: %d\n", s, txn->auth.method);
1227#endif
1228
1229 if (txn->auth.method == HTTP_AUTH_WRONG)
1230 return 0;
1231
1232 if (txn->auth.method)
1233 return 1;
1234
1235 txn->auth.method = HTTP_AUTH_WRONG;
1236
1237 ctx.idx = 0;
Willy Tarreau844a7e72010-01-31 21:46:18 +01001238
1239 if (txn->flags & TX_USE_PX_CONN) {
1240 h = "Proxy-Authorization";
1241 len = strlen(h);
1242 } else {
1243 h = "Authorization";
1244 len = strlen(h);
1245 }
1246
Willy Tarreau9b28e032012-10-12 23:49:43 +02001247 if (!http_find_header2(h, len, s->req->buf->p, &txn->hdr_idx, &ctx))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001248 return 0;
1249
1250 h = ctx.line + ctx.val;
1251
1252 p = memchr(h, ' ', ctx.vlen);
1253 if (!p || p == h)
1254 return 0;
1255
1256 chunk_initlen(&auth_method, h, 0, p-h);
1257 chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1);
1258
1259 if (!strncasecmp("Basic", auth_method.str, auth_method.len)) {
1260
1261 len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len,
Willy Tarreau7e2c6472012-10-29 20:44:36 +01001262 get_http_auth_buff, global.tune.bufsize - 1);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001263
1264 if (len < 0)
1265 return 0;
1266
1267
1268 get_http_auth_buff[len] = '\0';
1269
1270 p = strchr(get_http_auth_buff, ':');
1271
1272 if (!p)
1273 return 0;
1274
1275 txn->auth.user = get_http_auth_buff;
1276 *p = '\0';
1277 txn->auth.pass = p+1;
1278
1279 txn->auth.method = HTTP_AUTH_BASIC;
1280 return 1;
1281 }
1282
1283 return 0;
1284}
1285
Willy Tarreau58f10d72006-12-04 02:26:12 +01001286
Willy Tarreau8973c702007-01-21 23:58:29 +01001287/*
1288 * This function parses an HTTP message, either a request or a response,
Willy Tarreau8b1323e2012-03-09 14:46:19 +01001289 * depending on the initial msg->msg_state. The caller is responsible for
1290 * ensuring that the message does not wrap. The function can be preempted
1291 * everywhere when data are missing and recalled at the exact same location
1292 * with no information loss. The message may even be realigned between two
1293 * calls. The header index is re-initialized when switching from
Willy Tarreau9cdde232007-05-02 20:58:19 +02001294 * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other
Willy Tarreau26927362012-05-18 23:22:52 +02001295 * fields. Note that msg->sol will be initialized after completing the first
1296 * state, so that none of the msg pointers has to be initialized prior to the
1297 * first call.
Willy Tarreau8973c702007-01-21 23:58:29 +01001298 */
Willy Tarreaua560c212012-03-09 13:50:57 +01001299void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001300{
Willy Tarreaue69eada2008-01-27 00:34:10 +01001301 unsigned int state; /* updated only when leaving the FSM */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001302 register char *ptr, *end; /* request pointers, to avoid dereferences */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001303 struct buffer *buf;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001304
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001305 state = msg->msg_state;
Willy Tarreau9b28e032012-10-12 23:49:43 +02001306 buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001307 ptr = buf->p + msg->next;
1308 end = buf->p + buf->i;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001309
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001310 if (unlikely(ptr >= end))
1311 goto http_msg_ood;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001312
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001313 switch (state) {
Willy Tarreau8973c702007-01-21 23:58:29 +01001314 /*
1315 * First, states that are specific to the response only.
1316 * We check them first so that request and headers are
1317 * closer to each other (accessed more often).
1318 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001319 case HTTP_MSG_RPBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001320 http_msg_rpbefore:
Willy Tarreau8973c702007-01-21 23:58:29 +01001321 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001322 /* we have a start of message, but we have to check
1323 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001324 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001325 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001326 if (unlikely(ptr != buf->p)) {
1327 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001328 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001329 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001330 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8973c702007-01-21 23:58:29 +01001331 }
Willy Tarreau26927362012-05-18 23:22:52 +02001332 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001333 msg->sl.st.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001334 hdr_idx_init(idx);
1335 state = HTTP_MSG_RPVER;
1336 goto http_msg_rpver;
1337 }
1338
1339 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1340 goto http_msg_invalid;
1341
1342 if (unlikely(*ptr == '\n'))
1343 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1344 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR);
1345 /* stop here */
1346
Willy Tarreau8973c702007-01-21 23:58:29 +01001347 case HTTP_MSG_RPBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001348 http_msg_rpbefore_cr:
Willy Tarreau8973c702007-01-21 23:58:29 +01001349 EXPECT_LF_HERE(ptr, http_msg_invalid);
1350 EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE);
1351 /* stop here */
1352
Willy Tarreau8973c702007-01-21 23:58:29 +01001353 case HTTP_MSG_RPVER:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001354 http_msg_rpver:
Willy Tarreau8973c702007-01-21 23:58:29 +01001355 case HTTP_MSG_RPVER_SP:
1356 case HTTP_MSG_RPCODE:
1357 case HTTP_MSG_RPCODE_SP:
1358 case HTTP_MSG_RPREASON:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001359 ptr = (char *)http_parse_stsline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001360 state, ptr, end,
1361 &msg->next, &msg->msg_state);
Willy Tarreau8973c702007-01-21 23:58:29 +01001362 if (unlikely(!ptr))
1363 return;
1364
1365 /* we have a full response and we know that we have either a CR
1366 * or an LF at <ptr>.
1367 */
Willy Tarreau8973c702007-01-21 23:58:29 +01001368 hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r');
1369
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001370 msg->sol = ptr - buf->p;
Willy Tarreau8973c702007-01-21 23:58:29 +01001371 if (likely(*ptr == '\r'))
1372 EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END);
1373 goto http_msg_rpline_end;
1374
Willy Tarreau8973c702007-01-21 23:58:29 +01001375 case HTTP_MSG_RPLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001376 http_msg_rpline_end:
Willy Tarreau8973c702007-01-21 23:58:29 +01001377 /* msg->sol must point to the first of CR or LF. */
1378 EXPECT_LF_HERE(ptr, http_msg_invalid);
1379 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
1380 /* stop here */
1381
1382 /*
1383 * Second, states that are specific to the request only
1384 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001385 case HTTP_MSG_RQBEFORE:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001386 http_msg_rqbefore:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001387 if (likely(HTTP_IS_TOKEN(*ptr))) {
Willy Tarreau15de77e2010-01-02 21:59:16 +01001388 /* we have a start of message, but we have to check
1389 * first if we need to remove some CRLF. We can only
Willy Tarreau2e046c62012-03-01 16:08:30 +01001390 * do this when o=0.
Willy Tarreau15de77e2010-01-02 21:59:16 +01001391 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001392 if (likely(ptr != buf->p)) {
1393 if (buf->o)
Willy Tarreau15de77e2010-01-02 21:59:16 +01001394 goto http_msg_ood;
Willy Tarreau1d3bcce2009-12-27 15:50:06 +01001395 /* Remove empty leading lines, as recommended by RFC2616. */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001396 bi_fast_delete(buf, ptr - buf->p);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001397 }
Willy Tarreau26927362012-05-18 23:22:52 +02001398 msg->sol = 0;
Willy Tarreaue92693a2012-09-24 21:13:39 +02001399 msg->sl.rq.l = 0; /* used in debug mode */
Willy Tarreau8973c702007-01-21 23:58:29 +01001400 state = HTTP_MSG_RQMETH;
1401 goto http_msg_rqmeth;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001402 }
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001403
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001404 if (unlikely(!HTTP_IS_CRLF(*ptr)))
1405 goto http_msg_invalid;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001406
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001407 if (unlikely(*ptr == '\n'))
1408 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
1409 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore_cr, HTTP_MSG_RQBEFORE_CR);
Willy Tarreau8973c702007-01-21 23:58:29 +01001410 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001411
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001412 case HTTP_MSG_RQBEFORE_CR:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001413 http_msg_rqbefore_cr:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001414 EXPECT_LF_HERE(ptr, http_msg_invalid);
1415 EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE);
Willy Tarreau8973c702007-01-21 23:58:29 +01001416 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001417
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001418 case HTTP_MSG_RQMETH:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001419 http_msg_rqmeth:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001420 case HTTP_MSG_RQMETH_SP:
1421 case HTTP_MSG_RQURI:
1422 case HTTP_MSG_RQURI_SP:
1423 case HTTP_MSG_RQVER:
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001424 ptr = (char *)http_parse_reqline(msg,
Willy Tarreaua458b672012-03-05 11:17:50 +01001425 state, ptr, end,
1426 &msg->next, &msg->msg_state);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001427 if (unlikely(!ptr))
1428 return;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001429
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001430 /* we have a full request and we know that we have either a CR
1431 * or an LF at <ptr>.
1432 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001433 hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r');
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001434
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001435 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001436 if (likely(*ptr == '\r'))
1437 EAT_AND_JUMP_OR_RETURN(http_msg_rqline_end, HTTP_MSG_RQLINE_END);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001438 goto http_msg_rqline_end;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001439
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001440 case HTTP_MSG_RQLINE_END:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001441 http_msg_rqline_end:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001442 /* check for HTTP/0.9 request : no version information available.
1443 * msg->sol must point to the first of CR or LF.
1444 */
1445 if (unlikely(msg->sl.rq.v_l == 0))
1446 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001447
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001448 EXPECT_LF_HERE(ptr, http_msg_invalid);
1449 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST);
Willy Tarreau8973c702007-01-21 23:58:29 +01001450 /* stop here */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001451
Willy Tarreau8973c702007-01-21 23:58:29 +01001452 /*
1453 * Common states below
1454 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001455 case HTTP_MSG_HDR_FIRST:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001456 http_msg_hdr_first:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001457 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001458 if (likely(!HTTP_IS_CRLF(*ptr))) {
1459 goto http_msg_hdr_name;
1460 }
1461
1462 if (likely(*ptr == '\r'))
1463 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1464 goto http_msg_last_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001465
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001466 case HTTP_MSG_HDR_NAME:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001467 http_msg_hdr_name:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001468 /* assumes msg->sol points to the first char */
1469 if (likely(HTTP_IS_TOKEN(*ptr)))
1470 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001471
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001472 if (likely(*ptr == ':'))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001473 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau58f10d72006-12-04 02:26:12 +01001474
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001475 if (likely(msg->err_pos < -1) || *ptr == '\n')
1476 goto http_msg_invalid;
1477
1478 if (msg->err_pos == -1) /* capture error pointer */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001479 msg->err_pos = ptr - buf->p; /* >= 0 now */
Willy Tarreau32a4ec02009-04-02 11:35:18 +02001480
1481 /* and we still accept this non-token character */
1482 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_name, HTTP_MSG_HDR_NAME);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001483
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001484 case HTTP_MSG_HDR_L1_SP:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001485 http_msg_hdr_l1_sp:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001486 /* assumes msg->sol points to the first char */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001487 if (likely(HTTP_IS_SPHT(*ptr)))
1488 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001489
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001490 /* header value can be basically anything except CR/LF */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001491 msg->sov = ptr - buf->p;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001492
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001493 if (likely(!HTTP_IS_CRLF(*ptr))) {
1494 goto http_msg_hdr_val;
1495 }
1496
1497 if (likely(*ptr == '\r'))
1498 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF);
1499 goto http_msg_hdr_l1_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001500
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001501 case HTTP_MSG_HDR_L1_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001502 http_msg_hdr_l1_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001503 EXPECT_LF_HERE(ptr, http_msg_invalid);
1504 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lws, HTTP_MSG_HDR_L1_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001505
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001506 case HTTP_MSG_HDR_L1_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001507 http_msg_hdr_l1_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001508 if (likely(HTTP_IS_SPHT(*ptr))) {
1509 /* replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001510 for (; buf->p + msg->sov < ptr; msg->sov++)
1511 buf->p[msg->sov] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001512 goto http_msg_hdr_l1_sp;
1513 }
Willy Tarreauaa9dce32007-03-18 23:50:16 +01001514 /* we had a header consisting only in spaces ! */
Willy Tarreau12e48b32012-03-05 16:57:34 +01001515 msg->eol = msg->sov;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001516 goto http_msg_complete_header;
1517
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001518 case HTTP_MSG_HDR_VAL:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001519 http_msg_hdr_val:
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001520 /* assumes msg->sol points to the first char, and msg->sov
1521 * points to the first character of the value.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001522 */
1523 if (likely(!HTTP_IS_CRLF(*ptr)))
1524 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_val, HTTP_MSG_HDR_VAL);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001525
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001526 msg->eol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001527 /* Note: we could also copy eol into ->eoh so that we have the
1528 * real header end in case it ends with lots of LWS, but is this
1529 * really needed ?
1530 */
1531 if (likely(*ptr == '\r'))
1532 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF);
1533 goto http_msg_hdr_l2_lf;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001534
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001535 case HTTP_MSG_HDR_L2_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001536 http_msg_hdr_l2_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001537 EXPECT_LF_HERE(ptr, http_msg_invalid);
1538 EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lws, HTTP_MSG_HDR_L2_LWS);
Willy Tarreau976f1ee2006-12-17 10:06:03 +01001539
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001540 case HTTP_MSG_HDR_L2_LWS:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001541 http_msg_hdr_l2_lws:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001542 if (unlikely(HTTP_IS_SPHT(*ptr))) {
1543 /* LWS: replace HT,CR,LF with spaces */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001544 for (; buf->p + msg->eol < ptr; msg->eol++)
1545 buf->p[msg->eol] = ' ';
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001546 goto http_msg_hdr_val;
1547 }
1548 http_msg_complete_header:
1549 /*
1550 * It was a new header, so the last one is finished.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01001551 * Assumes msg->sol points to the first char, msg->sov points
1552 * to the first character of the value and msg->eol to the
1553 * first CR or LF so we know how the line ends. We insert last
1554 * header into the index.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001555 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001556 if (unlikely(hdr_idx_add(msg->eol - msg->sol, buf->p[msg->eol] == '\r',
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001557 idx, idx->tail) < 0))
1558 goto http_msg_invalid;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001559
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001560 msg->sol = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001561 if (likely(!HTTP_IS_CRLF(*ptr))) {
1562 goto http_msg_hdr_name;
1563 }
1564
1565 if (likely(*ptr == '\r'))
1566 EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF);
1567 goto http_msg_last_lf;
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001568
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001569 case HTTP_MSG_LAST_LF:
Willy Tarreaue3f284a2010-09-28 19:42:42 +02001570 http_msg_last_lf:
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001571 /* Assumes msg->sol points to the first of either CR or LF */
1572 EXPECT_LF_HERE(ptr, http_msg_invalid);
1573 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001574 msg->sov = msg->next = ptr - buf->p;
Willy Tarreau3a215be2012-03-09 21:39:51 +01001575 msg->eoh = msg->sol;
1576 msg->sol = 0;
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001577 msg->msg_state = HTTP_MSG_BODY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001578 return;
Willy Tarreaub56928a2012-04-16 14:51:55 +02001579
1580 case HTTP_MSG_ERROR:
1581 /* this may only happen if we call http_msg_analyser() twice with an error */
1582 break;
1583
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001584#ifdef DEBUG_FULL
1585 default:
1586 fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state);
1587 exit(1);
Willy Tarreau230fd0b2006-12-17 12:05:00 +01001588#endif
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001589 }
1590 http_msg_ood:
1591 /* out of data */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001592 msg->msg_state = state;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001593 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001594 return;
Willy Tarreau58f10d72006-12-04 02:26:12 +01001595
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001596 http_msg_invalid:
1597 /* invalid message */
Willy Tarreaub326fcc2007-03-03 13:54:32 +01001598 msg->msg_state = HTTP_MSG_ERROR;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001599 msg->next = ptr - buf->p;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01001600 return;
1601}
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001602
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001603/* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the
1604 * conversion succeeded, 0 in case of error. If the request was already 1.X,
1605 * nothing is done and 1 is returned.
1606 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001607static int http_upgrade_v09_to_v10(struct http_txn *txn)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001608{
1609 int delta;
1610 char *cur_end;
Willy Tarreau418bfcc2012-03-09 13:56:20 +01001611 struct http_msg *msg = &txn->req;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001612
1613 if (msg->sl.rq.v_l != 0)
1614 return 1;
1615
Willy Tarreau9b28e032012-10-12 23:49:43 +02001616 cur_end = msg->chn->buf->p + msg->sl.rq.l;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001617 delta = 0;
1618
1619 if (msg->sl.rq.u_l == 0) {
1620 /* if no URI was set, add "/" */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001621 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001622 cur_end += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01001623 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001624 }
1625 /* add HTTP version */
Willy Tarreau9b28e032012-10-12 23:49:43 +02001626 delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " HTTP/1.0\r\n", 11);
Willy Tarreaufa355d42009-11-29 18:12:29 +01001627 http_msg_move_end(msg, delta);
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001628 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02001629 cur_end = (char *)http_parse_reqline(msg,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001630 HTTP_MSG_RQMETH,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001631 msg->chn->buf->p, cur_end + 1,
Willy Tarreau2492d5b2009-07-11 00:06:00 +02001632 NULL, NULL);
1633 if (unlikely(!cur_end))
1634 return 0;
1635
1636 /* we have a full HTTP/1.0 request now and we know that
1637 * we have either a CR or an LF at <ptr>.
1638 */
1639 hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r');
1640 return 1;
1641}
1642
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001643/* Parse the Connection: header of an HTTP request, looking for both "close"
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001644 * and "keep-alive" values. If we already know that some headers may safely
1645 * be removed, we remove them now. The <to_del> flags are used for that :
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001646 * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses)
1647 * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1).
Willy Tarreau50fc7772012-11-11 22:19:57 +01001648 * Presence of the "Upgrade" token is also checked and reported.
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001649 * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was
1650 * found, and TX_CON_*_SET is adjusted depending on what is left so only
1651 * harmless combinations may be removed. Do not call that after changes have
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001652 * been processed.
Willy Tarreau5b154472009-12-21 20:11:07 +01001653 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001654void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, int to_del)
Willy Tarreau5b154472009-12-21 20:11:07 +01001655{
Willy Tarreau5b154472009-12-21 20:11:07 +01001656 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001657 const char *hdr_val = "Connection";
1658 int hdr_len = 10;
Willy Tarreau5b154472009-12-21 20:11:07 +01001659
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001660 if (txn->flags & TX_HDR_CONN_PRS)
Willy Tarreau5b154472009-12-21 20:11:07 +01001661 return;
1662
Willy Tarreau88d349d2010-01-25 12:15:43 +01001663 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1664 hdr_val = "Proxy-Connection";
1665 hdr_len = 16;
1666 }
1667
Willy Tarreau5b154472009-12-21 20:11:07 +01001668 ctx.idx = 0;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001669 txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001670 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001671 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1672 txn->flags |= TX_HDR_CONN_KAL;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001673 if (to_del & 2)
1674 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001675 else
1676 txn->flags |= TX_CON_KAL_SET;
1677 }
1678 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1679 txn->flags |= TX_HDR_CONN_CLO;
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001680 if (to_del & 1)
1681 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001682 else
1683 txn->flags |= TX_CON_CLO_SET;
1684 }
Willy Tarreau50fc7772012-11-11 22:19:57 +01001685 else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) {
1686 txn->flags |= TX_HDR_CONN_UPG;
1687 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001688 }
1689
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001690 txn->flags |= TX_HDR_CONN_PRS;
1691 return;
1692}
Willy Tarreau5b154472009-12-21 20:11:07 +01001693
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001694/* Apply desired changes on the Connection: header. Values may be removed and/or
1695 * added depending on the <wanted> flags, which are exclusively composed of
1696 * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The
1697 * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left.
1698 */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001699void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, int wanted)
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001700{
1701 struct hdr_ctx ctx;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001702 const char *hdr_val = "Connection";
1703 int hdr_len = 10;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001704
1705 ctx.idx = 0;
1706
Willy Tarreau88d349d2010-01-25 12:15:43 +01001707
1708 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1709 hdr_val = "Proxy-Connection";
1710 hdr_len = 16;
1711 }
1712
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001713 txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET);
Willy Tarreau9b28e032012-10-12 23:49:43 +02001714 while (http_find_header2(hdr_val, hdr_len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001715 if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) {
1716 if (wanted & TX_CON_KAL_SET)
1717 txn->flags |= TX_CON_KAL_SET;
1718 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001719 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau5b154472009-12-21 20:11:07 +01001720 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001721 else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) {
1722 if (wanted & TX_CON_CLO_SET)
1723 txn->flags |= TX_CON_CLO_SET;
1724 else
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001725 http_remove_header2(msg, &txn->hdr_idx, &ctx);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01001726 }
Willy Tarreau5b154472009-12-21 20:11:07 +01001727 }
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001728
1729 if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
1730 return;
1731
1732 if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) {
1733 txn->flags |= TX_CON_CLO_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001734 hdr_val = "Connection: close";
1735 hdr_len = 17;
1736 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1737 hdr_val = "Proxy-Connection: close";
1738 hdr_len = 23;
1739 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001740 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001741 }
1742
1743 if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) {
1744 txn->flags |= TX_CON_KAL_SET;
Willy Tarreau88d349d2010-01-25 12:15:43 +01001745 hdr_val = "Connection: keep-alive";
1746 hdr_len = 22;
1747 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
1748 hdr_val = "Proxy-Connection: keep-alive";
1749 hdr_len = 28;
1750 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01001751 http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len);
Willy Tarreaubbf0b372010-01-18 16:54:40 +01001752 }
1753 return;
Willy Tarreau5b154472009-12-21 20:11:07 +01001754}
1755
Willy Tarreaua458b672012-03-05 11:17:50 +01001756/* Parse the chunk size at msg->next. Once done, it adjusts ->next to point to the
Willy Tarreaud98cf932009-12-27 22:54:55 +01001757 * first byte of body, and increments msg->sov by the number of bytes parsed,
Willy Tarreau26927362012-05-18 23:22:52 +02001758 * so that we know we can forward between ->sol and ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001759 * Return >0 on success, 0 when some data is missing, <0 on error.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001760 * Note: this function is designed to parse wrapped CRLF at the end of the buffer.
Willy Tarreau115acb92009-12-26 13:56:06 +01001761 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001762static inline int http_parse_chunk_size(struct http_msg *msg)
Willy Tarreau115acb92009-12-26 13:56:06 +01001763{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001764 const struct buffer *buf = msg->chn->buf;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001765 const char *ptr = b_ptr(buf, msg->next);
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001766 const char *ptr_old = ptr;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001767 const char *end = buf->data + buf->size;
1768 const char *stop = bi_end(buf);
Willy Tarreau115acb92009-12-26 13:56:06 +01001769 unsigned int chunk = 0;
1770
1771 /* The chunk size is in the following form, though we are only
1772 * interested in the size and CRLF :
1773 * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF
1774 */
1775 while (1) {
1776 int c;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001777 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001778 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001779 c = hex2i(*ptr);
Willy Tarreau115acb92009-12-26 13:56:06 +01001780 if (c < 0) /* not a hex digit anymore */
1781 break;
Willy Tarreau0161d622013-04-02 01:26:55 +02001782 if (unlikely(++ptr >= end))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001783 ptr = buf->data;
Willy Tarreau431946e2012-02-24 19:20:12 +01001784 if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001785 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001786 chunk = (chunk << 4) + c;
1787 }
1788
Willy Tarreaud98cf932009-12-27 22:54:55 +01001789 /* empty size not allowed */
Willy Tarreau0161d622013-04-02 01:26:55 +02001790 if (unlikely(ptr == ptr_old))
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001791 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001792
1793 while (http_is_spht[(unsigned char)*ptr]) {
1794 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001795 ptr = buf->data;
Willy Tarreau0161d622013-04-02 01:26:55 +02001796 if (unlikely(ptr == stop))
Willy Tarreau115acb92009-12-26 13:56:06 +01001797 return 0;
Willy Tarreau115acb92009-12-26 13:56:06 +01001798 }
1799
Willy Tarreaud98cf932009-12-27 22:54:55 +01001800 /* Up to there, we know that at least one byte is present at *ptr. Check
1801 * for the end of chunk size.
1802 */
1803 while (1) {
1804 if (likely(HTTP_IS_CRLF(*ptr))) {
1805 /* we now have a CR or an LF at ptr */
1806 if (likely(*ptr == '\r')) {
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 Tarreaud98cf932009-12-27 22:54:55 +01001810 return 0;
1811 }
Willy Tarreau115acb92009-12-26 13:56:06 +01001812
Willy Tarreaud98cf932009-12-27 22:54:55 +01001813 if (*ptr != '\n')
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001814 goto error;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001815 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001816 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817 /* done */
1818 break;
1819 }
1820 else if (*ptr == ';') {
1821 /* chunk extension, ends at next CRLF */
1822 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001823 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001824 if (ptr == stop)
Willy Tarreau115acb92009-12-26 13:56:06 +01001825 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826
1827 while (!HTTP_IS_CRLF(*ptr)) {
1828 if (++ptr >= end)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001829 ptr = buf->data;
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001830 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001831 return 0;
1832 }
1833 /* we have a CRLF now, loop above */
1834 continue;
Willy Tarreau115acb92009-12-26 13:56:06 +01001835 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001836 else
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001837 goto error;
Willy Tarreau115acb92009-12-26 13:56:06 +01001838 }
1839
Willy Tarreaud98cf932009-12-27 22:54:55 +01001840 /* OK we found our CRLF and now <ptr> points to the next byte,
Willy Tarreaua458b672012-03-05 11:17:50 +01001841 * which may or may not be present. We save that into ->next and
Willy Tarreaud98cf932009-12-27 22:54:55 +01001842 * ->sov.
Willy Tarreau115acb92009-12-26 13:56:06 +01001843 */
Willy Tarreau0161d622013-04-02 01:26:55 +02001844 if (unlikely(ptr < ptr_old))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001845 msg->sov += buf->size;
Willy Tarreaua458b672012-03-05 11:17:50 +01001846 msg->sov += ptr - ptr_old;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001847 msg->next = buffer_count(buf, buf->p, ptr);
Willy Tarreau124d9912011-03-01 20:30:48 +01001848 msg->chunk_len = chunk;
1849 msg->body_len += chunk;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001850 msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS;
Willy Tarreau115acb92009-12-26 13:56:06 +01001851 return 1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001852 error:
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001853 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001854 return -1;
Willy Tarreau115acb92009-12-26 13:56:06 +01001855}
1856
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001857/* This function skips trailers in the buffer associated with HTTP
Willy Tarreaua458b672012-03-05 11:17:50 +01001858 * message <msg>. The first visited position is msg->next. If the end of
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 * the trailers is found, it is automatically scheduled to be forwarded,
1860 * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0.
1861 * If not enough data are available, the function does not change anything
Willy Tarreaua458b672012-03-05 11:17:50 +01001862 * except maybe msg->next and msg->sov if it could parse some lines, and returns
Willy Tarreau638cd022010-01-03 07:42:04 +01001863 * zero. If a parse error is encountered, the function returns < 0 and does not
Willy Tarreaua458b672012-03-05 11:17:50 +01001864 * change anything except maybe msg->next and msg->sov. Note that the message
Willy Tarreau638cd022010-01-03 07:42:04 +01001865 * must already be in HTTP_MSG_TRAILERS state before calling this function,
1866 * which implies that all non-trailers data have already been scheduled for
Willy Tarreau26927362012-05-18 23:22:52 +02001867 * forwarding, and that the difference between msg->sol and msg->sov exactly
Willy Tarreau638cd022010-01-03 07:42:04 +01001868 * matches the length of trailers already parsed and not forwarded. It is also
1869 * important to note that this function is designed to be able to parse wrapped
1870 * headers at end of buffer.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001871 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001872static int http_forward_trailers(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001873{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001874 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001875
Willy Tarreaua458b672012-03-05 11:17:50 +01001876 /* we have msg->next which points to next line. Look for CRLF. */
Willy Tarreaud98cf932009-12-27 22:54:55 +01001877 while (1) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001878 const char *p1 = NULL, *p2 = NULL;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001879 const char *ptr = b_ptr(buf, msg->next);
1880 const char *stop = bi_end(buf);
Willy Tarreau638cd022010-01-03 07:42:04 +01001881 int bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001882
1883 /* scan current line and stop at LF or CRLF */
1884 while (1) {
Willy Tarreau363a5bb2012-03-02 20:14:45 +01001885 if (ptr == stop)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001886 return 0;
1887
1888 if (*ptr == '\n') {
1889 if (!p1)
1890 p1 = ptr;
1891 p2 = ptr;
1892 break;
1893 }
1894
1895 if (*ptr == '\r') {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001896 if (p1) {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001897 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001898 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001899 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900 p1 = ptr;
1901 }
1902
1903 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001904 if (ptr >= buf->data + buf->size)
1905 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001906 }
1907
1908 /* after LF; point to beginning of next line */
1909 p2++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001910 if (p2 >= buf->data + buf->size)
1911 p2 = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001912
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001913 bytes = p2 - b_ptr(buf, msg->next);
Willy Tarreau638cd022010-01-03 07:42:04 +01001914 if (bytes < 0)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001915 bytes += buf->size;
Willy Tarreau638cd022010-01-03 07:42:04 +01001916
1917 /* schedule this line for forwarding */
1918 msg->sov += bytes;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001919 if (msg->sov >= buf->size)
1920 msg->sov -= buf->size;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001921
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001922 if (p1 == b_ptr(buf, msg->next)) {
Willy Tarreau638cd022010-01-03 07:42:04 +01001923 /* LF/CRLF at beginning of line => end of trailers at p2.
1924 * Everything was scheduled for forwarding, there's nothing
1925 * left from this message.
Willy Tarreau5523b322009-12-29 12:05:52 +01001926 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001927 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001928 msg->msg_state = HTTP_MSG_DONE;
1929 return 1;
1930 }
1931 /* OK, next line then */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001932 msg->next = buffer_count(buf, buf->p, p2);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001933 }
1934}
1935
Willy Tarreau54d23df2012-10-25 19:04:45 +02001936/* This function may be called only in HTTP_MSG_CHUNK_CRLF. It reads the CRLF or
Willy Tarreaud98cf932009-12-27 22:54:55 +01001937 * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov,
Willy Tarreau26927362012-05-18 23:22:52 +02001938 * ->sol, ->next in order to include this part into the next forwarding phase.
Willy Tarreaua458b672012-03-05 11:17:50 +01001939 * Note that the caller must ensure that ->p points to the first byte to parse.
Willy Tarreaud98cf932009-12-27 22:54:55 +01001940 * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If
1941 * not enough data are available, the function does not change anything and
1942 * returns zero. If a parse error is encountered, the function returns < 0 and
1943 * does not change anything. Note: this function is designed to parse wrapped
1944 * CRLF at the end of the buffer.
1945 */
Willy Tarreau24e6d972012-10-26 00:49:52 +02001946static inline int http_skip_chunk_crlf(struct http_msg *msg)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001947{
Willy Tarreau9b28e032012-10-12 23:49:43 +02001948 const struct buffer *buf = msg->chn->buf;
Willy Tarreau4baf44b2012-03-09 14:10:20 +01001949 const char *ptr;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001950 int bytes;
1951
1952 /* NB: we'll check data availabilty at the end. It's not a
1953 * problem because whatever we match first will be checked
1954 * against the correct length.
1955 */
1956 bytes = 1;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001957 ptr = buf->p;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001958 if (*ptr == '\r') {
1959 bytes++;
1960 ptr++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001961 if (ptr >= buf->data + buf->size)
1962 ptr = buf->data;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001963 }
1964
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001965 if (bytes > buf->i)
Willy Tarreaud98cf932009-12-27 22:54:55 +01001966 return 0;
1967
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001968 if (*ptr != '\n') {
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001969 msg->err_pos = buffer_count(buf, buf->p, ptr);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001970 return -1;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01001971 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001972
1973 ptr++;
Willy Tarreau0161d622013-04-02 01:26:55 +02001974 if (unlikely(ptr >= buf->data + buf->size))
Willy Tarreaucdbdd522012-10-12 22:51:15 +02001975 ptr = buf->data;
Willy Tarreau26927362012-05-18 23:22:52 +02001976 /* prepare the CRLF to be forwarded (between ->sol and ->sov) */
1977 msg->sol = 0;
Willy Tarreauea1175a2012-03-05 15:52:30 +01001978 msg->sov = msg->next = bytes;
Willy Tarreaud98cf932009-12-27 22:54:55 +01001979 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
1980 return 1;
1981}
Willy Tarreau5b154472009-12-21 20:11:07 +01001982
William Lallemand82fe75c2012-10-23 10:25:10 +02001983
1984/*
1985 * Selects a compression algorithm depending on the client request.
Willy Tarreau05d84602012-10-26 02:11:25 +02001986 */
William Lallemand82fe75c2012-10-23 10:25:10 +02001987int select_compression_request_header(struct session *s, struct buffer *req)
1988{
1989 struct http_txn *txn = &s->txn;
Willy Tarreau70737d12012-10-27 00:34:28 +02001990 struct http_msg *msg = &txn->req;
William Lallemand82fe75c2012-10-23 10:25:10 +02001991 struct hdr_ctx ctx;
1992 struct comp_algo *comp_algo = NULL;
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02001993 struct comp_algo *comp_algo_back = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02001994
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01001995 /* Disable compression for older user agents announcing themselves as "Mozilla/4"
1996 * unless they are known good (MSIE 6 with XP SP2, or MSIE 7 and later).
Willy Tarreau05d84602012-10-26 02:11:25 +02001997 * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details.
1998 */
1999 ctx.idx = 0;
2000 if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) &&
2001 ctx.vlen >= 9 &&
Finn Arne Gangstadcbb9a4b2012-10-29 21:43:01 +01002002 memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 &&
2003 (ctx.vlen < 31 ||
2004 memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 ||
2005 ctx.line[ctx.val + 30] < '6' ||
2006 (ctx.line[ctx.val + 30] == '6' &&
2007 (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) {
2008 s->comp_algo = NULL;
2009 return 0;
Willy Tarreau05d84602012-10-26 02:11:25 +02002010 }
2011
William Lallemand82fe75c2012-10-23 10:25:10 +02002012 /* search for the algo in the backend in priority or the frontend */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002013 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 +02002014 ctx.idx = 0;
2015 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002016 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002017 if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) {
2018 s->comp_algo = comp_algo;
Willy Tarreau70737d12012-10-27 00:34:28 +02002019
2020 /* remove all occurrences of the header when "compression offload" is set */
2021
2022 if ((s->be->comp && s->be->comp->offload) ||
2023 (s->fe->comp && s->fe->comp->offload)) {
2024 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2025 ctx.idx = 0;
2026 while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) {
2027 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2028 }
2029 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002030 return 1;
2031 }
2032 }
2033 }
2034 }
2035
2036 /* identity is implicit does not require headers */
Willy Tarreau3c7b97b2012-10-26 14:50:26 +02002037 if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) {
2038 for (comp_algo = comp_algo_back; comp_algo; comp_algo = comp_algo->next) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002039 if (comp_algo->add_data == identity_add_data) {
2040 s->comp_algo = comp_algo;
2041 return 1;
2042 }
2043 }
2044 }
2045
2046 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002047 return 0;
2048}
2049
2050/*
2051 * Selects a comression algorithm depending of the server response.
2052 */
2053int select_compression_response_header(struct session *s, struct buffer *res)
2054{
2055 struct http_txn *txn = &s->txn;
2056 struct http_msg *msg = &txn->rsp;
2057 struct hdr_ctx ctx;
2058 struct comp_type *comp_type;
William Lallemand82fe75c2012-10-23 10:25:10 +02002059
2060 /* no common compression algorithm was found in request header */
2061 if (s->comp_algo == NULL)
2062 goto fail;
2063
2064 /* HTTP < 1.1 should not be compressed */
2065 if (!(msg->flags & HTTP_MSGF_VER_11))
2066 goto fail;
2067
William Lallemandd3002612012-11-26 14:34:47 +01002068 /* 200 only */
2069 if (txn->status != 200)
2070 goto fail;
2071
William Lallemand82fe75c2012-10-23 10:25:10 +02002072 /* Content-Length is null */
2073 if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
2074 goto fail;
2075
Willy Tarreau667c2a32013-04-09 08:13:58 +02002076 /* TEMPORARY WORKAROUND: do not compress if response is chunked !!!!!! */
2077 if (msg->flags & HTTP_MSGF_TE_CHNK)
2078 goto fail;
2079
William Lallemand82fe75c2012-10-23 10:25:10 +02002080 /* content is already compressed */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002081 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002082 if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
2083 goto fail;
2084
Willy Tarreau56e9ffa2013-01-05 16:20:35 +01002085 /* no compression when Cache-Control: no-transform is present in the message */
2086 ctx.idx = 0;
2087 while (http_find_header2("Cache-Control", 13, res->p, &txn->hdr_idx, &ctx)) {
2088 if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12))
2089 goto fail;
2090 }
2091
William Lallemand82fe75c2012-10-23 10:25:10 +02002092 comp_type = NULL;
2093
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002094 /* we don't want to compress multipart content-types, nor content-types that are
2095 * not listed in the "compression type" directive if any. If no content-type was
2096 * found but configuration requires one, we don't compress either. Backend has
2097 * the priority.
William Lallemand82fe75c2012-10-23 10:25:10 +02002098 */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002099 ctx.idx = 0;
2100 if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
2101 if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
2102 goto fail;
2103
2104 if ((s->be->comp && (comp_type = s->be->comp->types)) ||
2105 (s->fe->comp && (comp_type = s->fe->comp->types))) {
William Lallemand82fe75c2012-10-23 10:25:10 +02002106 for (; comp_type; comp_type = comp_type->next) {
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002107 if (ctx.vlen >= comp_type->name_len &&
2108 strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
William Lallemand82fe75c2012-10-23 10:25:10 +02002109 /* this Content-Type should be compressed */
2110 break;
2111 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002112 /* this Content-Type should not be compressed */
2113 if (comp_type == NULL)
2114 goto fail;
William Lallemand82fe75c2012-10-23 10:25:10 +02002115 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002116 }
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002117 else { /* no content-type header */
2118 if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
2119 goto fail; /* a content-type was required */
William Lallemandd3002612012-11-26 14:34:47 +01002120 }
2121
William Lallemandd85f9172012-11-09 17:05:39 +01002122 /* limit compression rate */
2123 if (global.comp_rate_lim > 0)
2124 if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim)
2125 goto fail;
2126
William Lallemand072a2bf2012-11-20 17:01:01 +01002127 /* limit cpu usage */
2128 if (idle_pct < compress_min_idle)
2129 goto fail;
2130
William Lallemand4c49fae2012-11-07 15:00:23 +01002131 /* initialize compression */
William Lallemandf3747832012-11-09 12:33:10 +01002132 if (s->comp_algo->init(&s->comp_ctx, global.tune.comp_maxlevel) < 0)
William Lallemand4c49fae2012-11-07 15:00:23 +01002133 goto fail;
2134
William Lallemandec3e3892012-11-12 17:02:18 +01002135 s->flags |= SN_COMP_READY;
2136
William Lallemand82fe75c2012-10-23 10:25:10 +02002137 /* remove Content-Length header */
Willy Tarreau0a80a8d2012-11-26 16:33:37 +01002138 ctx.idx = 0;
William Lallemand82fe75c2012-10-23 10:25:10 +02002139 if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
2140 http_remove_header2(msg, &txn->hdr_idx, &ctx);
2141
2142 /* add Transfer-Encoding header */
2143 if (!(msg->flags & HTTP_MSGF_TE_CHNK))
2144 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26);
2145
2146 /*
2147 * Add Content-Encoding header when it's not identity encoding.
2148 * RFC 2616 : Identity encoding: This content-coding is used only in the
2149 * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding
2150 * header.
2151 */
2152 if (s->comp_algo->add_data != identity_add_data) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002153 trash.len = 18;
2154 memcpy(trash.str, "Content-Encoding: ", trash.len);
2155 memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len);
2156 trash.len += s->comp_algo->name_len;
2157 trash.str[trash.len] = '\0';
2158 http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len);
William Lallemand82fe75c2012-10-23 10:25:10 +02002159 }
William Lallemand82fe75c2012-10-23 10:25:10 +02002160 return 1;
2161
2162fail:
Willy Tarreaub97b6192012-11-19 14:55:02 +01002163 s->comp_algo = NULL;
William Lallemand82fe75c2012-10-23 10:25:10 +02002164 return 0;
2165}
2166
2167
Willy Tarreaud787e662009-07-07 10:14:51 +02002168/* This stream analyser waits for a complete HTTP request. It returns 1 if the
2169 * processing can continue on next analysers, or zero if it either needs more
2170 * data or wants to immediately abort the request (eg: timeout, error, ...). It
2171 * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers
2172 * when it has nothing left to do, and may remove any analyser when it wants to
2173 * abort.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002174 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002175int http_wait_for_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002176{
Willy Tarreau59234e92008-11-30 23:51:27 +01002177 /*
2178 * We will parse the partial (or complete) lines.
2179 * We will check the request syntax, and also join multi-line
2180 * headers. An index of all the lines will be elaborated while
2181 * parsing.
2182 *
2183 * For the parsing, we use a 28 states FSM.
2184 *
2185 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02002186 * req->buf->p = beginning of request
2187 * req->buf->p + msg->eoh = end of processed headers / start of current one
2188 * req->buf->p + req->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02002189 * msg->eol = end of current header or line (LF or CRLF)
2190 * msg->next = first non-visited byte
Willy Tarreaud787e662009-07-07 10:14:51 +02002191 *
2192 * At end of parsing, we may perform a capture of the error (if any), and
2193 * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE).
2194 * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and
2195 * finally headers capture.
Willy Tarreau59234e92008-11-30 23:51:27 +01002196 */
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002197
Willy Tarreau59234e92008-11-30 23:51:27 +01002198 int cur_idx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002199 int use_close_only;
Willy Tarreau59234e92008-11-30 23:51:27 +01002200 struct http_txn *txn = &s->txn;
2201 struct http_msg *msg = &txn->req;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002202 struct hdr_ctx ctx;
Willy Tarreau976f1ee2006-12-17 10:06:03 +01002203
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01002204 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 +01002205 now_ms, __FUNCTION__,
2206 s,
2207 req,
2208 req->rex, req->wex,
2209 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02002210 req->buf->i,
Willy Tarreau6bf17362009-02-24 10:48:35 +01002211 req->analysers);
2212
Willy Tarreau52a0c602009-08-16 22:45:38 +02002213 /* we're speaking HTTP here, so let's speak HTTP to the client */
2214 s->srv_error = http_return_srv_error;
2215
Willy Tarreau83e3af02009-12-28 17:39:57 +01002216 /* There's a protected area at the end of the buffer for rewriting
2217 * purposes. We don't want to start to parse the request if the
2218 * protected area is affected, because we may have to move processed
2219 * data later, which is much more complicated.
2220 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002221 if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02002222 if (txn->flags & TX_NOT_FIRST) {
2223 if (unlikely(!channel_reserved(req))) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002224 if (req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002225 goto failed_keep_alive;
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002226 /* some data has still not left the buffer, wake us once that's done */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002227 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002228 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
Willy Tarreau2ab6eb12010-01-02 22:04:45 +01002229 return 0;
2230 }
Willy Tarreau379357a2013-06-08 12:55:46 +02002231 if (unlikely(bi_end(req->buf) < b_ptr(req->buf, msg->next) ||
2232 bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite))
2233 buffer_slow_realign(req->buf);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002234 }
2235
Willy Tarreau065e8332010-01-08 00:30:20 +01002236 /* Note that we have the same problem with the response ; we
2237 * may want to send a redirect, error or anything which requires
2238 * some spare space. So we'll ensure that we have at least
2239 * maxrewrite bytes available in the response buffer before
2240 * processing that one. This will only affect pipelined
2241 * keep-alive requests.
2242 */
2243 if ((txn->flags & TX_NOT_FIRST) &&
Willy Tarreau379357a2013-06-08 12:55:46 +02002244 unlikely(!channel_reserved(s->rep) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02002245 bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) ||
2246 bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) {
2247 if (s->rep->buf->o) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002248 if (s->rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
Willy Tarreau64648412010-03-05 10:41:54 +01002249 goto failed_keep_alive;
Willy Tarreau065e8332010-01-08 00:30:20 +01002250 /* don't let a connection request be initiated */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002251 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002252 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau0499e352010-12-17 07:13:42 +01002253 s->rep->analysers |= an_bit; /* wake us up once it changes */
Willy Tarreau065e8332010-01-08 00:30:20 +01002254 return 0;
2255 }
2256 }
2257
Willy Tarreau9b28e032012-10-12 23:49:43 +02002258 if (likely(msg->next < req->buf->i)) /* some unparsed data are available */
Willy Tarreaua560c212012-03-09 13:50:57 +01002259 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01002260 }
2261
Willy Tarreau59234e92008-11-30 23:51:27 +01002262 /* 1: we might have to print this header in debug mode */
2263 if (unlikely((global.mode & MODE_DEBUG) &&
2264 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01002265 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002266 char *eol, *sol;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002267
Willy Tarreau9b28e032012-10-12 23:49:43 +02002268 sol = req->buf->p;
Willy Tarreaue92693a2012-09-24 21:13:39 +02002269 /* this is a bit complex : in case of error on the request line,
2270 * we know that rq.l is still zero, so we display only the part
2271 * up to the end of the line (truncated by debug_hdr).
2272 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002273 eol = sol + (msg->sl.rq.l ? msg->sl.rq.l : req->buf->i);
Willy Tarreau59234e92008-11-30 23:51:27 +01002274 debug_hdr("clireq", s, sol, eol);
Willy Tarreau45e73e32006-12-17 00:05:15 +01002275
Willy Tarreau59234e92008-11-30 23:51:27 +01002276 sol += hdr_idx_first_pos(&txn->hdr_idx);
2277 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01002278
Willy Tarreau59234e92008-11-30 23:51:27 +01002279 while (cur_idx) {
2280 eol = sol + txn->hdr_idx.v[cur_idx].len;
2281 debug_hdr("clihdr", s, sol, eol);
2282 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
2283 cur_idx = txn->hdr_idx.v[cur_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002284 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002285 }
2286
Willy Tarreau58f10d72006-12-04 02:26:12 +01002287
Willy Tarreau59234e92008-11-30 23:51:27 +01002288 /*
2289 * Now we quickly check if we have found a full valid request.
2290 * If not so, we check the FD and buffer states before leaving.
2291 * A full request is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01002292 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002293 * requests are checked first. When waiting for a second request
2294 * on a keep-alive session, if we encounter and error, close, t/o,
2295 * we note the error in the session flags but don't set any state.
2296 * Since the error will be noted there, it will not be counted by
2297 * process_session() as a frontend error.
Willy Tarreauda7ff642010-06-23 11:44:09 +02002298 * Last, we may increase some tracked counters' http request errors on
2299 * the cases that are deliberately the client's fault. For instance,
2300 * a timeout or connection reset is not counted as an error. However
2301 * a bad request is.
Willy Tarreau59234e92008-11-30 23:51:27 +01002302 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01002303
Willy Tarreau655dce92009-11-08 13:10:58 +01002304 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002305 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002306 * First, let's catch bad requests.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002307 */
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002308 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02002309 session_inc_http_req_ctr(s);
2310 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002311 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002312 goto return_bad_req;
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002313 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002314
Willy Tarreau59234e92008-11-30 23:51:27 +01002315 /* 1: Since we are in header mode, if there's no space
2316 * left for headers, we won't be able to free more
2317 * later, so the session will never terminate. We
2318 * must terminate it now.
2319 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002320 if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) {
Willy Tarreau59234e92008-11-30 23:51:27 +01002321 /* FIXME: check if URI is set and return Status
2322 * 414 Request URI too long instead.
Willy Tarreau58f10d72006-12-04 02:26:12 +01002323 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002324 session_inc_http_req_ctr(s);
2325 session_inc_http_err_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002326 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreaufec4d892011-09-02 20:04:57 +02002327 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02002328 msg->err_pos = req->buf->i;
Willy Tarreau59234e92008-11-30 23:51:27 +01002329 goto return_bad_req;
2330 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002331
Willy Tarreau59234e92008-11-30 23:51:27 +01002332 /* 2: have we encountered a read error ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002333 else if (req->flags & CF_READ_ERROR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002334 if (!(s->flags & SN_ERR_MASK))
2335 s->flags |= SN_ERR_CLICL;
2336
Willy Tarreaufcffa692010-01-10 14:21:19 +01002337 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002338 goto failed_keep_alive;
2339
Willy Tarreau59234e92008-11-30 23:51:27 +01002340 /* we cannot return any message on error */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002341 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002342 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002343 session_inc_http_err_ctr(s);
2344 }
2345
Willy Tarreaudc979f22012-12-04 10:39:01 +01002346 txn->status = 400;
2347 stream_int_retnclose(req->prod, NULL);
Willy Tarreau59234e92008-11-30 23:51:27 +01002348 msg->msg_state = HTTP_MSG_ERROR;
2349 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002350
Willy Tarreauda7ff642010-06-23 11:44:09 +02002351 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002352 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002353 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002354 if (s->listener->counters)
2355 s->listener->counters->failed_req++;
2356
Willy Tarreau59234e92008-11-30 23:51:27 +01002357 if (!(s->flags & SN_FINST_MASK))
2358 s->flags |= SN_FINST_R;
2359 return 0;
2360 }
Willy Tarreauf9839bd2008-08-27 23:57:16 +02002361
Willy Tarreau59234e92008-11-30 23:51:27 +01002362 /* 3: has the read timeout expired ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002363 else if (req->flags & CF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002364 if (!(s->flags & SN_ERR_MASK))
2365 s->flags |= SN_ERR_CLITO;
2366
Willy Tarreaufcffa692010-01-10 14:21:19 +01002367 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002368 goto failed_keep_alive;
2369
Willy Tarreau59234e92008-11-30 23:51:27 +01002370 /* read timeout : give up with an error message. */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002371 if (msg->err_pos >= 0) {
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002372 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002373 session_inc_http_err_ctr(s);
2374 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002375 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02002376 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau59234e92008-11-30 23:51:27 +01002377 msg->msg_state = HTTP_MSG_ERROR;
2378 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002379
Willy Tarreauda7ff642010-06-23 11:44:09 +02002380 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002381 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002382 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002383 if (s->listener->counters)
2384 s->listener->counters->failed_req++;
2385
Willy Tarreau59234e92008-11-30 23:51:27 +01002386 if (!(s->flags & SN_FINST_MASK))
2387 s->flags |= SN_FINST_R;
2388 return 0;
2389 }
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02002390
Willy Tarreau59234e92008-11-30 23:51:27 +01002391 /* 4: have we encountered a close ? */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002392 else if (req->flags & CF_SHUTR) {
Willy Tarreaud3c343f2010-01-16 10:26:19 +01002393 if (!(s->flags & SN_ERR_MASK))
2394 s->flags |= SN_ERR_CLICL;
2395
Willy Tarreaufcffa692010-01-10 14:21:19 +01002396 if (txn->flags & TX_WAIT_NEXT_RQ)
Willy Tarreaub608feb2010-01-02 22:47:18 +01002397 goto failed_keep_alive;
2398
Willy Tarreau4076a152009-04-02 15:18:36 +02002399 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002400 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau59234e92008-11-30 23:51:27 +01002401 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002402 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau59234e92008-11-30 23:51:27 +01002403 msg->msg_state = HTTP_MSG_ERROR;
2404 req->analysers = 0;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002405
Willy Tarreauda7ff642010-06-23 11:44:09 +02002406 session_inc_http_err_ctr(s);
2407 session_inc_http_req_ctr(s);
Willy Tarreau3e1b6d12010-03-04 23:02:38 +01002408 proxy_inc_fe_req_ctr(s->fe);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002409 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002410 if (s->listener->counters)
2411 s->listener->counters->failed_req++;
2412
Willy Tarreau59234e92008-11-30 23:51:27 +01002413 if (!(s->flags & SN_FINST_MASK))
2414 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02002415 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002416 }
2417
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002418 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002419 req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
2420 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau5e205522011-12-17 16:34:27 +01002421#ifdef TCP_QUICKACK
Willy Tarreau9b28e032012-10-12 23:49:43 +02002422 if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) {
Willy Tarreau5e205522011-12-17 16:34:27 +01002423 /* We need more data, we have to re-enable quick-ack in case we
2424 * previously disabled it, otherwise we might cause the client
2425 * to delay next data.
2426 */
Willy Tarreau7f7ad912012-11-11 19:27:15 +01002427 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01002428 }
2429#endif
Willy Tarreau1b194fe2009-03-21 21:10:04 +01002430
Willy Tarreaufcffa692010-01-10 14:21:19 +01002431 if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) {
2432 /* If the client starts to talk, let's fall back to
2433 * request timeout processing.
2434 */
2435 txn->flags &= ~TX_WAIT_NEXT_RQ;
Willy Tarreaub16a5742010-01-10 14:46:16 +01002436 req->analyse_exp = TICK_ETERNITY;
Willy Tarreaufcffa692010-01-10 14:21:19 +01002437 }
2438
Willy Tarreau59234e92008-11-30 23:51:27 +01002439 /* just set the request timeout once at the beginning of the request */
Willy Tarreaub16a5742010-01-10 14:46:16 +01002440 if (!tick_isset(req->analyse_exp)) {
2441 if ((msg->msg_state == HTTP_MSG_RQBEFORE) &&
2442 (txn->flags & TX_WAIT_NEXT_RQ) &&
2443 tick_isset(s->be->timeout.httpka))
2444 req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka);
2445 else
2446 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
2447 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002448
Willy Tarreau59234e92008-11-30 23:51:27 +01002449 /* we're not ready yet */
2450 return 0;
Willy Tarreaub608feb2010-01-02 22:47:18 +01002451
2452 failed_keep_alive:
2453 /* Here we process low-level errors for keep-alive requests. In
2454 * short, if the request is not the first one and it experiences
2455 * a timeout, read error or shutdown, we just silently close so
2456 * that the client can try again.
2457 */
2458 txn->status = 0;
2459 msg->msg_state = HTTP_MSG_RQBEFORE;
2460 req->analysers = 0;
2461 s->logs.logwait = 0;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002462 s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */
Willy Tarreau148d0992010-01-10 10:21:21 +01002463 stream_int_retnclose(req->prod, NULL);
Willy Tarreaub608feb2010-01-02 22:47:18 +01002464 return 0;
Willy Tarreau59234e92008-11-30 23:51:27 +01002465 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01002466
Willy Tarreaud787e662009-07-07 10:14:51 +02002467 /* OK now we have a complete HTTP request with indexed headers. Let's
2468 * complete the request parsing by setting a few fields we will need
Willy Tarreau9b28e032012-10-12 23:49:43 +02002469 * later. At this point, we have the last CRLF at req->buf->data + msg->eoh.
Willy Tarreaufa355d42009-11-29 18:12:29 +01002470 * If the request is in HTTP/0.9 form, the rule is still true, and eoh
Willy Tarreaua458b672012-03-05 11:17:50 +01002471 * points to the CRLF of the request line. msg->next points to the first
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01002472 * byte after the last LF. msg->sov points to the first byte of data.
2473 * msg->eol cannot be trusted because it may have been left uninitialized
2474 * (for instance in the absence of headers).
Willy Tarreaud787e662009-07-07 10:14:51 +02002475 */
Willy Tarreau9cdde232007-05-02 20:58:19 +02002476
Willy Tarreauda7ff642010-06-23 11:44:09 +02002477 session_inc_http_req_ctr(s);
Willy Tarreaud9b587f2010-02-26 10:05:55 +01002478 proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */
2479
Willy Tarreaub16a5742010-01-10 14:46:16 +01002480 if (txn->flags & TX_WAIT_NEXT_RQ) {
2481 /* kill the pending keep-alive timeout */
2482 txn->flags &= ~TX_WAIT_NEXT_RQ;
2483 req->analyse_exp = TICK_ETERNITY;
2484 }
2485
2486
Willy Tarreaud787e662009-07-07 10:14:51 +02002487 /* Maybe we found in invalid header name while we were configured not
2488 * to block on that, so we have to capture it now.
2489 */
2490 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002491 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02002492
Willy Tarreau59234e92008-11-30 23:51:27 +01002493 /*
2494 * 1: identify the method
2495 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02002496 txn->meth = find_http_meth(req->buf->p, msg->sl.rq.m_l);
Willy Tarreau59234e92008-11-30 23:51:27 +01002497
2498 /* we can make use of server redirect on GET and HEAD */
2499 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
2500 s->flags |= SN_REDIRECTABLE;
Willy Tarreaufa7e1022008-10-19 07:30:41 +02002501
Willy Tarreau59234e92008-11-30 23:51:27 +01002502 /*
2503 * 2: check if the URI matches the monitor_uri.
2504 * We have to do this for every request which gets in, because
2505 * the monitor-uri is defined by the frontend.
2506 */
2507 if (unlikely((s->fe->monitor_uri_len != 0) &&
2508 (s->fe->monitor_uri_len == msg->sl.rq.u_l) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002509 !memcmp(req->buf->p + msg->sl.rq.u,
Willy Tarreau59234e92008-11-30 23:51:27 +01002510 s->fe->monitor_uri,
2511 s->fe->monitor_uri_len))) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01002512 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01002513 * We have found the monitor URI
Willy Tarreau58f10d72006-12-04 02:26:12 +01002514 */
Willy Tarreau59234e92008-11-30 23:51:27 +01002515 struct acl_cond *cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002516
Willy Tarreau59234e92008-11-30 23:51:27 +01002517 s->flags |= SN_MONITOR;
Willy Tarreaueabea072011-09-10 23:29:44 +02002518 s->fe->fe_counters.intercepted_req++;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002519
Willy Tarreau59234e92008-11-30 23:51:27 +01002520 /* Check if we want to fail this monitor request or not */
Willy Tarreaud787e662009-07-07 10:14:51 +02002521 list_for_each_entry(cond, &s->fe->mon_fail_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02002522 int ret = acl_exec_cond(cond, s->fe, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau11382812008-07-09 16:18:21 +02002523
Willy Tarreau59234e92008-11-30 23:51:27 +01002524 ret = acl_pass(ret);
2525 if (cond->pol == ACL_COND_UNLESS)
2526 ret = !ret;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002527
Willy Tarreau59234e92008-11-30 23:51:27 +01002528 if (ret) {
2529 /* we fail this request, let's return 503 service unavail */
2530 txn->status = 503;
Willy Tarreau783f2582012-09-04 12:19:04 +02002531 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503));
Willy Tarreau59234e92008-11-30 23:51:27 +01002532 goto return_prx_cond;
Willy Tarreaub80c2302007-11-30 20:51:32 +01002533 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002534 }
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002535
Willy Tarreau59234e92008-11-30 23:51:27 +01002536 /* nothing to fail, let's reply normaly */
2537 txn->status = 200;
Willy Tarreau783f2582012-09-04 12:19:04 +02002538 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200));
Willy Tarreau59234e92008-11-30 23:51:27 +01002539 goto return_prx_cond;
2540 }
2541
2542 /*
2543 * 3: Maybe we have to copy the original REQURI for the logs ?
2544 * Note: we cannot log anymore if the request has been
2545 * classified as invalid.
2546 */
2547 if (unlikely(s->logs.logwait & LW_REQ)) {
2548 /* we have a complete HTTP request that we must log */
2549 if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) {
2550 int urilen = msg->sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002551
Willy Tarreau59234e92008-11-30 23:51:27 +01002552 if (urilen >= REQURI_LEN)
2553 urilen = REQURI_LEN - 1;
Willy Tarreau9b28e032012-10-12 23:49:43 +02002554 memcpy(txn->uri, req->buf->p, urilen);
Willy Tarreau59234e92008-11-30 23:51:27 +01002555 txn->uri[urilen] = 0;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002556
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002557 if (!(s->logs.logwait &= ~(LW_REQ|LW_INIT)))
Willy Tarreau59234e92008-11-30 23:51:27 +01002558 s->do_log(s);
2559 } else {
2560 Alert("HTTP logging : out of memory.\n");
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01002561 }
Willy Tarreau59234e92008-11-30 23:51:27 +01002562 }
Willy Tarreau06619262006-12-17 08:37:22 +01002563
Willy Tarreaud79a3b22012-12-28 09:40:16 +01002564 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
2565 s->unique_id = pool_alloc2(pool2_uniqueid);
William Lallemanda73203e2012-03-12 12:48:57 +01002566
Willy Tarreau59234e92008-11-30 23:51:27 +01002567 /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */
Willy Tarreau418bfcc2012-03-09 13:56:20 +01002568 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn))
Willy Tarreau2492d5b2009-07-11 00:06:00 +02002569 goto return_bad_req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01002570
Willy Tarreau5b154472009-12-21 20:11:07 +01002571 /* ... and check if the request is HTTP/1.1 or above */
2572 if ((msg->sl.rq.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002573 ((req->buf->p[msg->sl.rq.v + 5] > '1') ||
2574 ((req->buf->p[msg->sl.rq.v + 5] == '1') &&
2575 (req->buf->p[msg->sl.rq.v + 7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002576 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01002577
2578 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01002579 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 +01002580
Willy Tarreau88d349d2010-01-25 12:15:43 +01002581 /* if the frontend has "option http-use-proxy-header", we'll check if
2582 * we have what looks like a proxied connection instead of a connection,
2583 * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection.
2584 * Note that this is *not* RFC-compliant, however browsers and proxies
2585 * happen to do that despite being non-standard :-(
2586 * We consider that a request not beginning with either '/' or '*' is
2587 * a proxied connection, which covers both "scheme://location" and
2588 * CONNECT ip:port.
2589 */
2590 if ((s->fe->options2 & PR_O2_USE_PXHDR) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002591 req->buf->p[msg->sl.rq.u] != '/' && req->buf->p[msg->sl.rq.u] != '*')
Willy Tarreau88d349d2010-01-25 12:15:43 +01002592 txn->flags |= TX_USE_PX_CONN;
2593
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002594 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002595 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002596
Willy Tarreau59234e92008-11-30 23:51:27 +01002597 /* 5: we may need to capture headers */
Willy Tarreau42f7d892012-03-24 08:28:09 +01002598 if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02002599 capture_headers(req->buf->p, &txn->hdr_idx,
Willy Tarreau59234e92008-11-30 23:51:27 +01002600 txn->req.cap, s->fe->req_cap);
Willy Tarreau11382812008-07-09 16:18:21 +02002601
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002602 /* 6: determine the transfer-length.
2603 * According to RFC2616 #4.4, amended by the HTTPbis working group,
2604 * the presence of a message-body in a REQUEST and its transfer length
2605 * must be determined that way (in order of precedence) :
2606 * 1. The presence of a message-body in a request is signaled by the
2607 * inclusion of a Content-Length or Transfer-Encoding header field
2608 * in the request's header fields. When a request message contains
2609 * both a message-body of non-zero length and a method that does
2610 * not define any semantics for that request message-body, then an
2611 * origin server SHOULD either ignore the message-body or respond
2612 * with an appropriate error message (e.g., 413). A proxy or
2613 * gateway, when presented the same request, SHOULD either forward
2614 * the request inbound with the message- body or ignore the
2615 * message-body when determining a response.
2616 *
2617 * 2. If a Transfer-Encoding header field (Section 9.7) is present
2618 * and the "chunked" transfer-coding (Section 6.2) is used, the
2619 * transfer-length is defined by the use of this transfer-coding.
2620 * If a Transfer-Encoding header field is present and the "chunked"
2621 * transfer-coding is not present, the transfer-length is defined
2622 * by the sender closing the connection.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002623 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002624 * 3. If a Content-Length header field is present, its decimal value in
2625 * OCTETs represents both the entity-length and the transfer-length.
2626 * If a message is received with both a Transfer-Encoding header
2627 * field and a Content-Length header field, the latter MUST be ignored.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002628 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002629 * 4. By the server closing the connection. (Closing the connection
2630 * cannot be used to indicate the end of a request body, since that
2631 * would leave no possibility for the server to send back a response.)
2632 *
2633 * Whenever a transfer-coding is applied to a message-body, the set of
2634 * transfer-codings MUST include "chunked", unless the message indicates
2635 * it is terminated by closing the connection. When the "chunked"
2636 * transfer-coding is used, it MUST be the last transfer-coding applied
2637 * to the message-body.
Willy Tarreau32b47f42009-10-18 20:55:02 +02002638 */
2639
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002640 use_close_only = 0;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002641 ctx.idx = 0;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002642 /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002643 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002644 http_find_header2("Transfer-Encoding", 17, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002645 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002646 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
2647 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002648 /* bad transfer-encoding (chunked followed by something else) */
2649 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002650 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002651 break;
2652 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002653 }
2654
Willy Tarreau32b47f42009-10-18 20:55:02 +02002655 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002656 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02002657 http_find_header2("Content-Length", 14, req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau32b47f42009-10-18 20:55:02 +02002658 signed long long cl;
2659
Willy Tarreauad14f752011-09-02 20:33:27 +02002660 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002661 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002662 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002663 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002664
Willy Tarreauad14f752011-09-02 20:33:27 +02002665 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002666 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002667 goto return_bad_req; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02002668 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002669
Willy Tarreauad14f752011-09-02 20:33:27 +02002670 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002671 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002672 goto return_bad_req;
Willy Tarreauad14f752011-09-02 20:33:27 +02002673 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002674
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002675 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02002676 msg->err_pos = ctx.line + ctx.val - req->buf->p;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002677 goto return_bad_req; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02002678 }
Willy Tarreau32b47f42009-10-18 20:55:02 +02002679
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002680 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01002681 msg->body_len = msg->chunk_len = cl;
Willy Tarreau32b47f42009-10-18 20:55:02 +02002682 }
2683
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002684 /* bodyless requests have a known length */
2685 if (!use_close_only)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01002686 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01002687
Willy Tarreaud787e662009-07-07 10:14:51 +02002688 /* end of job, return OK */
Willy Tarreau3a816292009-07-07 10:55:49 +02002689 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02002690 req->analyse_exp = TICK_ETERNITY;
2691 return 1;
2692
2693 return_bad_req:
2694 /* We centralize bad requests processing here */
2695 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
2696 /* we detected a parsing error. We want to archive this request
2697 * in the dedicated proxy area for later troubleshooting.
2698 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01002699 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreaud787e662009-07-07 10:14:51 +02002700 }
2701
2702 txn->req.msg_state = HTTP_MSG_ERROR;
2703 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02002704 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002705
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002706 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02002707 if (s->listener->counters)
2708 s->listener->counters->failed_req++;
Willy Tarreaud787e662009-07-07 10:14:51 +02002709
2710 return_prx_cond:
2711 if (!(s->flags & SN_ERR_MASK))
2712 s->flags |= SN_ERR_PRXCOND;
2713 if (!(s->flags & SN_FINST_MASK))
2714 s->flags |= SN_FINST_R;
2715
2716 req->analysers = 0;
2717 req->analyse_exp = TICK_ETERNITY;
2718 return 0;
2719}
2720
Cyril Bonté70be45d2010-10-12 00:14:35 +02002721/* We reached the stats page through a POST request.
2722 * Parse the posted data and enable/disable servers if necessary.
Cyril Bonté23b39d92011-02-10 22:54:44 +01002723 * Returns 1 if request was parsed or zero if it needs more data.
Cyril Bonté70be45d2010-10-12 00:14:35 +02002724 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02002725int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct channel *req)
Cyril Bonté70be45d2010-10-12 00:14:35 +02002726{
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002727 struct proxy *px = NULL;
2728 struct server *sv = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002729
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002730 char key[LINESIZE];
2731 int action = ST_ADM_ACTION_NONE;
2732 int reprocess = 0;
2733
2734 int total_servers = 0;
2735 int altered_servers = 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002736
2737 char *first_param, *cur_param, *next_param, *end_params;
Willy Tarreau46787ed2012-04-11 17:28:40 +02002738 char *st_cur_param = NULL;
2739 char *st_next_param = NULL;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002740
Willy Tarreau9b28e032012-10-12 23:49:43 +02002741 first_param = req->buf->p + txn->req.eoh + 2;
Willy Tarreau124d9912011-03-01 20:30:48 +01002742 end_params = first_param + txn->req.body_len;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002743
2744 cur_param = next_param = end_params;
2745
Willy Tarreau9b28e032012-10-12 23:49:43 +02002746 if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002747 /* Prevent buffer overflow */
Willy Tarreau295a8372011-03-10 11:25:07 +01002748 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002749 return 1;
2750 }
Willy Tarreau9b28e032012-10-12 23:49:43 +02002751 else if (end_params > req->buf->p + req->buf->i) {
Cyril Bonté23b39d92011-02-10 22:54:44 +01002752 /* we need more data */
Willy Tarreau295a8372011-03-10 11:25:07 +01002753 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté23b39d92011-02-10 22:54:44 +01002754 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002755 }
2756
2757 *end_params = '\0';
2758
Willy Tarreau295a8372011-03-10 11:25:07 +01002759 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002760
2761 /*
2762 * Parse the parameters in reverse order to only store the last value.
2763 * From the html form, the backend and the action are at the end.
2764 */
2765 while (cur_param > first_param) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002766 char *value;
2767 int poffset, plen;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002768
2769 cur_param--;
2770 if ((*cur_param == '&') || (cur_param == first_param)) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002771 reprocess_servers:
Cyril Bonté70be45d2010-10-12 00:14:35 +02002772 /* Parse the key */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002773 poffset = (cur_param != first_param ? 1 : 0);
2774 plen = next_param - cur_param + (cur_param == first_param ? 1 : 0);
2775 if ((plen > 0) && (plen <= sizeof(key))) {
2776 strncpy(key, cur_param + poffset, plen);
2777 key[plen - 1] = '\0';
2778 } else {
2779 si->applet.ctx.stats.st_code = STAT_STATUS_EXCD;
2780 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002781 }
2782
2783 /* Parse the value */
2784 value = key;
2785 while (*value != '\0' && *value != '=') {
2786 value++;
2787 }
2788 if (*value == '=') {
2789 /* Ok, a value is found, we can mark the end of the key */
2790 *value++ = '\0';
2791 }
2792
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02002793 if (!url_decode(key) || !url_decode(value))
2794 break;
2795
Cyril Bonté70be45d2010-10-12 00:14:35 +02002796 /* Now we can check the key to see what to do */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002797 if (!px && (strcmp(key, "b") == 0)) {
2798 if ((px = findproxy(value, PR_CAP_BE)) == NULL) {
2799 /* the backend name is unknown or ambiguous (duplicate names) */
2800 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2801 goto out;
2802 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02002803 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002804 else if (!action && (strcmp(key, "action") == 0)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002805 if (strcmp(value, "disable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002806 action = ST_ADM_ACTION_DISABLE;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002807 }
2808 else if (strcmp(value, "enable") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002809 action = ST_ADM_ACTION_ENABLE;
2810 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002811 else if (strcmp(value, "stop") == 0) {
2812 action = ST_ADM_ACTION_STOP;
2813 }
2814 else if (strcmp(value, "start") == 0) {
2815 action = ST_ADM_ACTION_START;
2816 }
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002817 else if (strcmp(value, "shutdown") == 0) {
2818 action = ST_ADM_ACTION_SHUTDOWN;
2819 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002820 else {
2821 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2822 goto out;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002823 }
2824 }
2825 else if (strcmp(key, "s") == 0) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002826 if (!(px && action)) {
2827 /*
2828 * Indicates that we'll need to reprocess the parameters
2829 * as soon as backend and action are known
2830 */
2831 if (!reprocess) {
2832 st_cur_param = cur_param;
2833 st_next_param = next_param;
2834 }
2835 reprocess = 1;
2836 }
2837 else if ((sv = findserver(px, value)) != NULL) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002838 switch (action) {
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002839 case ST_ADM_ACTION_DISABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002840 if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002841 /* Not already in maintenance, we can change the server state */
2842 sv->state |= SRV_MAINTAIN;
2843 set_server_down(sv);
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002844 altered_servers++;
2845 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002846 }
2847 break;
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002848 case ST_ADM_ACTION_ENABLE:
Cyril Bonté1e2a1702011-03-03 21:05:17 +01002849 if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02002850 /* Already in maintenance, we can change the server state */
2851 set_server_up(sv);
Willy Tarreau70461302010-10-22 14:39:02 +02002852 sv->health = sv->rise; /* up, but will fall down at first failure */
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002853 altered_servers++;
2854 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002855 }
Willy Tarreaud7282242012-06-04 00:22:44 +02002856 break;
2857 case ST_ADM_ACTION_STOP:
2858 case ST_ADM_ACTION_START:
2859 if (action == ST_ADM_ACTION_START)
2860 sv->uweight = sv->iweight;
2861 else
2862 sv->uweight = 0;
2863
2864 if (px->lbprm.algo & BE_LB_PROP_DYN) {
2865 /* we must take care of not pushing the server to full throttle during slow starts */
2866 if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN))
2867 sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart;
2868 else
2869 sv->eweight = BE_WEIGHT_SCALE;
2870 sv->eweight *= sv->uweight;
2871 } else {
2872 sv->eweight = sv->uweight;
2873 }
2874
2875 /* static LB algorithms are a bit harder to update */
2876 if (px->lbprm.update_server_eweight)
2877 px->lbprm.update_server_eweight(sv);
2878 else if (sv->eweight) {
2879 if (px->lbprm.set_server_status_up)
2880 px->lbprm.set_server_status_up(sv);
2881 }
2882 else {
2883 if (px->lbprm.set_server_status_down)
2884 px->lbprm.set_server_status_down(sv);
2885 }
2886 altered_servers++;
2887 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002888 break;
Willy Tarreau4f8a83c2012-06-04 00:26:23 +02002889 case ST_ADM_ACTION_SHUTDOWN:
2890 if (px->state != PR_STSTOPPED) {
2891 struct session *sess, *sess_bck;
2892
2893 list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv)
2894 if (sess->srv_conn == sv)
2895 session_shutdown(sess, SN_ERR_KILLED);
2896
2897 altered_servers++;
2898 total_servers++;
2899 }
2900 break;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002901 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002902 } else {
2903 /* the server name is unknown or ambiguous (duplicate names) */
2904 total_servers++;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002905 }
2906 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002907 if (reprocess && px && action) {
2908 /* Now, we know the backend and the action chosen by the user.
2909 * We can safely restart from the first server parameter
2910 * to reprocess them
2911 */
2912 cur_param = st_cur_param;
2913 next_param = st_next_param;
2914 reprocess = 0;
2915 goto reprocess_servers;
2916 }
2917
Cyril Bonté70be45d2010-10-12 00:14:35 +02002918 next_param = cur_param;
2919 }
2920 }
Cyril Bontécf8d9ae2012-04-04 12:57:18 +02002921
2922 if (total_servers == 0) {
2923 si->applet.ctx.stats.st_code = STAT_STATUS_NONE;
2924 }
2925 else if (altered_servers == 0) {
2926 si->applet.ctx.stats.st_code = STAT_STATUS_ERRP;
2927 }
2928 else if (altered_servers == total_servers) {
2929 si->applet.ctx.stats.st_code = STAT_STATUS_DONE;
2930 }
2931 else {
2932 si->applet.ctx.stats.st_code = STAT_STATUS_PART;
2933 }
2934 out:
Cyril Bonté23b39d92011-02-10 22:54:44 +01002935 return 1;
Cyril Bonté70be45d2010-10-12 00:14:35 +02002936}
2937
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002938/* This function checks whether we need to enable a POST analyser to parse a
2939 * stats request, and also registers the stats I/O handler. It returns zero
Willy Tarreaucbc743e2012-12-28 08:36:50 +01002940 * if it needs to come back again, otherwise non-zero if it finishes. In the
2941 * latter case, it also clears the request analysers.
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002942 */
2943int http_handle_stats(struct session *s, struct channel *req)
2944{
2945 struct stats_admin_rule *stats_admin_rule;
2946 struct stream_interface *si = s->rep->prod;
2947 struct http_txn *txn = &s->txn;
2948 struct http_msg *msg = &txn->req;
2949 struct uri_auth *uri = s->be->uri_auth;
2950
2951 /* now check whether we have some admin rules for this request */
2952 list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) {
2953 int ret = 1;
2954
2955 if (stats_admin_rule->cond) {
2956 ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
2957 ret = acl_pass(ret);
2958 if (stats_admin_rule->cond->pol == ACL_COND_UNLESS)
2959 ret = !ret;
2960 }
2961
2962 if (ret) {
2963 /* no rule, or the rule matches */
2964 s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN;
2965 break;
2966 }
2967 }
2968
2969 /* Was the status page requested with a POST ? */
2970 if (unlikely(txn->meth == HTTP_METH_POST)) {
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002971 char scope_txt[STAT_SCOPE_TXT_MAXLEN + sizeof STAT_SCOPE_PATTERN];
2972
Willy Tarreau1facd6d2012-12-22 22:03:39 +01002973 if (si->applet.ctx.stats.flags & STAT_ADMIN) {
2974 if (msg->msg_state < HTTP_MSG_100_SENT) {
2975 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
2976 * send an HTTP/1.1 100 Continue intermediate response.
2977 */
2978 if (msg->flags & HTTP_MSGF_VER_11) {
2979 struct hdr_ctx ctx;
2980 ctx.idx = 0;
2981 /* Expect is allowed in 1.1, look for it */
2982 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
2983 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
2984 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
2985 }
2986 }
2987 msg->msg_state = HTTP_MSG_100_SENT;
2988 s->logs.tv_request = now; /* update the request timer to reflect full request */
2989 }
2990 if (!http_process_req_stat_post(si, txn, req))
2991 return 0; /* we need more data */
2992 }
2993 else
2994 si->applet.ctx.stats.st_code = STAT_STATUS_DENY;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002995 /* scope_txt = search pattern + search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
2996 scope_txt[0] = 0;
2997 if (si->applet.ctx.stats.scope_len) {
2998 strcpy(scope_txt, STAT_SCOPE_PATTERN);
2999 memcpy(scope_txt + strlen(STAT_SCOPE_PATTERN), bo_ptr(req->buf) + si->applet.ctx.stats.scope_str, si->applet.ctx.stats.scope_len);
3000 scope_txt[strlen(STAT_SCOPE_PATTERN) + si->applet.ctx.stats.scope_len] = 0;
3001 }
3002
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003003
3004 /* We don't want to land on the posted stats page because a refresh will
3005 * repost the data. We don't want this to happen on accident so we redirect
3006 * the browse to the stats page with a GET.
3007 */
3008 chunk_printf(&trash,
Yves Lafon4e8ec502013-03-11 11:06:05 -04003009 "HTTP/1.1 303 See Other\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003010 "Cache-Control: no-cache\r\n"
3011 "Content-Type: text/plain\r\n"
3012 "Connection: close\r\n"
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003013 "Location: %s;st=%s%s%s%s\r\n"
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003014 "\r\n",
3015 uri->uri_prefix,
3016 ((si->applet.ctx.stats.st_code > STAT_STATUS_INIT) &&
3017 (si->applet.ctx.stats.st_code < STAT_STATUS_SIZE) &&
3018 stat_status_codes[si->applet.ctx.stats.st_code]) ?
3019 stat_status_codes[si->applet.ctx.stats.st_code] :
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02003020 stat_status_codes[STAT_STATUS_UNKN],
3021 (si->applet.ctx.stats.flags & STAT_HIDE_DOWN) ? ";up" : "",
3022 (si->applet.ctx.stats.flags & STAT_NO_REFRESH) ? ";norefresh" : "",
3023 scope_txt);
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003024
3025 s->txn.status = 303;
3026 s->logs.tv_request = now;
3027 stream_int_retnclose(req->prod, &trash);
3028 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
3029
3030 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3031 s->fe->fe_counters.intercepted_req++;
3032
3033 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3034 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3035 if (!(s->flags & SN_FINST_MASK))
3036 s->flags |= SN_FINST_R;
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003037 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003038 return 1;
3039 }
3040
3041 /* OK, let's go on now */
3042
3043 chunk_printf(&trash,
3044 "HTTP/1.0 200 OK\r\n"
3045 "Cache-Control: no-cache\r\n"
3046 "Connection: close\r\n"
3047 "Content-Type: %s\r\n",
Willy Tarreau354898b2012-12-23 18:15:23 +01003048 (si->applet.ctx.stats.flags & STAT_FMT_HTML) ? "text/html" : "text/plain");
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003049
3050 if (uri->refresh > 0 && !(si->applet.ctx.stats.flags & STAT_NO_REFRESH))
3051 chunk_appendf(&trash, "Refresh: %d\r\n",
3052 uri->refresh);
3053
3054 chunk_appendf(&trash, "\r\n");
3055
3056 s->txn.status = 200;
3057 s->logs.tv_request = now;
3058
3059 if (s->fe == s->be) /* report it if the request was intercepted by the frontend */
3060 s->fe->fe_counters.intercepted_req++;
3061
3062 if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is
3063 s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy
3064 if (!(s->flags & SN_FINST_MASK))
3065 s->flags |= SN_FINST_R;
3066
3067 if (s->txn.meth == HTTP_METH_HEAD) {
3068 /* that's all we return in case of HEAD request, so let's immediately close. */
3069 stream_int_retnclose(req->prod, &trash);
3070 s->target = &http_stats_applet.obj_type; /* just for logging the applet name */
Willy Tarreaucbc743e2012-12-28 08:36:50 +01003071 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003072 return 1;
3073 }
3074
3075 /* OK, push the response and hand over to the stats I/O handler */
3076 bi_putchk(s->rep, &trash);
3077
3078 s->task->nice = -32; /* small boost for HTTP statistics */
3079 stream_int_register_handler(s->rep->prod, &http_stats_applet);
3080 s->target = s->rep->prod->conn->target; // for logging only
3081 s->rep->prod->conn->xprt_ctx = s;
3082 s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0;
3083 req->analysers = 0;
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003084 return 1;
3085}
3086
Willy Tarreau20b0de52012-12-24 15:45:22 +01003087/* Executes the http-request rules <rules> for session <s>, proxy <px> and
Willy Tarreau96257ec2012-12-27 10:46:37 +01003088 * transaction <txn>. Returns the first rule that prevents further processing
3089 * of the request (auth, deny, ...) or NULL if it executed all rules or stopped
3090 * on an allow. It may set the TX_CLDENY on txn->flags if it encounters a deny
3091 * rule.
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003092 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003093static struct http_req_rule *
Willy Tarreau96257ec2012-12-27 10:46:37 +01003094http_req_get_intercept_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003095{
Willy Tarreauff011f22011-01-06 17:51:27 +01003096 struct http_req_rule *rule;
Willy Tarreau20b0de52012-12-24 15:45:22 +01003097 struct hdr_ctx ctx;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003098
Willy Tarreauff011f22011-01-06 17:51:27 +01003099 list_for_each_entry(rule, rules, list) {
Willy Tarreauff011f22011-01-06 17:51:27 +01003100 if (rule->action >= HTTP_REQ_ACT_MAX)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003101 continue;
3102
Willy Tarreau96257ec2012-12-27 10:46:37 +01003103 /* check optional condition */
Willy Tarreauff011f22011-01-06 17:51:27 +01003104 if (rule->cond) {
Willy Tarreau96257ec2012-12-27 10:46:37 +01003105 int ret;
3106
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003107 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003108 ret = acl_pass(ret);
3109
Willy Tarreauff011f22011-01-06 17:51:27 +01003110 if (rule->cond->pol == ACL_COND_UNLESS)
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003111 ret = !ret;
Willy Tarreau96257ec2012-12-27 10:46:37 +01003112
3113 if (!ret) /* condition not matched */
3114 continue;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003115 }
3116
Willy Tarreau20b0de52012-12-24 15:45:22 +01003117
Willy Tarreau96257ec2012-12-27 10:46:37 +01003118 switch (rule->action) {
3119 case HTTP_REQ_ACT_ALLOW:
3120 return NULL; /* "allow" rules are OK */
3121
3122 case HTTP_REQ_ACT_DENY:
3123 txn->flags |= TX_CLDENY;
3124 return rule;
3125
Willy Tarreauccbcc372012-12-27 12:37:57 +01003126 case HTTP_REQ_ACT_TARPIT:
3127 txn->flags |= TX_CLTARPIT;
3128 return rule;
3129
Willy Tarreau96257ec2012-12-27 10:46:37 +01003130 case HTTP_REQ_ACT_AUTH:
3131 return rule;
3132
Willy Tarreau81499eb2012-12-27 12:19:02 +01003133 case HTTP_REQ_ACT_REDIR:
3134 return rule;
3135
Willy Tarreau96257ec2012-12-27 10:46:37 +01003136 case HTTP_REQ_ACT_SET_HDR:
3137 ctx.idx = 0;
3138 /* remove all occurrences of the header */
3139 while (http_find_header2(rule->arg.hdr_add.name, rule->arg.hdr_add.name_len,
3140 txn->req.chn->buf->p, &txn->hdr_idx, &ctx)) {
3141 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Willy Tarreau20b0de52012-12-24 15:45:22 +01003142 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003143 /* now fall through to header addition */
3144
3145 case HTTP_REQ_ACT_ADD_HDR:
3146 chunk_printf(&trash, "%s: ", rule->arg.hdr_add.name);
3147 memcpy(trash.str, rule->arg.hdr_add.name, rule->arg.hdr_add.name_len);
3148 trash.len = rule->arg.hdr_add.name_len;
3149 trash.str[trash.len++] = ':';
3150 trash.str[trash.len++] = ' ';
3151 trash.len += build_logline(s, trash.str + trash.len, trash.size - trash.len, &rule->arg.hdr_add.fmt);
3152 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len);
3153 break;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003154 }
3155 }
Willy Tarreau96257ec2012-12-27 10:46:37 +01003156
3157 /* we reached the end of the rules, nothing to report */
Willy Tarreau418c1a02012-12-25 20:52:58 +01003158 return NULL;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003159}
3160
Willy Tarreau71241ab2012-12-27 11:30:54 +01003161
3162/* Perform an HTTP redirect based on the information in <rule>. The function
3163 * returns non-zero on success, or zero in case of a, irrecoverable error such
3164 * as too large a request to build a valid response.
3165 */
3166static int http_apply_redirect_rule(struct redirect_rule *rule, struct session *s, struct http_txn *txn)
3167{
3168 struct http_msg *msg = &txn->req;
3169 const char *msg_fmt;
3170
3171 /* build redirect message */
3172 switch(rule->code) {
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04003173 case 308:
3174 msg_fmt = HTTP_308;
3175 break;
3176 case 307:
3177 msg_fmt = HTTP_307;
3178 break;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003179 case 303:
3180 msg_fmt = HTTP_303;
3181 break;
3182 case 301:
3183 msg_fmt = HTTP_301;
3184 break;
3185 case 302:
3186 default:
3187 msg_fmt = HTTP_302;
3188 break;
3189 }
3190
3191 if (unlikely(!chunk_strcpy(&trash, msg_fmt)))
3192 return 0;
3193
3194 switch(rule->type) {
3195 case REDIRECT_TYPE_SCHEME: {
3196 const char *path;
3197 const char *host;
3198 struct hdr_ctx ctx;
3199 int pathlen;
3200 int hostlen;
3201
3202 host = "";
3203 hostlen = 0;
3204 ctx.idx = 0;
3205 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
3206 host = ctx.line + ctx.val;
3207 hostlen = ctx.vlen;
3208 }
3209
3210 path = http_get_path(txn);
3211 /* build message using path */
3212 if (path) {
3213 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3214 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3215 int qs = 0;
3216 while (qs < pathlen) {
3217 if (path[qs] == '?') {
3218 pathlen = qs;
3219 break;
3220 }
3221 qs++;
3222 }
3223 }
3224 } else {
3225 path = "/";
3226 pathlen = 1;
3227 }
3228
3229 /* check if we can add scheme + "://" + host + path */
3230 if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4)
3231 return 0;
3232
3233 /* add scheme */
3234 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3235 trash.len += rule->rdr_len;
3236
3237 /* add "://" */
3238 memcpy(trash.str + trash.len, "://", 3);
3239 trash.len += 3;
3240
3241 /* add host */
3242 memcpy(trash.str + trash.len, host, hostlen);
3243 trash.len += hostlen;
3244
3245 /* add path */
3246 memcpy(trash.str + trash.len, path, pathlen);
3247 trash.len += pathlen;
3248
3249 /* append a slash at the end of the location is needed and missing */
3250 if (trash.len && trash.str[trash.len - 1] != '/' &&
3251 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3252 if (trash.len > trash.size - 5)
3253 return 0;
3254 trash.str[trash.len] = '/';
3255 trash.len++;
3256 }
3257
3258 break;
3259 }
3260 case REDIRECT_TYPE_PREFIX: {
3261 const char *path;
3262 int pathlen;
3263
3264 path = http_get_path(txn);
3265 /* build message using path */
3266 if (path) {
3267 pathlen = txn->req.sl.rq.u_l + (txn->req.chn->buf->p + txn->req.sl.rq.u) - path;
3268 if (rule->flags & REDIRECT_FLAG_DROP_QS) {
3269 int qs = 0;
3270 while (qs < pathlen) {
3271 if (path[qs] == '?') {
3272 pathlen = qs;
3273 break;
3274 }
3275 qs++;
3276 }
3277 }
3278 } else {
3279 path = "/";
3280 pathlen = 1;
3281 }
3282
3283 if (trash.len + rule->rdr_len + pathlen > trash.size - 4)
3284 return 0;
3285
3286 /* add prefix. Note that if prefix == "/", we don't want to
3287 * add anything, otherwise it makes it hard for the user to
3288 * configure a self-redirection.
3289 */
3290 if (rule->rdr_len != 1 || *rule->rdr_str != '/') {
3291 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3292 trash.len += rule->rdr_len;
3293 }
3294
3295 /* add path */
3296 memcpy(trash.str + trash.len, path, pathlen);
3297 trash.len += pathlen;
3298
3299 /* append a slash at the end of the location is needed and missing */
3300 if (trash.len && trash.str[trash.len - 1] != '/' &&
3301 (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) {
3302 if (trash.len > trash.size - 5)
3303 return 0;
3304 trash.str[trash.len] = '/';
3305 trash.len++;
3306 }
3307
3308 break;
3309 }
3310 case REDIRECT_TYPE_LOCATION:
3311 default:
3312 if (trash.len + rule->rdr_len > trash.size - 4)
3313 return 0;
3314
3315 /* add location */
3316 memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len);
3317 trash.len += rule->rdr_len;
3318 break;
3319 }
3320
3321 if (rule->cookie_len) {
3322 memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14);
3323 trash.len += 14;
3324 memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len);
3325 trash.len += rule->cookie_len;
3326 memcpy(trash.str + trash.len, "\r\n", 2);
3327 trash.len += 2;
3328 }
3329
3330 /* add end of headers and the keep-alive/close status.
3331 * We may choose to set keep-alive if the Location begins
3332 * with a slash, because the client will come back to the
3333 * same server.
3334 */
3335 txn->status = rule->code;
3336 /* let's log the request time */
3337 s->logs.tv_request = now;
3338
3339 if (rule->rdr_len >= 1 && *rule->rdr_str == '/' &&
3340 (msg->flags & HTTP_MSGF_XFER_LEN) &&
3341 !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len &&
3342 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL ||
3343 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) {
3344 /* keep-alive possible */
3345 if (!(msg->flags & HTTP_MSGF_VER_11)) {
3346 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3347 memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30);
3348 trash.len += 30;
3349 } else {
3350 memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24);
3351 trash.len += 24;
3352 }
3353 }
3354 memcpy(trash.str + trash.len, "\r\n\r\n", 4);
3355 trash.len += 4;
3356 bo_inject(txn->rsp.chn, trash.str, trash.len);
3357 /* "eat" the request */
3358 bi_fast_delete(txn->req.chn->buf, msg->sov);
3359 msg->sov = 0;
3360 txn->req.chn->analysers = AN_REQ_HTTP_XFER_BODY;
3361 s->rep->analysers = AN_RES_HTTP_XFER_BODY;
3362 txn->req.msg_state = HTTP_MSG_CLOSED;
3363 txn->rsp.msg_state = HTTP_MSG_DONE;
3364 } else {
3365 /* keep-alive not possible */
3366 if (unlikely(txn->flags & TX_USE_PX_CONN)) {
3367 memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29);
3368 trash.len += 29;
3369 } else {
3370 memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23);
3371 trash.len += 23;
3372 }
3373 stream_int_retnclose(txn->req.chn->prod, &trash);
3374 txn->req.chn->analysers = 0;
3375 }
3376
3377 if (!(s->flags & SN_ERR_MASK))
3378 s->flags |= SN_ERR_PRXCOND;
3379 if (!(s->flags & SN_FINST_MASK))
3380 s->flags |= SN_FINST_R;
3381
3382 return 1;
3383}
3384
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003385/* This stream analyser runs all HTTP request processing which is common to
3386 * frontends and backends, which means blocking ACLs, filters, connection-close,
3387 * reqadd, stats and redirects. This is performed for the designated proxy.
Willy Tarreaud787e662009-07-07 10:14:51 +02003388 * It returns 1 if the processing can continue on next analysers, or zero if it
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003389 * either needs more data or wants to immediately abort the request (eg: deny,
3390 * error, ...).
Willy Tarreaud787e662009-07-07 10:14:51 +02003391 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003392int http_process_req_common(struct session *s, struct channel *req, int an_bit, struct proxy *px)
Willy Tarreaud787e662009-07-07 10:14:51 +02003393{
Willy Tarreaud787e662009-07-07 10:14:51 +02003394 struct http_txn *txn = &s->txn;
3395 struct http_msg *msg = &txn->req;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003396 struct acl_cond *cond;
Willy Tarreauff011f22011-01-06 17:51:27 +01003397 struct http_req_rule *http_req_last_rule = NULL;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003398 struct redirect_rule *rule;
Willy Tarreauf4f04122010-01-28 18:10:50 +01003399 struct cond_wordlist *wl;
Simon Horman70735c92011-06-07 11:07:50 +09003400 int do_stats;
Willy Tarreaud787e662009-07-07 10:14:51 +02003401
Willy Tarreau655dce92009-11-08 13:10:58 +01003402 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003403 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003404 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003405 return 0;
3406 }
3407
Willy Tarreau3a816292009-07-07 10:55:49 +02003408 req->analysers &= ~an_bit;
Willy Tarreaud787e662009-07-07 10:14:51 +02003409 req->analyse_exp = TICK_ETERNITY;
3410
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003411 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 +02003412 now_ms, __FUNCTION__,
3413 s,
3414 req,
3415 req->rex, req->wex,
3416 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003417 req->buf->i,
Willy Tarreaud787e662009-07-07 10:14:51 +02003418 req->analysers);
3419
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003420 /* first check whether we have some ACLs set to block this request */
3421 list_for_each_entry(cond, &px->block_cond, list) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003422 int ret = acl_exec_cond(cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreaub463dfb2008-06-07 23:08:56 +02003423
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003424 ret = acl_pass(ret);
3425 if (cond->pol == ACL_COND_UNLESS)
3426 ret = !ret;
Willy Tarreau53b6c742006-12-17 13:37:46 +01003427
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003428 if (ret) {
3429 txn->status = 403;
3430 /* let's log the request time */
3431 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003432 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003433 session_inc_http_err_ctr(s);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003434 goto return_prx_cond;
Willy Tarreau59234e92008-11-30 23:51:27 +01003435 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003436 }
Willy Tarreau59234e92008-11-30 23:51:27 +01003437
Willy Tarreau5d5b5d82012-12-09 12:00:04 +01003438 /* just in case we have some per-backend tracking */
3439 session_inc_be_http_req_ctr(s);
3440
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003441 /* evaluate http-request rules */
Willy Tarreau96257ec2012-12-27 10:46:37 +01003442 http_req_last_rule = http_req_get_intercept_rule(px, &px->http_req_rules, s, txn);
Willy Tarreau51425942010-02-01 10:40:19 +01003443
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003444 /* evaluate stats http-request rules only if http-request is OK */
Willy Tarreauff011f22011-01-06 17:51:27 +01003445 if (!http_req_last_rule) {
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003446 do_stats = stats_check_uri(s->rep->prod, txn, px);
3447 if (do_stats)
Willy Tarreau96257ec2012-12-27 10:46:37 +01003448 http_req_last_rule = http_req_get_intercept_rule(px, &px->uri_auth->http_req_rules, s, txn);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003449 }
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003450 else
3451 do_stats = 0;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003452
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003453 /* return a 403 if either rule has blocked */
Willy Tarreauccbcc372012-12-27 12:37:57 +01003454 if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) {
3455 if (txn->flags & TX_CLDENY) {
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003456 txn->status = 403;
3457 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003458 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003459 session_inc_http_err_ctr(s);
Willy Tarreau6da0f6d2011-01-06 18:19:50 +01003460 s->fe->fe_counters.denied_req++;
3461 if (an_bit == AN_REQ_HTTP_PROCESS_BE)
3462 s->be->be_counters.denied_req++;
3463 if (s->listener->counters)
3464 s->listener->counters->denied_req++;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003465 goto return_prx_cond;
Willy Tarreauccbcc372012-12-27 12:37:57 +01003466 }
3467 /* When a connection is tarpitted, we use the tarpit timeout,
3468 * which may be the same as the connect timeout if unspecified.
3469 * If unset, then set it to zero because we really want it to
3470 * eventually expire. We build the tarpit as an analyser.
3471 */
3472 if (txn->flags & TX_CLTARPIT) {
3473 channel_erase(s->req);
3474 /* wipe the request out so that we can drop the connection early
3475 * if the client closes first.
3476 */
3477 channel_dont_connect(req);
3478 req->analysers = 0; /* remove switching rules etc... */
3479 req->analysers |= AN_REQ_HTTP_TARPIT;
3480 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3481 if (!req->analyse_exp)
3482 req->analyse_exp = tick_add(now_ms, 0);
3483 session_inc_http_err_ctr(s);
3484 s->fe->fe_counters.denied_req++;
3485 if (s->fe != s->be)
3486 s->be->be_counters.denied_req++;
3487 if (s->listener->counters)
3488 s->listener->counters->denied_req++;
3489 return 1;
3490 }
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003491 }
3492
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003493 /* try headers filters */
3494 if (px->req_exp != NULL) {
Willy Tarreau6c123b12010-01-28 20:22:06 +01003495 if (apply_filters_to_request(s, req, px) < 0)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003496 goto return_bad_req;
Willy Tarreau06619262006-12-17 08:37:22 +01003497
Willy Tarreau59234e92008-11-30 23:51:27 +01003498 /* has the request been denied ? */
3499 if (txn->flags & TX_CLDENY) {
3500 /* no need to go further */
3501 txn->status = 403;
3502 /* let's log the request time */
3503 s->logs.tv_request = now;
Willy Tarreau783f2582012-09-04 12:19:04 +02003504 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403));
Willy Tarreauda7ff642010-06-23 11:44:09 +02003505 session_inc_http_err_ctr(s);
Willy Tarreau59234e92008-11-30 23:51:27 +01003506 goto return_prx_cond;
3507 }
Willy Tarreauc465fd72009-08-31 00:17:18 +02003508
3509 /* When a connection is tarpitted, we use the tarpit timeout,
3510 * which may be the same as the connect timeout if unspecified.
3511 * If unset, then set it to zero because we really want it to
3512 * eventually expire. We build the tarpit as an analyser.
3513 */
3514 if (txn->flags & TX_CLTARPIT) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003515 channel_erase(s->req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003516 /* wipe the request out so that we can drop the connection early
3517 * if the client closes first.
3518 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003519 channel_dont_connect(req);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003520 req->analysers = 0; /* remove switching rules etc... */
3521 req->analysers |= AN_REQ_HTTP_TARPIT;
3522 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit);
3523 if (!req->analyse_exp)
3524 req->analyse_exp = tick_add(now_ms, 0);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003525 session_inc_http_err_ctr(s);
Willy Tarreauc465fd72009-08-31 00:17:18 +02003526 return 1;
3527 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003528 }
Willy Tarreau06619262006-12-17 08:37:22 +01003529
Willy Tarreau5b154472009-12-21 20:11:07 +01003530 /* Until set to anything else, the connection mode is set as TUNNEL. It will
3531 * only change if both the request and the config reference something else.
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003532 * Option httpclose by itself does not set a mode, it remains a tunnel mode
3533 * in which headers are mangled. However, if another mode is set, it will
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003534 * affect it (eg: server-close/keep-alive + httpclose = close). Note that we
3535 * avoid to redo the same work if FE and BE have the same settings (common).
3536 * The method consists in checking if options changed between the two calls
3537 * (implying that either one is non-null, or one of them is non-null and we
3538 * are there for the first time.
Willy Tarreau42736642009-10-18 21:04:35 +02003539 */
Willy Tarreau5b154472009-12-21 20:11:07 +01003540
Willy Tarreaudc008c52010-02-01 16:20:08 +01003541 if ((!(txn->flags & TX_HDR_CONN_PRS) &&
3542 (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) ||
3543 ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) !=
3544 (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 +01003545 int tmp = TX_CON_WANT_TUN;
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003546
Cyril Bonté9ea2b9a2010-12-29 09:36:56 +01003547 if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE ||
3548 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreau5b154472009-12-21 20:11:07 +01003549 tmp = TX_CON_WANT_KAL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01003550 if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO)
3551 tmp = TX_CON_WANT_SCL;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003552 if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO)
Willy Tarreau5b154472009-12-21 20:11:07 +01003553 tmp = TX_CON_WANT_CLO;
3554
Willy Tarreau5b154472009-12-21 20:11:07 +01003555 if ((txn->flags & TX_CON_WANT_MSK) < tmp)
3556 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp;
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003557
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003558 if (!(txn->flags & TX_HDR_CONN_PRS)) {
3559 /* parse the Connection header and possibly clean it */
3560 int to_del = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003561 if ((msg->flags & HTTP_MSGF_VER_11) ||
Willy Tarreau8a8e1d92010-04-05 16:15:16 +02003562 ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL &&
3563 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003564 to_del |= 2; /* remove "keep-alive" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003565 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003566 to_del |= 1; /* remove "close" */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003567 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau0dfdf192010-01-05 11:33:11 +01003568 }
Willy Tarreau5b154472009-12-21 20:11:07 +01003569
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003570 /* check if client or config asks for explicit close in KAL/SCL */
3571 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
3572 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) &&
3573 ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003574 (!(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 +01003575 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003576 !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */
Willy Tarreauc3e8b252010-01-28 15:01:20 +01003577 s->fe->state == PR_STSTOPPED)) /* frontend is stopping */
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003578 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
3579 }
Willy Tarreau78599912009-10-17 20:12:21 +02003580
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003581 /* we can be blocked here because the request needs to be authenticated,
3582 * either to pass or to access stats.
3583 */
Willy Tarreau20b0de52012-12-24 15:45:22 +01003584 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_AUTH) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01003585 char *realm = http_req_last_rule->arg.auth.realm;
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003586
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01003587 if (!realm)
3588 realm = do_stats?STATS_DEFAULT_REALM:px->id;
3589
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003590 chunk_printf(&trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003591 txn->status = 401;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003592 stream_int_retnclose(req->prod, &trash);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003593 /* on 401 we still count one error, because normal browsing
3594 * won't significantly increase the counter but brute force
3595 * attempts will.
3596 */
3597 session_inc_http_err_ctr(s);
Krzysztof Piotr Oledzki59bb2182010-01-29 17:58:21 +01003598 goto return_prx_cond;
3599 }
3600
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003601 /* add request headers from the rule sets in the same order */
3602 list_for_each_entry(wl, &px->req_add, list) {
3603 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003604 int ret = acl_exec_cond(wl->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003605 ret = acl_pass(ret);
3606 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
3607 ret = !ret;
3608 if (!ret)
3609 continue;
3610 }
3611
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003612 if (unlikely(http_header_add_tail(&txn->req, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003613 goto return_bad_req;
Willy Tarreau81499eb2012-12-27 12:19:02 +01003614 }
3615
3616 if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_REDIR) {
3617 if (!http_apply_redirect_rule(http_req_last_rule->arg.redir, s, txn))
3618 goto return_bad_req;
3619 req->analyse_exp = TICK_ETERNITY;
3620 return 1;
Willy Tarreauf68a15a2011-01-06 16:53:21 +01003621 }
3622
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003623 if (unlikely(do_stats)) {
3624 /* process the stats request now */
3625 if (!http_handle_stats(s, req)) {
3626 /* we need more data, let's come back here later */
3627 req->analysers |= an_bit;
3628 channel_dont_connect(req);
Willy Tarreau7fe33002013-04-21 08:04:22 +02003629 return 0;
Cyril Bonté70be45d2010-10-12 00:14:35 +02003630 }
Willy Tarreau1facd6d2012-12-22 22:03:39 +01003631 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003632 }
Willy Tarreaub2513902006-12-17 14:52:38 +01003633
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003634 /* check whether we have some ACLs set to redirect this request */
3635 list_for_each_entry(rule, &px->redirect_rules, list) {
Willy Tarreauf285f542010-01-03 20:03:03 +01003636 if (rule->cond) {
Willy Tarreau71241ab2012-12-27 11:30:54 +01003637 int ret;
3638
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02003639 ret = acl_exec_cond(rule->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf285f542010-01-03 20:03:03 +01003640 ret = acl_pass(ret);
3641 if (rule->cond->pol == ACL_COND_UNLESS)
3642 ret = !ret;
Willy Tarreau71241ab2012-12-27 11:30:54 +01003643 if (!ret)
3644 continue;
Willy Tarreauf285f542010-01-03 20:03:03 +01003645 }
Willy Tarreau71241ab2012-12-27 11:30:54 +01003646 if (!http_apply_redirect_rule(rule, s, txn))
3647 goto return_bad_req;
Willy Tarreaua9679ac2010-01-03 17:32:57 +01003648
Willy Tarreau71241ab2012-12-27 11:30:54 +01003649 req->analyse_exp = TICK_ETERNITY;
3650 return 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003651 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003652
Willy Tarreau2be39392010-01-03 17:24:51 +01003653 /* POST requests may be accompanied with an "Expect: 100-Continue" header.
3654 * If this happens, then the data will not come immediately, so we must
3655 * send all what we have without waiting. Note that due to the small gain
3656 * in waiting for the body of the request, it's easier to simply put the
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003657 * CF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove
Willy Tarreau2be39392010-01-03 17:24:51 +01003658 * itself once used.
3659 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003660 req->flags |= CF_SEND_DONTWAIT;
Willy Tarreau2be39392010-01-03 17:24:51 +01003661
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003662 /* that's OK for us now, let's move on to next analysers */
3663 return 1;
Willy Tarreau11382812008-07-09 16:18:21 +02003664
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003665 return_bad_req:
3666 /* We centralize bad requests processing here */
3667 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
3668 /* we detected a parsing error. We want to archive this request
3669 * in the dedicated proxy area for later troubleshooting.
3670 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003671 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003672 }
Willy Tarreau55ea7572007-06-17 19:56:27 +02003673
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003674 txn->req.msg_state = HTTP_MSG_ERROR;
3675 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02003676 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003677
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003678 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003679 if (s->listener->counters)
3680 s->listener->counters->failed_req++;
Willy Tarreau6e4261e2007-09-18 18:36:05 +02003681
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003682 return_prx_cond:
3683 if (!(s->flags & SN_ERR_MASK))
3684 s->flags |= SN_ERR_PRXCOND;
3685 if (!(s->flags & SN_FINST_MASK))
3686 s->flags |= SN_FINST_R;
Willy Tarreauf1221aa2006-12-17 22:14:12 +01003687
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003688 req->analysers = 0;
3689 req->analyse_exp = TICK_ETERNITY;
3690 return 0;
3691}
Willy Tarreau58f10d72006-12-04 02:26:12 +01003692
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003693/* This function performs all the processing enabled for the current request.
3694 * It returns 1 if the processing can continue on next analysers, or zero if it
3695 * needs more data, encounters an error, or wants to immediately abort the
3696 * request. It relies on buffers flags, and updates s->req->analysers.
3697 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003698int http_process_request(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003699{
3700 struct http_txn *txn = &s->txn;
3701 struct http_msg *msg = &txn->req;
Willy Tarreau58f10d72006-12-04 02:26:12 +01003702
Willy Tarreau655dce92009-11-08 13:10:58 +01003703 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau51aecc72009-07-12 09:47:04 +02003704 /* we need more data */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003705 channel_dont_connect(req);
Willy Tarreau51aecc72009-07-12 09:47:04 +02003706 return 0;
3707 }
3708
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01003709 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 +02003710 now_ms, __FUNCTION__,
3711 s,
3712 req,
3713 req->rex, req->wex,
3714 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003715 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02003716 req->analysers);
Willy Tarreau06619262006-12-17 08:37:22 +01003717
William Lallemand82fe75c2012-10-23 10:25:10 +02003718 if (s->fe->comp || s->be->comp)
3719 select_compression_request_header(s, req->buf);
3720
Willy Tarreau59234e92008-11-30 23:51:27 +01003721 /*
3722 * Right now, we know that we have processed the entire headers
3723 * and that unwanted requests have been filtered out. We can do
3724 * whatever we want with the remaining request. Also, now we
3725 * may have separate values for ->fe, ->be.
3726 */
Willy Tarreau06619262006-12-17 08:37:22 +01003727
Willy Tarreau59234e92008-11-30 23:51:27 +01003728 /*
3729 * If HTTP PROXY is set we simply get remote server address
3730 * parsing incoming request.
3731 */
3732 if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) {
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003733 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 +01003734 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01003735
Willy Tarreau59234e92008-11-30 23:51:27 +01003736 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003737 * 7: Now we can work with the cookies.
Willy Tarreau59234e92008-11-30 23:51:27 +01003738 * Note that doing so might move headers in the request, but
3739 * the fields will stay coherent and the URI will not move.
3740 * This should only be performed in the backend.
3741 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02003742 if ((s->be->cookie_name || s->be->appsession_name || s->fe->capture_name)
Willy Tarreau59234e92008-11-30 23:51:27 +01003743 && !(txn->flags & (TX_CLDENY|TX_CLTARPIT)))
3744 manage_client_side_cookies(s, req);
Willy Tarreau7ac51f62007-03-25 16:00:04 +02003745
Willy Tarreau59234e92008-11-30 23:51:27 +01003746 /*
Cyril Bontéb21570a2009-11-29 20:04:48 +01003747 * 8: the appsession cookie was looked up very early in 1.2,
3748 * so let's do the same now.
3749 */
3750
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02003751 /* It needs to look into the URI unless persistence must be ignored */
3752 if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02003753 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 +01003754 }
3755
William Lallemanda73203e2012-03-12 12:48:57 +01003756 /* add unique-id if "header-unique-id" is specified */
3757
3758 if (!LIST_ISEMPTY(&s->fe->format_unique_id))
3759 build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id);
3760
3761 if (s->fe->header_unique_id && s->unique_id) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003762 chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id);
3763 if (trash.len < 0)
William Lallemanda73203e2012-03-12 12:48:57 +01003764 goto return_bad_req;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003765 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, trash.len) < 0))
William Lallemanda73203e2012-03-12 12:48:57 +01003766 goto return_bad_req;
3767 }
3768
Cyril Bontéb21570a2009-11-29 20:04:48 +01003769 /*
Willy Tarreau59234e92008-11-30 23:51:27 +01003770 * 9: add X-Forwarded-For if either the frontend or the backend
3771 * asks for it.
3772 */
3773 if ((s->fe->options | s->be->options) & PR_O_FWDFOR) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003774 struct hdr_ctx ctx = { .idx = 0 };
Willy Tarreau87cf5142011-08-19 22:57:24 +02003775 if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) &&
Cyril Bontéa32d2752012-05-29 23:27:41 +02003776 http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name,
3777 s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_len : s->fe->fwdfor_hdr_len,
Willy Tarreau9b28e032012-10-12 23:49:43 +02003778 req->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreau87cf5142011-08-19 22:57:24 +02003779 /* The header is set to be added only if none is present
3780 * and we found it, so don't do anything.
3781 */
3782 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003783 else if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003784 /* Add an X-Forwarded-For header unless the source IP is
3785 * in the 'except' network range.
3786 */
3787 if ((!s->fe->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003788 (((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 +01003789 != s->fe->except_net.s_addr) &&
3790 (!s->be->except_mask.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003791 (((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 +01003792 != s->be->except_net.s_addr)) {
Willy Tarreau2a324282006-12-05 00:05:46 +01003793 int len;
Willy Tarreau59234e92008-11-30 23:51:27 +01003794 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003795 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.from)->sin_addr;
Ross Westaf72a1d2008-08-03 10:51:45 +02003796
3797 /* Note: we rely on the backend to get the header name to be used for
3798 * x-forwarded-for, because the header is really meant for the backends.
3799 * However, if the backend did not specify any option, we have to rely
3800 * on the frontend's header name.
3801 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003802 if (s->be->fwdfor_hdr_len) {
3803 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003804 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Ross Westaf72a1d2008-08-03 10:51:45 +02003805 } else {
Willy Tarreau59234e92008-11-30 23:51:27 +01003806 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003807 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003808 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003809 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Willy Tarreauedcf6682008-11-30 23:15:34 +01003810
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003811 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau06619262006-12-17 08:37:22 +01003812 goto return_bad_req;
Willy Tarreau2a324282006-12-05 00:05:46 +01003813 }
3814 }
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003815 else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) {
Willy Tarreau59234e92008-11-30 23:51:27 +01003816 /* FIXME: for the sake of completeness, we should also support
3817 * 'except' here, although it is mostly useless in this case.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003818 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003819 int len;
3820 char pn[INET6_ADDRSTRLEN];
3821 inet_ntop(AF_INET6,
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003822 (const void *)&((struct sockaddr_in6 *)(&s->req->prod->conn->addr.from))->sin6_addr,
Willy Tarreau59234e92008-11-30 23:51:27 +01003823 pn, sizeof(pn));
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003824
Willy Tarreau59234e92008-11-30 23:51:27 +01003825 /* Note: we rely on the backend to get the header name to be used for
3826 * x-forwarded-for, because the header is really meant for the backends.
3827 * However, if the backend did not specify any option, we have to rely
3828 * on the frontend's header name.
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003829 */
Willy Tarreau59234e92008-11-30 23:51:27 +01003830 if (s->be->fwdfor_hdr_len) {
3831 len = s->be->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003832 memcpy(trash.str, s->be->fwdfor_hdr_name, len);
Willy Tarreau59234e92008-11-30 23:51:27 +01003833 } else {
3834 len = s->fe->fwdfor_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003835 memcpy(trash.str, s->fe->fwdfor_hdr_name, len);
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +02003836 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003837 len += sprintf(trash.str + len, ": %s", pn);
Willy Tarreauadfb8562008-08-11 15:24:42 +02003838
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003839 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Willy Tarreau59234e92008-11-30 23:51:27 +01003840 goto return_bad_req;
3841 }
3842 }
3843
3844 /*
Maik Broemme2850cb42009-04-17 18:53:21 +02003845 * 10: add X-Original-To if either the frontend or the backend
3846 * asks for it.
3847 */
3848 if ((s->fe->options | s->be->options) & PR_O_ORGTO) {
3849
3850 /* FIXME: don't know if IPv6 can handle that case too. */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003851 if (s->req->prod->conn->addr.from.ss_family == AF_INET) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003852 /* Add an X-Original-To header unless the destination IP is
3853 * in the 'except' network range.
3854 */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003855 conn_get_to_addr(s->req->prod->conn);
Maik Broemme2850cb42009-04-17 18:53:21 +02003856
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003857 if (s->req->prod->conn->addr.to.ss_family == AF_INET &&
Emeric Brun5bd86a82010-10-22 17:23:04 +02003858 ((!s->fe->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003859 (((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 +02003860 != s->fe->except_to.s_addr) &&
3861 (!s->be->except_mask_to.s_addr ||
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003862 (((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 +02003863 != s->be->except_to.s_addr))) {
Maik Broemme2850cb42009-04-17 18:53:21 +02003864 int len;
3865 unsigned char *pn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02003866 pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->conn->addr.to)->sin_addr;
Maik Broemme2850cb42009-04-17 18:53:21 +02003867
3868 /* Note: we rely on the backend to get the header name to be used for
3869 * x-original-to, because the header is really meant for the backends.
3870 * However, if the backend did not specify any option, we have to rely
3871 * on the frontend's header name.
3872 */
3873 if (s->be->orgto_hdr_len) {
3874 len = s->be->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003875 memcpy(trash.str, s->be->orgto_hdr_name, len);
Maik Broemme2850cb42009-04-17 18:53:21 +02003876 } else {
3877 len = s->fe->orgto_hdr_len;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003878 memcpy(trash.str, s->fe->orgto_hdr_name, len);
Willy Tarreaub86db342009-11-30 11:50:16 +01003879 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003880 len += sprintf(trash.str + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]);
Maik Broemme2850cb42009-04-17 18:53:21 +02003881
Willy Tarreau19d14ef2012-10-29 16:51:55 +01003882 if (unlikely(http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, len) < 0))
Maik Broemme2850cb42009-04-17 18:53:21 +02003883 goto return_bad_req;
3884 }
3885 }
3886 }
3887
Willy Tarreau50fc7772012-11-11 22:19:57 +01003888 /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set.
3889 * If an "Upgrade" token is found, the header is left untouched in order not to have
3890 * to deal with some servers bugs : some of them fail an Upgrade if anything but
3891 * "Upgrade" is present in the Connection header.
3892 */
3893 if (!(txn->flags & TX_HDR_CONN_UPG) &&
3894 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
3895 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003896 unsigned int want_flags = 0;
3897
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003898 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau22a95342010-09-29 14:31:41 +02003899 if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL ||
3900 ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) &&
3901 !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003902 want_flags |= TX_CON_CLO_SET;
3903 } else {
Willy Tarreau22a95342010-09-29 14:31:41 +02003904 if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL &&
3905 !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) ||
3906 ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003907 want_flags |= TX_CON_KAL_SET;
3908 }
3909
3910 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01003911 http_change_connection_header(txn, msg, want_flags);
Willy Tarreau59234e92008-11-30 23:51:27 +01003912 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01003913
Willy Tarreaubbf0b372010-01-18 16:54:40 +01003914
Willy Tarreau522d6c02009-12-06 18:49:18 +01003915 /* If we have no server assigned yet and we're balancing on url_param
3916 * with a POST request, we may be interested in checking the body for
3917 * that parameter. This will be done in another analyser.
Willy Tarreau59234e92008-11-30 23:51:27 +01003918 */
3919 if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) &&
3920 s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01003921 s->be->url_param_post_limit != 0 &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003922 (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003923 channel_dont_connect(req);
Willy Tarreau522d6c02009-12-06 18:49:18 +01003924 req->analysers |= AN_REQ_HTTP_BODY;
Willy Tarreau59234e92008-11-30 23:51:27 +01003925 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02003926
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003927 if (msg->flags & HTTP_MSGF_XFER_LEN) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01003928 req->analysers |= AN_REQ_HTTP_XFER_BODY;
Willy Tarreau5e205522011-12-17 16:34:27 +01003929#ifdef TCP_QUICKACK
3930 /* We expect some data from the client. Unless we know for sure
3931 * we already have a full request, we have to re-enable quick-ack
3932 * in case we previously disabled it, otherwise we might cause
3933 * the client to delay further data.
3934 */
3935 if ((s->listener->options & LI_O_NOQUICKACK) &&
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01003936 ((msg->flags & HTTP_MSGF_TE_CHNK) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02003937 (msg->body_len > req->buf->i - txn->req.eoh - 2)))
Willy Tarreau7f7ad912012-11-11 19:27:15 +01003938 setsockopt(s->si[0].conn->t.sock.fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one));
Willy Tarreau5e205522011-12-17 16:34:27 +01003939#endif
3940 }
Willy Tarreau03945942009-12-22 16:50:27 +01003941
Willy Tarreau59234e92008-11-30 23:51:27 +01003942 /*************************************************************
3943 * OK, that's finished for the headers. We have done what we *
3944 * could. Let's switch to the DATA state. *
3945 ************************************************************/
Willy Tarreau522d6c02009-12-06 18:49:18 +01003946 req->analyse_exp = TICK_ETERNITY;
3947 req->analysers &= ~an_bit;
Willy Tarreaubaaee002006-06-26 02:48:02 +02003948
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003949 /* if the server closes the connection, we want to immediately react
3950 * and close the socket to save packets and syscalls.
3951 */
Willy Tarreau40f151a2012-12-20 12:10:09 +01003952 if (!(req->analysers & AN_REQ_HTTP_XFER_BODY))
3953 req->cons->flags |= SI_FL_NOHALF;
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02003954
Willy Tarreau59234e92008-11-30 23:51:27 +01003955 s->logs.tv_request = now;
Willy Tarreau59234e92008-11-30 23:51:27 +01003956 /* OK let's go on with the BODY now */
3957 return 1;
Willy Tarreau06619262006-12-17 08:37:22 +01003958
Willy Tarreau59234e92008-11-30 23:51:27 +01003959 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau4076a152009-04-02 15:18:36 +02003960 if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) {
Willy Tarreauf073a832009-03-01 23:21:47 +01003961 /* we detected a parsing error. We want to archive this request
3962 * in the dedicated proxy area for later troubleshooting.
3963 */
Willy Tarreau8a0cef22012-03-09 13:39:23 +01003964 http_capture_bad_message(&s->fe->invalid_req, s, msg, msg->msg_state, s->fe);
Willy Tarreauf073a832009-03-01 23:21:47 +01003965 }
Willy Tarreau4076a152009-04-02 15:18:36 +02003966
Willy Tarreau59234e92008-11-30 23:51:27 +01003967 txn->req.msg_state = HTTP_MSG_ERROR;
3968 txn->status = 400;
3969 req->analysers = 0;
Willy Tarreau783f2582012-09-04 12:19:04 +02003970 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003971
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01003972 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02003973 if (s->listener->counters)
3974 s->listener->counters->failed_req++;
Willy Tarreauadfb8562008-08-11 15:24:42 +02003975
Willy Tarreau59234e92008-11-30 23:51:27 +01003976 if (!(s->flags & SN_ERR_MASK))
3977 s->flags |= SN_ERR_PRXCOND;
3978 if (!(s->flags & SN_FINST_MASK))
3979 s->flags |= SN_FINST_R;
Willy Tarreaudafde432008-08-17 01:00:46 +02003980 return 0;
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02003981}
Willy Tarreauadfb8562008-08-11 15:24:42 +02003982
Willy Tarreau60b85b02008-11-30 23:28:40 +01003983/* This function is an analyser which processes the HTTP tarpit. It always
3984 * returns zero, at the beginning because it prevents any other processing
3985 * from occurring, and at the end because it terminates the request.
3986 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02003987int http_process_tarpit(struct session *s, struct channel *req, int an_bit)
Willy Tarreau60b85b02008-11-30 23:28:40 +01003988{
3989 struct http_txn *txn = &s->txn;
3990
3991 /* This connection is being tarpitted. The CLIENT side has
3992 * already set the connect expiration date to the right
3993 * timeout. We just have to check that the client is still
3994 * there and that the timeout has not expired.
3995 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02003996 channel_dont_connect(req);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02003997 if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 &&
Willy Tarreau60b85b02008-11-30 23:28:40 +01003998 !tick_is_expired(req->analyse_exp, now_ms))
3999 return 0;
4000
4001 /* We will set the queue timer to the time spent, just for
4002 * logging purposes. We fake a 500 server error, so that the
4003 * attacker will not suspect his connection has been tarpitted.
4004 * It will not cause trouble to the logs because we can exclude
4005 * the tarpitted connections by filtering on the 'PT' status flags.
4006 */
Willy Tarreau60b85b02008-11-30 23:28:40 +01004007 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
4008
4009 txn->status = 500;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004010 if (!(req->flags & CF_READ_ERROR))
Willy Tarreau783f2582012-09-04 12:19:04 +02004011 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500));
Willy Tarreau60b85b02008-11-30 23:28:40 +01004012
4013 req->analysers = 0;
4014 req->analyse_exp = TICK_ETERNITY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004015
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004016 s->fe->fe_counters.failed_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02004017 if (s->listener->counters)
4018 s->listener->counters->failed_req++;
Willy Tarreau60b85b02008-11-30 23:28:40 +01004019
Willy Tarreau60b85b02008-11-30 23:28:40 +01004020 if (!(s->flags & SN_ERR_MASK))
4021 s->flags |= SN_ERR_PRXCOND;
4022 if (!(s->flags & SN_FINST_MASK))
4023 s->flags |= SN_FINST_T;
4024 return 0;
4025}
4026
Willy Tarreaud34af782008-11-30 23:36:37 +01004027/* This function is an analyser which processes the HTTP request body. It looks
4028 * for parameters to be used for the load balancing algorithm (url_param). It
4029 * must only be called after the standard HTTP request processing has occurred,
4030 * because it expects the request to be parsed. It returns zero if it needs to
4031 * read more data, or 1 once it has completed its analysis.
4032 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004033int http_process_request_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud34af782008-11-30 23:36:37 +01004034{
Willy Tarreau522d6c02009-12-06 18:49:18 +01004035 struct http_txn *txn = &s->txn;
Willy Tarreaud34af782008-11-30 23:36:37 +01004036 struct http_msg *msg = &s->txn.req;
Willy Tarreaud34af782008-11-30 23:36:37 +01004037 long long limit = s->be->url_param_post_limit;
Willy Tarreaud34af782008-11-30 23:36:37 +01004038
4039 /* We have to parse the HTTP request body to find any required data.
4040 * "balance url_param check_post" should have been the only way to get
4041 * into this. We were brought here after HTTP header analysis, so all
4042 * related structures are ready.
4043 */
4044
Willy Tarreau522d6c02009-12-06 18:49:18 +01004045 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4046 goto missing_data;
4047
4048 if (msg->msg_state < HTTP_MSG_100_SENT) {
4049 /* If we have HTTP/1.1 and Expect: 100-continue, then we must
4050 * send an HTTP/1.1 100 Continue intermediate response.
4051 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004052 if (msg->flags & HTTP_MSGF_VER_11) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004053 struct hdr_ctx ctx;
4054 ctx.idx = 0;
4055 /* Expect is allowed in 1.1, look for it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004056 if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) &&
Willy Tarreau522d6c02009-12-06 18:49:18 +01004057 unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) {
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004058 bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004059 }
4060 }
4061 msg->msg_state = HTTP_MSG_100_SENT;
4062 }
4063
4064 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004065 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004066 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004067 * is still null. We must save the body in msg->next because it
4068 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004069 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004070 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004071
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004072 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau522d6c02009-12-06 18:49:18 +01004073 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
4074 else
4075 msg->msg_state = HTTP_MSG_DATA;
4076 }
4077
4078 if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004079 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004080 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004081 * TRAILERS state.
Willy Tarreau115acb92009-12-26 13:56:06 +01004082 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004083 int ret = http_parse_chunk_size(msg);
Willy Tarreaud34af782008-11-30 23:36:37 +01004084
Willy Tarreau115acb92009-12-26 13:56:06 +01004085 if (!ret)
4086 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004087 else if (ret < 0) {
4088 session_inc_http_err_ctr(s);
Willy Tarreau522d6c02009-12-06 18:49:18 +01004089 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004090 }
Willy Tarreaud34af782008-11-30 23:36:37 +01004091 }
4092
Willy Tarreaud98cf932009-12-27 22:54:55 +01004093 /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state.
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004094 * We have the first data byte is in msg->sov. We're waiting for at
4095 * least <url_param_post_limit> bytes after msg->sov.
Willy Tarreaud34af782008-11-30 23:36:37 +01004096 */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004097
Willy Tarreau124d9912011-03-01 20:30:48 +01004098 if (msg->body_len < limit)
4099 limit = msg->body_len;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004100
Willy Tarreau9b28e032012-10-12 23:49:43 +02004101 if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */
Willy Tarreau522d6c02009-12-06 18:49:18 +01004102 goto http_end;
4103
4104 missing_data:
4105 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004106 if (buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreauda7ff642010-06-23 11:44:09 +02004107 session_inc_http_err_ctr(s);
Willy Tarreau115acb92009-12-26 13:56:06 +01004108 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004109 }
Willy Tarreau115acb92009-12-26 13:56:06 +01004110
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004111 if ((req->flags & CF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) {
Willy Tarreau522d6c02009-12-06 18:49:18 +01004112 txn->status = 408;
Willy Tarreau783f2582012-09-04 12:19:04 +02004113 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408));
Willy Tarreau79ebac62010-06-07 13:47:49 +02004114
4115 if (!(s->flags & SN_ERR_MASK))
4116 s->flags |= SN_ERR_CLITO;
4117 if (!(s->flags & SN_FINST_MASK))
4118 s->flags |= SN_FINST_D;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004119 goto return_err_msg;
Willy Tarreaud34af782008-11-30 23:36:37 +01004120 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004121
4122 /* we get here if we need to wait for more data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004123 if (!(req->flags & (CF_SHUTR | CF_READ_ERROR)) && !buffer_full(req->buf, global.tune.maxrewrite)) {
Willy Tarreaud34af782008-11-30 23:36:37 +01004124 /* Not enough data. We'll re-use the http-request
4125 * timeout here. Ideally, we should set the timeout
4126 * relative to the accept() date. We just set the
4127 * request timeout once at the beginning of the
4128 * request.
4129 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004130 channel_dont_connect(req);
Willy Tarreaud34af782008-11-30 23:36:37 +01004131 if (!tick_isset(req->analyse_exp))
Willy Tarreaucd7afc02009-07-12 10:03:17 +02004132 req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq);
Willy Tarreaud34af782008-11-30 23:36:37 +01004133 return 0;
4134 }
Willy Tarreau522d6c02009-12-06 18:49:18 +01004135
4136 http_end:
4137 /* The situation will not evolve, so let's give up on the analysis. */
4138 s->logs.tv_request = now; /* update the request timer to reflect full request */
4139 req->analysers &= ~an_bit;
4140 req->analyse_exp = TICK_ETERNITY;
4141 return 1;
4142
4143 return_bad_req: /* let's centralize all bad requests */
4144 txn->req.msg_state = HTTP_MSG_ERROR;
4145 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004146 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreau522d6c02009-12-06 18:49:18 +01004147
Willy Tarreau79ebac62010-06-07 13:47:49 +02004148 if (!(s->flags & SN_ERR_MASK))
4149 s->flags |= SN_ERR_PRXCOND;
4150 if (!(s->flags & SN_FINST_MASK))
4151 s->flags |= SN_FINST_R;
4152
Willy Tarreau522d6c02009-12-06 18:49:18 +01004153 return_err_msg:
4154 req->analysers = 0;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004155 s->fe->fe_counters.failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004156 if (s->listener->counters)
4157 s->listener->counters->failed_req++;
Willy Tarreau522d6c02009-12-06 18:49:18 +01004158 return 0;
Willy Tarreaud34af782008-11-30 23:36:37 +01004159}
4160
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004161/* send a server's name with an outgoing request over an established connection.
4162 * Note: this function is designed to be called once the request has been scheduled
4163 * for being forwarded. This is the reason why it rewinds the buffer before
4164 * proceeding.
4165 */
Willy Tarreau45c0d982012-03-09 12:11:57 +01004166int http_send_name_header(struct http_txn *txn, struct proxy* be, const char* srv_name) {
Mark Lamourinec2247f02012-01-04 13:02:01 -05004167
4168 struct hdr_ctx ctx;
4169
Mark Lamourinec2247f02012-01-04 13:02:01 -05004170 char *hdr_name = be->server_id_hdr_name;
4171 int hdr_name_len = be->server_id_hdr_len;
Willy Tarreau394db372012-10-12 22:40:39 +02004172 struct channel *chn = txn->req.chn;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004173 char *hdr_val;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004174 unsigned int old_o, old_i;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004175
William Lallemandd9e90662012-01-30 17:27:17 +01004176 ctx.idx = 0;
4177
Willy Tarreau9b28e032012-10-12 23:49:43 +02004178 old_o = chn->buf->o;
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004179 if (old_o) {
4180 /* The request was already skipped, let's restore it */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004181 b_rew(chn->buf, old_o);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004182 }
4183
Willy Tarreau9b28e032012-10-12 23:49:43 +02004184 old_i = chn->buf->i;
4185 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 -05004186 /* remove any existing values from the header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01004187 http_remove_header2(&txn->req, &txn->hdr_idx, &ctx);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004188 }
4189
4190 /* Add the new header requested with the server value */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004191 hdr_val = trash.str;
Mark Lamourinec2247f02012-01-04 13:02:01 -05004192 memcpy(hdr_val, hdr_name, hdr_name_len);
4193 hdr_val += hdr_name_len;
4194 *hdr_val++ = ':';
4195 *hdr_val++ = ' ';
Willy Tarreau19d14ef2012-10-29 16:51:55 +01004196 hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val);
4197 http_header_add_tail2(&txn->req, &txn->hdr_idx, trash.str, hdr_val - trash.str);
Mark Lamourinec2247f02012-01-04 13:02:01 -05004198
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004199 if (old_o) {
4200 /* If this was a forwarded request, we must readjust the amount of
4201 * data to be forwarded in order to take into account the size
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004202 * variations. Note that if the request was already scheduled for
4203 * forwarding, it had its req->sol pointing to the body, which
4204 * must then be updated too.
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004205 */
Willy Tarreau2fef9b12013-03-26 01:08:21 +01004206 txn->req.sol += chn->buf->i - old_i;
Willy Tarreau9b28e032012-10-12 23:49:43 +02004207 b_adv(chn->buf, old_o + chn->buf->i - old_i);
Willy Tarreaud1de8af2012-05-18 22:12:14 +02004208 }
4209
Mark Lamourinec2247f02012-01-04 13:02:01 -05004210 return 0;
4211}
4212
Willy Tarreau610ecce2010-01-04 21:15:02 +01004213/* Terminate current transaction and prepare a new one. This is very tricky
4214 * right now but it works.
4215 */
4216void http_end_txn_clean_session(struct session *s)
4217{
4218 /* FIXME: We need a more portable way of releasing a backend's and a
4219 * server's connections. We need a safer way to reinitialize buffer
4220 * flags. We also need a more accurate method for computing per-request
4221 * data.
4222 */
4223 http_silent_debug(__LINE__, s);
4224
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02004225 s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF;
Willy Tarreau73b013b2012-05-21 16:31:45 +02004226 si_shutr(s->req->cons);
4227 si_shutw(s->req->cons);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004228
4229 http_silent_debug(__LINE__, s);
4230
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004231 if (s->flags & SN_BE_ASSIGNED) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004232 s->be->beconn--;
Willy Tarreau2d5cd472012-03-01 23:34:37 +01004233 if (unlikely(s->srv_conn))
4234 sess_change_server(s, NULL);
4235 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004236
4237 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
4238 session_process_counters(s);
Willy Tarreauf059a0f2010-08-03 16:29:52 +02004239 session_stop_backend_counters(s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004240
4241 if (s->txn.status) {
4242 int n;
4243
4244 n = s->txn.status / 100;
4245 if (n < 1 || n > 5)
4246 n = 0;
4247
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004248 if (s->fe->mode == PR_MODE_HTTP) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004249 s->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004250 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004251 s->fe->fe_counters.p.http.comp_rsp++;
4252 }
Willy Tarreau24657792010-02-26 10:30:28 +01004253 if ((s->flags & SN_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004254 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004255 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004256 s->be->be_counters.p.http.cum_req++;
Willy Tarreau8139b992012-11-27 07:35:31 +01004257 if (s->comp_algo && (s->flags & SN_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01004258 s->be->be_counters.p.http.comp_rsp++;
4259 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004260 }
4261
4262 /* don't count other requests' data */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004263 s->logs.bytes_in -= s->req->buf->i;
4264 s->logs.bytes_out -= s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004265
4266 /* let's do a final log if we need it */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01004267 if (!LIST_ISEMPTY(&s->fe->logformat) && s->logs.logwait &&
Willy Tarreau610ecce2010-01-04 21:15:02 +01004268 !(s->flags & SN_MONITOR) &&
4269 (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) {
4270 s->do_log(s);
4271 }
4272
4273 s->logs.accept_date = date; /* user-visible date for logging */
4274 s->logs.tv_accept = now; /* corrected date for internal use */
4275 tv_zero(&s->logs.tv_request);
4276 s->logs.t_queue = -1;
4277 s->logs.t_connect = -1;
4278 s->logs.t_data = -1;
4279 s->logs.t_close = 0;
4280 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
4281 s->logs.srv_queue_size = 0; /* we will get this number soon */
4282
Willy Tarreau9b28e032012-10-12 23:49:43 +02004283 s->logs.bytes_in = s->req->total = s->req->buf->i;
4284 s->logs.bytes_out = s->rep->total = s->rep->buf->i;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004285
4286 if (s->pend_pos)
4287 pendconn_free(s->pend_pos);
4288
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004289 if (objt_server(s->target)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004290 if (s->flags & SN_CURR_SESS) {
4291 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004292 objt_server(s->target)->cur_sess--;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004293 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004294 if (may_dequeue_tasks(objt_server(s->target), s->be))
4295 process_srv_queue(objt_server(s->target));
Willy Tarreau610ecce2010-01-04 21:15:02 +01004296 }
4297
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004298 s->target = NULL;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004299
4300 s->req->cons->state = s->req->cons->prev_state = SI_ST_INI;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02004301 s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */
4302 s->req->cons->conn->flags = CO_FL_NONE;
Willy Tarreau14cba4b2012-11-30 17:33:05 +01004303 s->req->cons->conn->err_code = CO_ER_NONE;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004304 s->req->cons->err_type = SI_ET_NONE;
Willy Tarreau0b3a4112011-03-27 19:16:56 +02004305 s->req->cons->conn_retries = 0; /* used for logging too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004306 s->req->cons->err_loc = NULL;
4307 s->req->cons->exp = TICK_ETERNITY;
4308 s->req->cons->flags = SI_FL_NONE;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004309 s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT);
4310 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 +02004311 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 +01004312 s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE);
Willy Tarreau543db622012-11-15 16:41:22 +01004313
4314 if (s->flags & SN_COMP_READY)
4315 s->comp_algo->end(&s->comp_ctx);
4316 s->comp_algo = NULL;
4317 s->flags &= ~SN_COMP_READY;
4318
Willy Tarreau610ecce2010-01-04 21:15:02 +01004319 s->txn.meth = 0;
4320 http_reset_txn(s);
Willy Tarreaufcffa692010-01-10 14:21:19 +01004321 s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ;
Willy Tarreauee55dc02010-06-01 10:56:34 +02004322 if (s->fe->options2 & PR_O2_INDEPSTR)
Willy Tarreau610ecce2010-01-04 21:15:02 +01004323 s->req->cons->flags |= SI_FL_INDEP_STR;
4324
Willy Tarreau96e31212011-05-30 18:10:30 +02004325 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004326 s->req->flags |= CF_NEVER_WAIT;
4327 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +02004328 }
4329
Willy Tarreau610ecce2010-01-04 21:15:02 +01004330 /* if the request buffer is not empty, it means we're
4331 * about to process another request, so send pending
4332 * data with MSG_MORE to merge TCP packets when possible.
Willy Tarreau065e8332010-01-08 00:30:20 +01004333 * Just don't do this if the buffer is close to be full,
4334 * because the request will wait for it to flush a little
4335 * bit before proceeding.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004336 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02004337 if (s->req->buf->i) {
4338 if (s->rep->buf->o &&
4339 !buffer_full(s->rep->buf, global.tune.maxrewrite) &&
4340 bi_end(s->rep->buf) <= s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004341 s->rep->flags |= CF_EXPECT_MORE;
Willy Tarreau065e8332010-01-08 00:30:20 +01004342 }
Willy Tarreau90deb182010-01-07 00:20:41 +01004343
4344 /* we're removing the analysers, we MUST re-enable events detection */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004345 channel_auto_read(s->req);
4346 channel_auto_close(s->req);
4347 channel_auto_read(s->rep);
4348 channel_auto_close(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004349
Willy Tarreau342b11c2010-11-24 16:22:09 +01004350 s->req->analysers = s->listener->analysers;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004351 s->rep->analysers = 0;
4352
4353 http_silent_debug(__LINE__, s);
4354}
4355
4356
4357/* This function updates the request state machine according to the response
4358 * state machine and buffer flags. It returns 1 if it changes anything (flag
4359 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4360 * it is only used to find when a request/response couple is complete. Both
4361 * this function and its equivalent should loop until both return zero. It
4362 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4363 */
4364int http_sync_req_state(struct session *s)
4365{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004366 struct channel *chn = s->req;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004367 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004368 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004369 unsigned int old_state = txn->req.msg_state;
4370
4371 http_silent_debug(__LINE__, s);
4372 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY))
4373 return 0;
4374
4375 if (txn->req.msg_state == HTTP_MSG_DONE) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004376 /* No need to read anymore, the request was completely parsed.
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004377 * We can shut the read side unless we want to abort_on_close,
4378 * or we have a POST request. The issue with POST requests is
4379 * that some browsers still send a CRLF after the request, and
4380 * this CRLF must be read so that it does not remain in the kernel
4381 * buffers, otherwise a close could cause an RST on some systems
4382 * (eg: Linux).
Willy Tarreau90deb182010-01-07 00:20:41 +01004383 */
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004384 if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST)
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004385 channel_dont_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004386
Willy Tarreau40f151a2012-12-20 12:10:09 +01004387 /* if the server closes the connection, we want to immediately react
4388 * and close the socket to save packets and syscalls.
4389 */
4390 chn->cons->flags |= SI_FL_NOHALF;
4391
Willy Tarreau610ecce2010-01-04 21:15:02 +01004392 if (txn->rsp.msg_state == HTTP_MSG_ERROR)
4393 goto wait_other_side;
4394
4395 if (txn->rsp.msg_state < HTTP_MSG_DONE) {
4396 /* The server has not finished to respond, so we
4397 * don't want to move in order not to upset it.
4398 */
4399 goto wait_other_side;
4400 }
4401
4402 if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) {
4403 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004404 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004405 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004406 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004407 goto wait_other_side;
4408 }
4409
4410 /* When we get here, it means that both the request and the
4411 * response have finished receiving. Depending on the connection
4412 * mode, we'll have to wait for the last bytes to leave in either
4413 * direction, and sometimes for a close to be effective.
4414 */
4415
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004416 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4417 /* Server-close mode : queue a connection close to the server */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004418 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW)))
4419 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004420 }
4421 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4422 /* Option forceclose is set, or either side wants to close,
4423 * let's enforce it now that we're not expecting any new
4424 * data to come. The caller knows the session is complete
4425 * once both states are CLOSED.
4426 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004427 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4428 channel_shutr_now(chn);
4429 channel_shutw_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004430 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004431 }
4432 else {
4433 /* The last possible modes are keep-alive and tunnel. Since tunnel
4434 * mode does not set the body analyser, we can't reach this place
4435 * in tunnel mode, so we're left with keep-alive only.
4436 * This mode is currently not implemented, we switch to tunnel mode.
4437 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004438 channel_auto_read(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004439 txn->req.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004440 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004441 }
4442
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004443 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004444 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004445 chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004446
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004447 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004448 txn->req.msg_state = HTTP_MSG_CLOSING;
4449 goto http_msg_closing;
4450 }
4451 else {
4452 txn->req.msg_state = HTTP_MSG_CLOSED;
4453 goto http_msg_closed;
4454 }
4455 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004456 goto wait_other_side;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004457 }
4458
4459 if (txn->req.msg_state == HTTP_MSG_CLOSING) {
4460 http_msg_closing:
4461 /* nothing else to forward, just waiting for the output buffer
4462 * to be empty and for the shutw_now to take effect.
4463 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004464 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004465 txn->req.msg_state = HTTP_MSG_CLOSED;
4466 goto http_msg_closed;
4467 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004468 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004469 txn->req.msg_state = HTTP_MSG_ERROR;
4470 goto wait_other_side;
4471 }
4472 }
4473
4474 if (txn->req.msg_state == HTTP_MSG_CLOSED) {
4475 http_msg_closed:
4476 goto wait_other_side;
4477 }
4478
4479 wait_other_side:
4480 http_silent_debug(__LINE__, s);
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004481 return txn->req.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004482}
4483
4484
4485/* This function updates the response state machine according to the request
4486 * state machine and buffer flags. It returns 1 if it changes anything (flag
4487 * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as
4488 * it is only used to find when a request/response couple is complete. Both
4489 * this function and its equivalent should loop until both return zero. It
4490 * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR.
4491 */
4492int http_sync_res_state(struct session *s)
4493{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004494 struct channel *chn = s->rep;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004495 struct http_txn *txn = &s->txn;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004496 unsigned int old_flags = chn->flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004497 unsigned int old_state = txn->rsp.msg_state;
4498
4499 http_silent_debug(__LINE__, s);
4500 if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY))
4501 return 0;
4502
4503 if (txn->rsp.msg_state == HTTP_MSG_DONE) {
4504 /* In theory, we don't need to read anymore, but we must
Willy Tarreau90deb182010-01-07 00:20:41 +01004505 * still monitor the server connection for a possible close
4506 * while the request is being uploaded, so we don't disable
4507 * reading.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004508 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004509 /* channel_dont_read(chn); */
Willy Tarreau610ecce2010-01-04 21:15:02 +01004510
4511 if (txn->req.msg_state == HTTP_MSG_ERROR)
4512 goto wait_other_side;
4513
4514 if (txn->req.msg_state < HTTP_MSG_DONE) {
4515 /* The client seems to still be sending data, probably
4516 * because we got an error response during an upload.
4517 * We have the choice of either breaking the connection
4518 * or letting it pass through. Let's do the later.
4519 */
4520 goto wait_other_side;
4521 }
4522
4523 if (txn->req.msg_state == HTTP_MSG_TUNNEL) {
4524 /* if any side switches to tunnel mode, the other one does too */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004525 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004526 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004527 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004528 goto wait_other_side;
4529 }
4530
4531 /* When we get here, it means that both the request and the
4532 * response have finished receiving. Depending on the connection
4533 * mode, we'll have to wait for the last bytes to leave in either
4534 * direction, and sometimes for a close to be effective.
4535 */
4536
4537 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
4538 /* Server-close mode : shut read and wait for the request
4539 * side to close its output buffer. The caller will detect
4540 * when we're in DONE and the other is in CLOSED and will
4541 * catch that for the final cleanup.
4542 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004543 if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW)))
4544 channel_shutr_now(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004545 }
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004546 else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
4547 /* Option forceclose is set, or either side wants to close,
4548 * let's enforce it now that we're not expecting any new
4549 * data to come. The caller knows the session is complete
4550 * once both states are CLOSED.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004551 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004552 if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) {
4553 channel_shutr_now(chn);
4554 channel_shutw_now(chn);
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004555 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004556 }
4557 else {
Willy Tarreaucce7fa42010-01-16 23:19:39 +01004558 /* The last possible modes are keep-alive and tunnel. Since tunnel
4559 * mode does not set the body analyser, we can't reach this place
4560 * in tunnel mode, so we're left with keep-alive only.
4561 * This mode is currently not implemented, we switch to tunnel mode.
Willy Tarreau610ecce2010-01-04 21:15:02 +01004562 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004563 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004564 txn->rsp.msg_state = HTTP_MSG_TUNNEL;
Willy Tarreaufc47f912012-10-20 10:38:09 +02004565 chn->flags |= CF_NEVER_WAIT;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004566 }
4567
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004568 if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004569 /* if we've just closed an output, let's switch */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004570 if (!channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004571 txn->rsp.msg_state = HTTP_MSG_CLOSING;
4572 goto http_msg_closing;
4573 }
4574 else {
4575 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4576 goto http_msg_closed;
4577 }
4578 }
4579 goto wait_other_side;
4580 }
4581
4582 if (txn->rsp.msg_state == HTTP_MSG_CLOSING) {
4583 http_msg_closing:
4584 /* nothing else to forward, just waiting for the output buffer
4585 * to be empty and for the shutw_now to take effect.
4586 */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004587 if (channel_is_empty(chn)) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004588 txn->rsp.msg_state = HTTP_MSG_CLOSED;
4589 goto http_msg_closed;
4590 }
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004591 else if (chn->flags & CF_SHUTW) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01004592 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004593 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004594 if (objt_server(s->target))
4595 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004596 goto wait_other_side;
4597 }
4598 }
4599
4600 if (txn->rsp.msg_state == HTTP_MSG_CLOSED) {
4601 http_msg_closed:
4602 /* drop any pending data */
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004603 bi_erase(chn);
4604 channel_auto_close(chn);
4605 channel_auto_read(chn);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004606 goto wait_other_side;
4607 }
4608
4609 wait_other_side:
4610 http_silent_debug(__LINE__, s);
Willy Tarreaufc47f912012-10-20 10:38:09 +02004611 /* We force the response to leave immediately if we're waiting for the
4612 * other side, since there is no pending shutdown to push it out.
4613 */
4614 if (!channel_is_empty(chn))
4615 chn->flags |= CF_SEND_DONTWAIT;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02004616 return txn->rsp.msg_state != old_state || chn->flags != old_flags;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004617}
4618
4619
4620/* Resync the request and response state machines. Return 1 if either state
4621 * changes.
4622 */
4623int http_resync_states(struct session *s)
4624{
4625 struct http_txn *txn = &s->txn;
4626 int old_req_state = txn->req.msg_state;
4627 int old_res_state = txn->rsp.msg_state;
4628
4629 http_silent_debug(__LINE__, s);
4630 http_sync_req_state(s);
4631 while (1) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004632 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004633 if (!http_sync_res_state(s))
4634 break;
Willy Tarreau90deb182010-01-07 00:20:41 +01004635 http_silent_debug(__LINE__, s);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004636 if (!http_sync_req_state(s))
4637 break;
4638 }
4639 http_silent_debug(__LINE__, s);
4640 /* OK, both state machines agree on a compatible state.
4641 * There are a few cases we're interested in :
4642 * - HTTP_MSG_TUNNEL on either means we have to disable both analysers
4643 * - HTTP_MSG_CLOSED on both sides means we've reached the end in both
4644 * directions, so let's simply disable both analysers.
4645 * - HTTP_MSG_CLOSED on the response only means we must abort the
4646 * request.
4647 * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response
4648 * with server-close mode means we've completed one request and we
4649 * must re-initialize the server connection.
4650 */
4651
4652 if (txn->req.msg_state == HTTP_MSG_TUNNEL ||
4653 txn->rsp.msg_state == HTTP_MSG_TUNNEL ||
4654 (txn->req.msg_state == HTTP_MSG_CLOSED &&
4655 txn->rsp.msg_state == HTTP_MSG_CLOSED)) {
4656 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004657 channel_auto_close(s->req);
4658 channel_auto_read(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004659 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004660 channel_auto_close(s->rep);
4661 channel_auto_read(s->rep);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004662 }
Willy Tarreau40f151a2012-12-20 12:10:09 +01004663 else if ((txn->req.msg_state >= HTTP_MSG_DONE &&
4664 (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) ||
Willy Tarreau2fa144c2010-01-04 23:13:26 +01004665 txn->rsp.msg_state == HTTP_MSG_ERROR ||
Willy Tarreau40f151a2012-12-20 12:10:09 +01004666 txn->req.msg_state == HTTP_MSG_ERROR) {
Willy Tarreau90deb182010-01-07 00:20:41 +01004667 s->rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004668 channel_auto_close(s->rep);
4669 channel_auto_read(s->rep);
Willy Tarreau90deb182010-01-07 00:20:41 +01004670 s->req->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004671 channel_abort(s->req);
4672 channel_auto_close(s->req);
4673 channel_auto_read(s->req);
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02004674 bi_erase(s->req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004675 }
4676 else if (txn->req.msg_state == HTTP_MSG_CLOSED &&
4677 txn->rsp.msg_state == HTTP_MSG_DONE &&
4678 ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) {
4679 /* server-close: terminate this server connection and
4680 * reinitialize a fresh-new transaction.
4681 */
4682 http_end_txn_clean_session(s);
4683 }
4684
4685 http_silent_debug(__LINE__, s);
4686 return txn->req.msg_state != old_req_state ||
4687 txn->rsp.msg_state != old_res_state;
4688}
4689
Willy Tarreaud98cf932009-12-27 22:54:55 +01004690/* This function is an analyser which forwards request body (including chunk
4691 * sizes if any). It is called as soon as we must forward, even if we forward
4692 * zero byte. The only situation where it must not be called is when we're in
4693 * tunnel mode and we want to forward till the close. It's used both to forward
4694 * remaining data and to resync after end of body. It expects the msg_state to
4695 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
4696 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01004697 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02004698 * bytes of pending data + the headers if not already done (between sol and sov).
4699 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004700 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004701int http_request_forward_body(struct session *s, struct channel *req, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004702{
4703 struct http_txn *txn = &s->txn;
4704 struct http_msg *msg = &s->txn.req;
4705
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004706 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
4707 return 0;
4708
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004709 if ((req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02004710 ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02004711 /* Output closed while we were sending data. We must abort and
4712 * wake the other side up.
4713 */
4714 msg->msg_state = HTTP_MSG_ERROR;
4715 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01004716 return 1;
4717 }
4718
Willy Tarreau4fe41902010-06-07 22:27:41 +02004719 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004720 channel_auto_close(req);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004721
4722 /* Note that we don't have to send 100-continue back because we don't
4723 * need the data to complete our job, and it's up to the server to
4724 * decide whether to return 100, 417 or anything else in return of
4725 * an "Expect: 100-continue" header.
4726 */
4727
4728 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01004729 /* we have msg->sov which points to the first byte of message body.
Willy Tarreau9b28e032012-10-12 23:49:43 +02004730 * req->buf->p still points to the beginning of the message and msg->sol
Willy Tarreau26927362012-05-18 23:22:52 +02004731 * is still null. We must save the body in msg->next because it
4732 * survives buffer re-alignments.
Willy Tarreaud98cf932009-12-27 22:54:55 +01004733 */
Willy Tarreauea1175a2012-03-05 15:52:30 +01004734 msg->next = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004735
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004736 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004737 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02004738 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01004739 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004740 }
4741
Willy Tarreaud98cf932009-12-27 22:54:55 +01004742 while (1) {
Willy Tarreauea953162012-05-18 23:41:28 +02004743 unsigned int bytes;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004744
Willy Tarreau610ecce2010-01-04 21:15:02 +01004745 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02004746 /* we may have some data pending between sol and sov */
Willy Tarreau26927362012-05-18 23:22:52 +02004747 bytes = msg->sov - msg->sol;
Willy Tarreaud8ee85a2011-03-28 16:06:28 +02004748 if (msg->chunk_len || bytes) {
Willy Tarreau26927362012-05-18 23:22:52 +02004749 msg->sol = msg->sov;
Willy Tarreaua458b672012-03-05 11:17:50 +01004750 msg->next -= bytes; /* will be forwarded */
Willy Tarreauea953162012-05-18 23:41:28 +02004751 msg->chunk_len += bytes;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004752 msg->chunk_len -= channel_forward(req, msg->chunk_len);
Willy Tarreau638cd022010-01-03 07:42:04 +01004753 }
Willy Tarreau5523b322009-12-29 12:05:52 +01004754
Willy Tarreaucaabe412010-01-03 23:08:28 +01004755 if (msg->msg_state == HTTP_MSG_DATA) {
4756 /* must still forward */
4757 if (req->to_forward)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004758 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004759
4760 /* nothing left to forward */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004761 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004762 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004763 else
Willy Tarreaucaabe412010-01-03 23:08:28 +01004764 msg->msg_state = HTTP_MSG_DONE;
Willy Tarreaucaabe412010-01-03 23:08:28 +01004765 }
4766 else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) {
Willy Tarreau124d9912011-03-01 20:30:48 +01004767 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01004768 * set ->sov and ->next to point to the body and switch to DATA or
Willy Tarreaud98cf932009-12-27 22:54:55 +01004769 * TRAILERS state.
4770 */
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004771 int ret = http_parse_chunk_size(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004772
Willy Tarreau54d23df2012-10-25 19:04:45 +02004773 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01004774 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004775 else if (ret < 0) {
4776 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004777 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004778 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_SIZE, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004779 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004780 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004781 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004782 }
Willy Tarreau54d23df2012-10-25 19:04:45 +02004783 else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) {
Willy Tarreaud98cf932009-12-27 22:54:55 +01004784 /* we want the CRLF after the data */
Willy Tarreau54d23df2012-10-25 19:04:45 +02004785 int ret = http_skip_chunk_crlf(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004786
4787 if (ret == 0)
4788 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004789 else if (ret < 0) {
4790 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004791 if (msg->err_pos >= 0)
Willy Tarreau54d23df2012-10-25 19:04:45 +02004792 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_CHUNK_CRLF, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004793 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004794 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004795 /* we're in MSG_CHUNK_SIZE now */
4796 }
4797 else if (msg->msg_state == HTTP_MSG_TRAILERS) {
Willy Tarreau4baf44b2012-03-09 14:10:20 +01004798 int ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004799
4800 if (ret == 0)
4801 goto missing_data;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004802 else if (ret < 0) {
4803 session_inc_http_err_ctr(s);
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004804 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004805 http_capture_bad_message(&s->fe->invalid_req, s, msg, HTTP_MSG_TRAILERS, s->be);
Willy Tarreaud98cf932009-12-27 22:54:55 +01004806 goto return_bad_req;
Willy Tarreauda7ff642010-06-23 11:44:09 +02004807 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004808 /* we're in HTTP_MSG_DONE now */
4809 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004810 else {
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004811 int old_state = msg->msg_state;
4812
Willy Tarreau610ecce2010-01-04 21:15:02 +01004813 /* other states, DONE...TUNNEL */
Willy Tarreau4fe41902010-06-07 22:27:41 +02004814 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004815 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
4816 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004817 channel_dont_close(req);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004818 if (http_resync_states(s)) {
4819 /* some state changes occurred, maybe the analyser
4820 * was disabled too.
Willy Tarreauface8392010-01-03 11:37:54 +01004821 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004822 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004823 if (req->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004824 /* request errors are most likely due to
4825 * the server aborting the transfer.
4826 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004827 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004828 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01004829 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01004830 http_capture_bad_message(&s->fe->invalid_req, s, msg, old_state, s->be);
Willy Tarreau610ecce2010-01-04 21:15:02 +01004831 goto return_bad_req;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01004832 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01004833 return 1;
Willy Tarreaub608feb2010-01-02 22:47:18 +01004834 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004835
4836 /* If "option abortonclose" is set on the backend, we
4837 * want to monitor the client's connection and forward
4838 * any shutdown notification to the server, which will
4839 * decide whether to close or to go on processing the
4840 * request.
4841 */
4842 if (s->be->options & PR_O_ABRT_CLOSE) {
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004843 channel_auto_read(req);
4844 channel_auto_close(req);
Willy Tarreau5c54c712010-07-17 08:02:58 +02004845 }
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004846 else if (s->txn.meth == HTTP_METH_POST) {
4847 /* POST requests may require to read extra CRLF
4848 * sent by broken browsers and which could cause
4849 * an RST to be sent upon close on some systems
4850 * (eg: Linux).
4851 */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004852 channel_auto_read(req);
Willy Tarreau58bd8fd2010-09-28 14:16:41 +02004853 }
Willy Tarreau5c54c712010-07-17 08:02:58 +02004854
Willy Tarreau610ecce2010-01-04 21:15:02 +01004855 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004856 }
4857 }
4858
Willy Tarreaud98cf932009-12-27 22:54:55 +01004859 missing_data:
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004860 /* stop waiting for data if the input is closed before the end */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004861 if (req->flags & CF_SHUTR) {
Willy Tarreau79ebac62010-06-07 13:47:49 +02004862 if (!(s->flags & SN_ERR_MASK))
4863 s->flags |= SN_ERR_CLICL;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004864 if (!(s->flags & SN_FINST_MASK)) {
4865 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4866 s->flags |= SN_FINST_H;
4867 else
4868 s->flags |= SN_FINST_D;
4869 }
4870
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004871 s->fe->fe_counters.cli_aborts++;
4872 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004873 if (objt_server(s->target))
4874 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004875
4876 goto return_bad_req_stats_ok;
Willy Tarreau79ebac62010-06-07 13:47:49 +02004877 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004878
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004879 /* waiting for the last bits to leave the buffer */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004880 if (req->flags & CF_SHUTW)
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004881 goto aborted_xfer;
Willy Tarreau610ecce2010-01-04 21:15:02 +01004882
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004883 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004884 * chunks even if the client has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004885 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004886 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02004887 channel_dont_close(req);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02004888
Willy Tarreau5c620922011-05-11 19:56:11 +02004889 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004890 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02004891 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01004892 * modes are already handled by the stream sock layer. We must not do
4893 * this in content-length mode because it could present the MSG_MORE
4894 * flag with the last block of forwarded data, which would cause an
4895 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02004896 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01004897 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02004898 req->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02004899
Willy Tarreau610ecce2010-01-04 21:15:02 +01004900 http_silent_debug(__LINE__, s);
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01004901 return 0;
4902
Willy Tarreaud98cf932009-12-27 22:54:55 +01004903 return_bad_req: /* let's centralize all bad requests */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004904 s->fe->fe_counters.failed_req++;
Willy Tarreaud98cf932009-12-27 22:54:55 +01004905 if (s->listener->counters)
4906 s->listener->counters->failed_req++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004907 return_bad_req_stats_ok:
4908 txn->req.msg_state = HTTP_MSG_ERROR;
4909 if (txn->status) {
4910 /* Note: we don't send any error if some data were already sent */
4911 stream_int_retnclose(req->prod, NULL);
4912 } else {
4913 txn->status = 400;
Willy Tarreau783f2582012-09-04 12:19:04 +02004914 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004915 }
4916 req->analysers = 0;
4917 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreaud98cf932009-12-27 22:54:55 +01004918
4919 if (!(s->flags & SN_ERR_MASK))
4920 s->flags |= SN_ERR_PRXCOND;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004921 if (!(s->flags & SN_FINST_MASK)) {
4922 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4923 s->flags |= SN_FINST_H;
4924 else
4925 s->flags |= SN_FINST_D;
4926 }
4927 return 0;
4928
4929 aborted_xfer:
4930 txn->req.msg_state = HTTP_MSG_ERROR;
4931 if (txn->status) {
4932 /* Note: we don't send any error if some data were already sent */
4933 stream_int_retnclose(req->prod, NULL);
4934 } else {
4935 txn->status = 502;
Willy Tarreau783f2582012-09-04 12:19:04 +02004936 stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502));
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004937 }
4938 req->analysers = 0;
4939 s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */
4940
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01004941 s->fe->fe_counters.srv_aborts++;
4942 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01004943 if (objt_server(s->target))
4944 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01004945
4946 if (!(s->flags & SN_ERR_MASK))
4947 s->flags |= SN_ERR_SRVCL;
4948 if (!(s->flags & SN_FINST_MASK)) {
4949 if (txn->rsp.msg_state < HTTP_MSG_ERROR)
4950 s->flags |= SN_FINST_H;
4951 else
4952 s->flags |= SN_FINST_D;
4953 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01004954 return 0;
4955}
4956
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004957/* This stream analyser waits for a complete HTTP response. It returns 1 if the
4958 * processing can continue on next analysers, or zero if it either needs more
4959 * data or wants to immediately abort the response (eg: timeout, error, ...). It
4960 * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers
4961 * when it has nothing left to do, and may remove any analyser when it wants to
4962 * abort.
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004963 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02004964int http_wait_for_response(struct session *s, struct channel *rep, int an_bit)
Willy Tarreauc65a3ba2008-08-11 23:42:50 +02004965{
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004966 struct http_txn *txn = &s->txn;
4967 struct http_msg *msg = &txn->rsp;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02004968 struct hdr_ctx ctx;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01004969 int use_close_only;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004970 int cur_idx;
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02004971 int n;
Willy Tarreauadfb8562008-08-11 15:24:42 +02004972
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01004973 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 +02004974 now_ms, __FUNCTION__,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004975 s,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004976 rep,
4977 rep->rex, rep->wex,
4978 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02004979 rep->buf->i,
Willy Tarreau3a16b2c2008-08-28 08:54:27 +02004980 rep->analysers);
Willy Tarreau67f0eea2008-08-10 22:55:22 +02004981
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004982 /*
4983 * Now parse the partial (or complete) lines.
4984 * We will check the response syntax, and also join multi-line
4985 * headers. An index of all the lines will be elaborated while
4986 * parsing.
4987 *
4988 * For the parsing, we use a 28 states FSM.
4989 *
4990 * Here is the information we currently have :
Willy Tarreau9b28e032012-10-12 23:49:43 +02004991 * rep->buf->p = beginning of response
4992 * rep->buf->p + msg->eoh = end of processed headers / start of current one
4993 * rep->buf->p + rep->buf->i = end of input data
Willy Tarreau26927362012-05-18 23:22:52 +02004994 * msg->eol = end of current header or line (LF or CRLF)
4995 * msg->next = first non-visited byte
Willy Tarreaub37c27e2009-10-18 22:53:08 +02004996 */
4997
Willy Tarreau83e3af02009-12-28 17:39:57 +01004998 /* There's a protected area at the end of the buffer for rewriting
4999 * purposes. We don't want to start to parse the request if the
5000 * protected area is affected, because we may have to move processed
5001 * data later, which is much more complicated.
5002 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005003 if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) {
Willy Tarreau379357a2013-06-08 12:55:46 +02005004 if (unlikely(!channel_reserved(rep))) {
5005 /* some data has still not left the buffer, wake us once that's done */
5006 if (rep->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_WRITE_ERROR|CF_WRITE_TIMEOUT))
5007 goto abort_response;
5008 channel_dont_close(rep);
5009 rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */
5010 return 0;
Willy Tarreau83e3af02009-12-28 17:39:57 +01005011 }
5012
Willy Tarreau379357a2013-06-08 12:55:46 +02005013 if (unlikely(bi_end(rep->buf) < b_ptr(rep->buf, msg->next) ||
5014 bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite))
5015 buffer_slow_realign(rep->buf);
5016
Willy Tarreau9b28e032012-10-12 23:49:43 +02005017 if (likely(msg->next < rep->buf->i))
Willy Tarreaua560c212012-03-09 13:50:57 +01005018 http_msg_analyzer(msg, &txn->hdr_idx);
Willy Tarreau83e3af02009-12-28 17:39:57 +01005019 }
5020
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005021 /* 1: we might have to print this header in debug mode */
5022 if (unlikely((global.mode & MODE_DEBUG) &&
5023 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) &&
Willy Tarreau655dce92009-11-08 13:10:58 +01005024 (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005025 char *eol, *sol;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005026
Willy Tarreau9b28e032012-10-12 23:49:43 +02005027 sol = rep->buf->p;
5028 eol = sol + (msg->sl.st.l ? msg->sl.st.l : rep->buf->i);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005029 debug_hdr("srvrep", s, sol, eol);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005030
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005031 sol += hdr_idx_first_pos(&txn->hdr_idx);
5032 cur_idx = hdr_idx_first_idx(&txn->hdr_idx);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005033
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005034 while (cur_idx) {
5035 eol = sol + txn->hdr_idx.v[cur_idx].len;
5036 debug_hdr("srvhdr", s, sol, eol);
5037 sol = eol + txn->hdr_idx.v[cur_idx].cr + 1;
5038 cur_idx = txn->hdr_idx.v[cur_idx].next;
5039 }
5040 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005041
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005042 /*
5043 * Now we quickly check if we have found a full valid response.
5044 * If not so, we check the FD and buffer states before leaving.
5045 * A full response is indicated by the fact that we have seen
Willy Tarreau655dce92009-11-08 13:10:58 +01005046 * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005047 * responses are checked first.
5048 *
5049 * Depending on whether the client is still there or not, we
5050 * may send an error response back or not. Note that normally
5051 * we should only check for HTTP status there, and check I/O
5052 * errors somewhere else.
5053 */
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005054
Willy Tarreau655dce92009-11-08 13:10:58 +01005055 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005056 /* Invalid response */
5057 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
5058 /* we detected a parsing error. We want to archive this response
5059 * in the dedicated proxy area for later troubleshooting.
5060 */
5061 hdr_response_bad:
5062 if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005063 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005064
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005065 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005066 if (objt_server(s->target)) {
5067 objt_server(s->target)->counters.failed_resp++;
5068 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005069 }
Willy Tarreau64648412010-03-05 10:41:54 +01005070 abort_response:
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005071 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005072 rep->analysers = 0;
5073 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005074 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005075 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005076 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005077
5078 if (!(s->flags & SN_ERR_MASK))
5079 s->flags |= SN_ERR_PRXCOND;
5080 if (!(s->flags & SN_FINST_MASK))
5081 s->flags |= SN_FINST_H;
5082
5083 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005084 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005085
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005086 /* too large response does not fit in buffer. */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005087 else if (buffer_full(rep->buf, global.tune.maxrewrite)) {
Willy Tarreaufec4d892011-09-02 20:04:57 +02005088 if (msg->err_pos < 0)
Willy Tarreau9b28e032012-10-12 23:49:43 +02005089 msg->err_pos = rep->buf->i;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005090 goto hdr_response_bad;
5091 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005092
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005093 /* read error */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005094 else if (rep->flags & CF_READ_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005095 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005096 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau4076a152009-04-02 15:18:36 +02005097
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005098 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005099 if (objt_server(s->target)) {
5100 objt_server(s->target)->counters.failed_resp++;
5101 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005102 }
Willy Tarreau461f6622008-08-15 23:43:19 +02005103
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005104 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005105 rep->analysers = 0;
5106 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005107 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005108 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005109 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau816b9792009-09-15 21:25:21 +02005110
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005111 if (!(s->flags & SN_ERR_MASK))
5112 s->flags |= SN_ERR_SRVCL;
5113 if (!(s->flags & SN_FINST_MASK))
5114 s->flags |= SN_FINST_H;
Willy Tarreaucebf57e2008-08-15 18:16:37 +02005115 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005116 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005117
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005118 /* read timeout : return a 504 to the client. */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005119 else if (rep->flags & CF_READ_TIMEOUT) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005120 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005121 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005122
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005123 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005124 if (objt_server(s->target)) {
5125 objt_server(s->target)->counters.failed_resp++;
5126 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005127 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005128
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005129 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005130 rep->analysers = 0;
5131 txn->status = 504;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005132 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005133 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005134 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504));
Willy Tarreau4076a152009-04-02 15:18:36 +02005135
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005136 if (!(s->flags & SN_ERR_MASK))
5137 s->flags |= SN_ERR_SRVTO;
5138 if (!(s->flags & SN_FINST_MASK))
5139 s->flags |= SN_FINST_H;
5140 return 0;
5141 }
Willy Tarreaua7c52762008-08-16 18:40:18 +02005142
Willy Tarreauf003d372012-11-26 13:35:37 +01005143 /* client abort with an abortonclose */
5144 else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) {
5145 s->fe->fe_counters.cli_aborts++;
5146 s->be->be_counters.cli_aborts++;
5147 if (objt_server(s->target))
5148 objt_server(s->target)->counters.cli_aborts++;
5149
5150 rep->analysers = 0;
5151 channel_auto_close(rep);
5152
5153 txn->status = 400;
5154 bi_erase(rep);
5155 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400));
5156
5157 if (!(s->flags & SN_ERR_MASK))
5158 s->flags |= SN_ERR_CLICL;
5159 if (!(s->flags & SN_FINST_MASK))
5160 s->flags |= SN_FINST_H;
5161
5162 /* process_session() will take care of the error */
5163 return 0;
5164 }
5165
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005166 /* close from server, capture the response if the server has started to respond */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005167 else if (rep->flags & CF_SHUTR) {
Willy Tarreau3b8c08a2011-09-02 20:16:24 +02005168 if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005169 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreau21d2af32008-02-14 20:25:24 +01005170
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005171 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005172 if (objt_server(s->target)) {
5173 objt_server(s->target)->counters.failed_resp++;
5174 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005175 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005176
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005177 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005178 rep->analysers = 0;
5179 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005180 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005181 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005182 stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502));
Willy Tarreau21d2af32008-02-14 20:25:24 +01005183
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005184 if (!(s->flags & SN_ERR_MASK))
5185 s->flags |= SN_ERR_SRVCL;
5186 if (!(s->flags & SN_FINST_MASK))
5187 s->flags |= SN_FINST_H;
5188 return 0;
5189 }
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005190
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005191 /* write error to client (we don't send any message then) */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005192 else if (rep->flags & CF_WRITE_ERROR) {
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005193 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005194 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Krzysztof Piotr Oledzki5fb18822009-10-13 21:14:09 +02005195
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005196 s->be->be_counters.failed_resp++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005197 rep->analysers = 0;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005198 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005199
5200 if (!(s->flags & SN_ERR_MASK))
5201 s->flags |= SN_ERR_CLICL;
5202 if (!(s->flags & SN_FINST_MASK))
5203 s->flags |= SN_FINST_H;
5204
5205 /* process_session() will take care of the error */
5206 return 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005207 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005208
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005209 channel_dont_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005210 return 0;
5211 }
5212
5213 /* More interesting part now : we know that we have a complete
5214 * response which at least looks like HTTP. We have an indicator
5215 * of each header's length, so we can parse them quickly.
5216 */
5217
5218 if (unlikely(msg->err_pos >= 0))
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005219 http_capture_bad_message(&s->be->invalid_rep, s, msg, msg->msg_state, s->fe);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005220
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005221 /*
5222 * 1: get the status code
5223 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02005224 n = rep->buf->p[msg->sl.st.c] - '0';
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005225 if (n < 1 || n > 5)
5226 n = 0;
Willy Tarreauda7ff642010-06-23 11:44:09 +02005227 /* when the client triggers a 4xx from the server, it's most often due
5228 * to a missing object or permission. These events should be tracked
5229 * because if they happen often, it may indicate a brute force or a
5230 * vulnerability scan.
5231 */
5232 if (n == 4)
5233 session_inc_http_err_ctr(s);
5234
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005235 if (objt_server(s->target))
5236 objt_server(s->target)->counters.p.http.rsp[n]++;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005237
Willy Tarreau5b154472009-12-21 20:11:07 +01005238 /* check if the response is HTTP/1.1 or above */
5239 if ((msg->sl.st.v_l == 8) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005240 ((rep->buf->p[5] > '1') ||
5241 ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1'))))
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005242 msg->flags |= HTTP_MSGF_VER_11;
Willy Tarreau5b154472009-12-21 20:11:07 +01005243
5244 /* "connection" has not been parsed yet */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005245 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 +01005246
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005247 /* transfer length unknown*/
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005248 msg->flags &= ~HTTP_MSGF_XFER_LEN;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005249
Willy Tarreau9b28e032012-10-12 23:49:43 +02005250 txn->status = strl2ui(rep->buf->p + msg->sl.st.c, msg->sl.st.c_l);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005251
Willy Tarreau39650402010-03-15 19:44:39 +01005252 /* Adjust server's health based on status code. Note: status codes 501
5253 * and 505 are triggered on demand by client request, so we must not
5254 * count them as server failures.
5255 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005256 if (objt_server(s->target)) {
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005257 if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505))
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005258 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005259 else
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005260 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS);
Willy Tarreaud45b3d52010-05-20 11:49:03 +02005261 }
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005262
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005263 /*
5264 * 2: check for cacheability.
5265 */
5266
5267 switch (txn->status) {
5268 case 200:
5269 case 203:
5270 case 206:
5271 case 300:
5272 case 301:
5273 case 410:
5274 /* RFC2616 @13.4:
5275 * "A response received with a status code of
5276 * 200, 203, 206, 300, 301 or 410 MAY be stored
5277 * by a cache (...) unless a cache-control
5278 * directive prohibits caching."
5279 *
5280 * RFC2616 @9.5: POST method :
5281 * "Responses to this method are not cacheable,
5282 * unless the response includes appropriate
5283 * Cache-Control or Expires header fields."
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005284 */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005285 if (likely(txn->meth != HTTP_METH_POST) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005286 ((s->be->options & PR_O_CHK_CACHE) || (s->be->ck_opts & PR_CK_NOC)))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005287 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
5288 break;
5289 default:
5290 break;
5291 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005292
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005293 /*
5294 * 3: we may need to capture headers
5295 */
5296 s->logs.logwait &= ~LW_RESP;
Willy Tarreau42f7d892012-03-24 08:28:09 +01005297 if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap))
Willy Tarreau9b28e032012-10-12 23:49:43 +02005298 capture_headers(rep->buf->p, &txn->hdr_idx,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005299 txn->rsp.cap, s->fe->rsp_cap);
5300
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005301 /* 4: determine the transfer-length.
5302 * According to RFC2616 #4.4, amended by the HTTPbis working group,
5303 * the presence of a message-body in a RESPONSE and its transfer length
5304 * must be determined that way :
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005305 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005306 * All responses to the HEAD request method MUST NOT include a
5307 * message-body, even though the presence of entity-header fields
5308 * might lead one to believe they do. All 1xx (informational), 204
5309 * (No Content), and 304 (Not Modified) responses MUST NOT include a
5310 * message-body. All other responses do include a message-body,
5311 * although it MAY be of zero length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005312 *
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005313 * 1. Any response which "MUST NOT" include a message-body (such as the
5314 * 1xx, 204 and 304 responses and any response to a HEAD request) is
5315 * always terminated by the first empty line after the header fields,
5316 * regardless of the entity-header fields present in the message.
5317 *
5318 * 2. If a Transfer-Encoding header field (Section 9.7) is present and
5319 * the "chunked" transfer-coding (Section 6.2) is used, the
5320 * transfer-length is defined by the use of this transfer-coding.
5321 * If a Transfer-Encoding header field is present and the "chunked"
5322 * transfer-coding is not present, the transfer-length is defined by
5323 * the sender closing the connection.
5324 *
5325 * 3. If a Content-Length header field is present, its decimal value in
5326 * OCTETs represents both the entity-length and the transfer-length.
5327 * If a message is received with both a Transfer-Encoding header
5328 * field and a Content-Length header field, the latter MUST be ignored.
5329 *
5330 * 4. If the message uses the media type "multipart/byteranges", and
5331 * the transfer-length is not otherwise specified, then this self-
5332 * delimiting media type defines the transfer-length. This media
5333 * type MUST NOT be used unless the sender knows that the recipient
5334 * can parse it; the presence in a request of a Range header with
5335 * multiple byte-range specifiers from a 1.1 client implies that the
5336 * client can parse multipart/byteranges responses.
5337 *
5338 * 5. By the server closing the connection.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005339 */
5340
5341 /* Skip parsing if no content length is possible. The response flags
Willy Tarreau124d9912011-03-01 20:30:48 +01005342 * remain 0 as well as the chunk_len, which may or may not mirror
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005343 * the real header value, and we note that we know the response's length.
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005344 * FIXME: should we parse anyway and return an error on chunked encoding ?
5345 */
5346 if (txn->meth == HTTP_METH_HEAD ||
5347 (txn->status >= 100 && txn->status < 200) ||
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005348 txn->status == 204 || txn->status == 304) {
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005349 msg->flags |= HTTP_MSGF_XFER_LEN;
Willy Tarreau91015352012-11-27 07:31:33 +01005350 s->comp_algo = NULL;
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005351 goto skip_content_length;
5352 }
5353
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005354 use_close_only = 0;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005355 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005356 while ((msg->flags & HTTP_MSGF_VER_11) &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005357 http_find_header2("Transfer-Encoding", 17, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005358 if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0)
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005359 msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
5360 else if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005361 /* bad transfer-encoding (chunked followed by something else) */
5362 use_close_only = 1;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005363 msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN);
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005364 break;
5365 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005366 }
5367
5368 /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */
5369 ctx.idx = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005370 while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only &&
Willy Tarreau9b28e032012-10-12 23:49:43 +02005371 http_find_header2("Content-Length", 14, rep->buf->p, &txn->hdr_idx, &ctx)) {
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005372 signed long long cl;
5373
Willy Tarreauad14f752011-09-02 20:33:27 +02005374 if (!ctx.vlen) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005375 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005376 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005377 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005378
Willy Tarreauad14f752011-09-02 20:33:27 +02005379 if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005380 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005381 goto hdr_response_bad; /* parse failure */
Willy Tarreauad14f752011-09-02 20:33:27 +02005382 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005383
Willy Tarreauad14f752011-09-02 20:33:27 +02005384 if (cl < 0) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005385 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005386 goto hdr_response_bad;
Willy Tarreauad14f752011-09-02 20:33:27 +02005387 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005388
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005389 if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02005390 msg->err_pos = ctx.line + ctx.val - rep->buf->p;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005391 goto hdr_response_bad; /* already specified, was different */
Willy Tarreauad14f752011-09-02 20:33:27 +02005392 }
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005393
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005394 msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN;
Willy Tarreau124d9912011-03-01 20:30:48 +01005395 msg->body_len = msg->chunk_len = cl;
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005396 }
5397
William Lallemand82fe75c2012-10-23 10:25:10 +02005398 if (s->fe->comp || s->be->comp)
5399 select_compression_response_header(s, rep->buf);
5400
Willy Tarreaue8e785b2009-12-26 15:34:26 +01005401 /* FIXME: we should also implement the multipart/byterange method.
5402 * For now on, we resort to close mode in this case (unknown length).
5403 */
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005404skip_content_length:
Willy Tarreaub8c82c22009-10-18 23:45:12 +02005405
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005406 /* end of job, return OK */
5407 rep->analysers &= ~an_bit;
5408 rep->analyse_exp = TICK_ETERNITY;
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005409 channel_auto_close(rep);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005410 return 1;
5411}
5412
5413/* This function performs all the processing enabled for the current response.
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005414 * It normally returns 1 unless it wants to break. It relies on buffers flags,
5415 * and updates t->rep->analysers. It might make sense to explode it into several
5416 * other functions. It works like process_request (see indications above).
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005417 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005418int http_process_res_common(struct session *t, struct channel *rep, int an_bit, struct proxy *px)
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005419{
5420 struct http_txn *txn = &t->txn;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005421 struct http_msg *msg = &txn->rsp;
5422 struct proxy *cur_proxy;
Willy Tarreauf4f04122010-01-28 18:10:50 +01005423 struct cond_wordlist *wl;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005424
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01005425 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 +02005426 now_ms, __FUNCTION__,
5427 t,
5428 rep,
5429 rep->rex, rep->wex,
5430 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02005431 rep->buf->i,
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005432 rep->analysers);
5433
Willy Tarreau655dce92009-11-08 13:10:58 +01005434 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005435 return 0;
5436
5437 rep->analysers &= ~an_bit;
5438 rep->analyse_exp = TICK_ETERNITY;
5439
Willy Tarreau5b154472009-12-21 20:11:07 +01005440 /* Now we have to check if we need to modify the Connection header.
5441 * This is more difficult on the response than it is on the request,
5442 * because we can have two different HTTP versions and we don't know
5443 * how the client will interprete a response. For instance, let's say
5444 * that the client sends a keep-alive request in HTTP/1.0 and gets an
5445 * HTTP/1.1 response without any header. Maybe it will bound itself to
5446 * HTTP/1.0 because it only knows about it, and will consider the lack
5447 * of header as a close, or maybe it knows HTTP/1.1 and can consider
5448 * the lack of header as a keep-alive. Thus we will use two flags
5449 * indicating how a request MAY be understood by the client. In case
5450 * of multiple possibilities, we'll fix the header to be explicit. If
5451 * ambiguous cases such as both close and keepalive are seen, then we
5452 * will fall back to explicit close. Note that we won't take risks with
5453 * HTTP/1.0 clients which may not necessarily understand keep-alive.
Willy Tarreau60466522010-01-18 19:08:45 +01005454 * See doc/internals/connection-header.txt for the complete matrix.
Willy Tarreau5b154472009-12-21 20:11:07 +01005455 */
5456
Willy Tarreaudc008c52010-02-01 16:20:08 +01005457 if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) ||
5458 txn->status == 101)) {
5459 /* Either we've established an explicit tunnel, or we're
5460 * switching the protocol. In both cases, we're very unlikely
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005461 * to understand the next protocols. We have to switch to tunnel
5462 * mode, so that we transfer the request and responses then let
5463 * this protocol pass unmodified. When we later implement specific
5464 * parsers for such protocols, we'll want to check the Upgrade
Willy Tarreaudc008c52010-02-01 16:20:08 +01005465 * header which contains information about that protocol for
5466 * responses with status 101 (eg: see RFC2817 about TLS).
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005467 */
5468 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN;
5469 }
Willy Tarreaudc008c52010-02-01 16:20:08 +01005470 else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) &&
5471 ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN ||
5472 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005473 int to_del = 0;
Willy Tarreau5b154472009-12-21 20:11:07 +01005474
Willy Tarreau60466522010-01-18 19:08:45 +01005475 /* on unknown transfer length, we must close */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005476 if (!(msg->flags & HTTP_MSGF_XFER_LEN) &&
Willy Tarreau60466522010-01-18 19:08:45 +01005477 (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN)
5478 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO;
Willy Tarreau5b154472009-12-21 20:11:07 +01005479
Willy Tarreau60466522010-01-18 19:08:45 +01005480 /* now adjust header transformations depending on current state */
5481 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN ||
5482 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) {
5483 to_del |= 2; /* remove "keep-alive" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005484 if (!(msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005485 to_del |= 1; /* remove "close" for HTTP/1.0 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005486 }
Willy Tarreau60466522010-01-18 19:08:45 +01005487 else { /* SCL / KAL */
5488 to_del |= 1; /* remove "close" on any response */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005489 if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005490 to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */
Willy Tarreau5b154472009-12-21 20:11:07 +01005491 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005492
Willy Tarreau60466522010-01-18 19:08:45 +01005493 /* Parse and remove some headers from the connection header */
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005494 http_parse_connection_header(txn, msg, to_del);
Willy Tarreau5b154472009-12-21 20:11:07 +01005495
Willy Tarreau60466522010-01-18 19:08:45 +01005496 /* Some keep-alive responses are converted to Server-close if
5497 * the server wants to close.
Willy Tarreau5b154472009-12-21 20:11:07 +01005498 */
Willy Tarreau60466522010-01-18 19:08:45 +01005499 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) {
5500 if ((txn->flags & TX_HDR_CONN_CLO) ||
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005501 (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11)))
Willy Tarreau60466522010-01-18 19:08:45 +01005502 txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL;
Willy Tarreaub608feb2010-01-02 22:47:18 +01005503 }
Willy Tarreau5b154472009-12-21 20:11:07 +01005504 }
5505
Willy Tarreaub37c27e2009-10-18 22:53:08 +02005506 if (1) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005507 /*
5508 * 3: we will have to evaluate the filters.
5509 * As opposed to version 1.2, now they will be evaluated in the
5510 * filters order and not in the header order. This means that
5511 * each filter has to be validated among all headers.
5512 *
5513 * Filters are tried with ->be first, then with ->fe if it is
5514 * different from ->be.
5515 */
5516
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005517 cur_proxy = t->be;
5518 while (1) {
5519 struct proxy *rule_set = cur_proxy;
5520
5521 /* try headers filters */
5522 if (rule_set->rsp_exp != NULL) {
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005523 if (apply_filters_to_response(t, rep, rule_set) < 0) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005524 return_bad_resp:
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005525 if (objt_server(t->target)) {
5526 objt_server(t->target)->counters.failed_resp++;
5527 health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +01005528 }
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005529 t->be->be_counters.failed_resp++;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005530 return_srv_prx_502:
Willy Tarreau2df28e82008-08-17 15:20:19 +02005531 rep->analysers = 0;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005532 txn->status = 502;
Willy Tarreauc88ea682009-12-29 14:56:36 +01005533 rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau9dab5fc2012-05-07 11:56:55 +02005534 bi_erase(rep);
Willy Tarreau783f2582012-09-04 12:19:04 +02005535 stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502));
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005536 if (!(t->flags & SN_ERR_MASK))
5537 t->flags |= SN_ERR_PRXCOND;
5538 if (!(t->flags & SN_FINST_MASK))
5539 t->flags |= SN_FINST_H;
Willy Tarreaudafde432008-08-17 01:00:46 +02005540 return 0;
Willy Tarreau21d2af32008-02-14 20:25:24 +01005541 }
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005542 }
Willy Tarreau21d2af32008-02-14 20:25:24 +01005543
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005544 /* has the response been denied ? */
5545 if (txn->flags & TX_SVDENY) {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005546 if (objt_server(t->target))
5547 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005548
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005549 t->be->be_counters.denied_resp++;
5550 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005551 if (t->listener->counters)
5552 t->listener->counters->denied_resp++;
5553
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005554 goto return_srv_prx_502;
Willy Tarreau51406232008-03-10 22:04:20 +01005555 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005556
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005557 /* add response headers from the rule sets in the same order */
Willy Tarreaudeb9ed82010-01-03 21:03:22 +01005558 list_for_each_entry(wl, &rule_set->rsp_add, list) {
Willy Tarreau816b9792009-09-15 21:25:21 +02005559 if (txn->status < 200)
5560 break;
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005561 if (wl->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02005562 int ret = acl_exec_cond(wl->cond, px, t, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01005563 ret = acl_pass(ret);
5564 if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS)
5565 ret = !ret;
5566 if (!ret)
5567 continue;
5568 }
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005569 if (unlikely(http_header_add_tail(&txn->rsp, &txn->hdr_idx, wl->s) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005570 goto return_bad_resp;
Willy Tarreau0bbc3cf2006-10-15 14:26:02 +02005571 }
5572
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005573 /* check whether we're already working on the frontend */
5574 if (cur_proxy == t->fe)
5575 break;
5576 cur_proxy = t->fe;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005577 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005578
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005579 /*
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005580 * We may be facing a 100-continue response, in which case this
5581 * is not the right response, and we're waiting for the next one.
5582 * Let's allow this response to go to the client and wait for the
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005583 * next one.
5584 */
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005585 if (unlikely(txn->status == 100)) {
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005586 hdr_idx_init(&txn->hdr_idx);
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005587 msg->next -= channel_forward(rep, msg->next);
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005588 msg->msg_state = HTTP_MSG_RPBEFORE;
5589 txn->status = 0;
5590 rep->analysers |= AN_RES_WAIT_HTTP | an_bit;
5591 return 1;
5592 }
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005593 else if (unlikely(txn->status < 200))
5594 goto skip_header_mangling;
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005595
5596 /* we don't have any 1xx status code now */
5597
5598 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005599 * 4: check for server cookie.
5600 */
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005601 if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name ||
5602 (t->be->options & PR_O_CHK_CACHE))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005603 manage_server_side_cookies(t, rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005604
Willy Tarreaubaaee002006-06-26 02:48:02 +02005605
Willy Tarreaua15645d2007-03-18 16:22:39 +01005606 /*
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005607 * 5: check for cache-control or pragma headers if required.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005608 */
Willy Tarreau67402132012-05-31 20:40:20 +02005609 if ((t->be->options & PR_O_CHK_CACHE) || (t->be->ck_opts & PR_CK_NOC))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005610 check_response_for_cacheability(t, rep);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005611
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005612 /*
5613 * 6: add server cookie in the response if needed
5614 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005615 if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) &&
Willy Tarreau67402132012-05-31 20:40:20 +02005616 !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02005617 (!(t->flags & SN_DIRECT) ||
5618 ((t->be->cookie_maxidle || txn->cookie_last_date) &&
5619 (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) ||
5620 (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date
5621 (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date
Willy Tarreau67402132012-05-31 20:40:20 +02005622 (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) &&
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02005623 !(t->flags & SN_IGNORE_PRST)) {
Willy Tarreauef4f3912010-10-07 21:00:29 +02005624 /* the server is known, it's not the one the client requested, or the
5625 * cookie's last seen date needs to be refreshed. We have to
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005626 * insert a set-cookie here, except if we want to insert only on POST
5627 * requests and this one isn't. Note that servers which don't have cookies
5628 * (eg: some backup servers) will return a full cookie removal request.
5629 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005630 if (!objt_server(t->target)->cookie) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005631 chunk_printf(&trash,
Willy Tarreauef4f3912010-10-07 21:00:29 +02005632 "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/",
5633 t->be->cookie_name);
5634 }
5635 else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005636 chunk_printf(&trash, "Set-Cookie: %s=%s", t->be->cookie_name, objt_server(t->target)->cookie);
Willy Tarreauef4f3912010-10-07 21:00:29 +02005637
5638 if (t->be->cookie_maxidle || t->be->cookie_maxlife) {
5639 /* emit last_date, which is mandatory */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005640 trash.str[trash.len++] = COOKIE_DELIM_DATE;
5641 s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len);
5642 trash.len += 5;
5643
Willy Tarreauef4f3912010-10-07 21:00:29 +02005644 if (t->be->cookie_maxlife) {
5645 /* emit first_date, which is either the original one or
5646 * the current date.
5647 */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005648 trash.str[trash.len++] = COOKIE_DELIM_DATE;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005649 s30tob64(txn->cookie_first_date ?
5650 txn->cookie_first_date >> 2 :
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005651 (date.tv_sec+3) >> 2, trash.str + trash.len);
5652 trash.len += 5;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005653 }
5654 }
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005655 chunk_appendf(&trash, "; path=/");
Willy Tarreauef4f3912010-10-07 21:00:29 +02005656 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005657
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005658 if (t->be->cookie_domain)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005659 chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain);
Willy Tarreaubaaee002006-06-26 02:48:02 +02005660
Willy Tarreau4992dd22012-05-31 21:02:17 +02005661 if (t->be->ck_opts & PR_CK_HTTPONLY)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005662 chunk_appendf(&trash, "; HttpOnly");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005663
5664 if (t->be->ck_opts & PR_CK_SECURE)
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005665 chunk_appendf(&trash, "; Secure");
Willy Tarreau4992dd22012-05-31 21:02:17 +02005666
Willy Tarreau19d14ef2012-10-29 16:51:55 +01005667 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, trash.str, trash.len) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005668 goto return_bad_resp;
Willy Tarreauef4f3912010-10-07 21:00:29 +02005669
Willy Tarreauf1348312010-10-07 15:54:11 +02005670 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005671 if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT))
Willy Tarreauef4f3912010-10-07 21:00:29 +02005672 /* the server did not change, only the date was updated */
5673 txn->flags |= TX_SCK_UPDATED;
5674 else
5675 txn->flags |= TX_SCK_INSERTED;
Willy Tarreaubaaee002006-06-26 02:48:02 +02005676
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005677 /* Here, we will tell an eventual cache on the client side that we don't
5678 * want it to cache this reply because HTTP/1.0 caches also cache cookies !
5679 * Some caches understand the correct form: 'no-cache="set-cookie"', but
5680 * others don't (eg: apache <= 1.3.26). So we use 'private' instead.
5681 */
Willy Tarreau67402132012-05-31 20:40:20 +02005682 if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +02005683
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005684 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
5685
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005686 if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx,
Willy Tarreau58cc8722009-12-28 06:57:33 +01005687 "Cache-control: private", 22) < 0))
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005688 goto return_bad_resp;
Willy Tarreaua15645d2007-03-18 16:22:39 +01005689 }
5690 }
Willy Tarreaubaaee002006-06-26 02:48:02 +02005691
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005692 /*
5693 * 7: check if result will be cacheable with a cookie.
5694 * We'll block the response if security checks have caught
5695 * nasty things such as a cacheable cookie.
5696 */
Willy Tarreauf1348312010-10-07 15:54:11 +02005697 if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) ==
5698 (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) &&
Willy Tarreau63c9e5f2009-12-22 16:01:27 +01005699 (t->be->options & PR_O_CHK_CACHE)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005700
5701 /* we're in presence of a cacheable response containing
5702 * a set-cookie header. We'll block it as requested by
5703 * the 'checkcache' option, and send an alert.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005704 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005705 if (objt_server(t->target))
5706 objt_server(t->target)->counters.failed_secu++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005707
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01005708 t->be->be_counters.denied_resp++;
5709 t->fe->fe_counters.denied_resp++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02005710 if (t->listener->counters)
5711 t->listener->counters->denied_resp++;
5712
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005713 Alert("Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005714 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005715 send_log(t->be, LOG_ALERT,
5716 "Blocking cacheable cookie in response from instance %s, server %s.\n",
Willy Tarreau3fdb3662012-11-12 00:42:33 +01005717 t->be->id, objt_server(t->target) ? objt_server(t->target)->id : "<dispatch>");
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005718 goto return_srv_prx_502;
5719 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005720
5721 /*
Willy Tarreau60466522010-01-18 19:08:45 +01005722 * 8: adjust "Connection: close" or "Connection: keep-alive" if needed.
Willy Tarreau50fc7772012-11-11 22:19:57 +01005723 * If an "Upgrade" token is found, the header is left untouched in order
5724 * not to have to deal with some client bugs : some of them fail an upgrade
5725 * if anything but "Upgrade" is present in the Connection header.
Willy Tarreaua15645d2007-03-18 16:22:39 +01005726 */
Willy Tarreau50fc7772012-11-11 22:19:57 +01005727 if (!(txn->flags & TX_HDR_CONN_UPG) &&
5728 (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) ||
5729 ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) {
Willy Tarreau60466522010-01-18 19:08:45 +01005730 unsigned int want_flags = 0;
5731
5732 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5733 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) {
5734 /* we want a keep-alive response here. Keep-alive header
5735 * required if either side is not 1.1.
5736 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005737 if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11))
Willy Tarreau60466522010-01-18 19:08:45 +01005738 want_flags |= TX_CON_KAL_SET;
5739 }
5740 else {
5741 /* we want a close response here. Close header required if
5742 * the server is 1.1, regardless of the client.
5743 */
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005744 if (msg->flags & HTTP_MSGF_VER_11)
Willy Tarreau60466522010-01-18 19:08:45 +01005745 want_flags |= TX_CON_CLO_SET;
5746 }
5747
5748 if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET)))
Willy Tarreau6acf7c92012-03-09 13:30:45 +01005749 http_change_connection_header(txn, msg, want_flags);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005750 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005751
Willy Tarreau5843d1a2010-02-01 15:13:32 +01005752 skip_header_mangling:
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005753 if ((msg->flags & HTTP_MSGF_XFER_LEN) ||
Willy Tarreaudc008c52010-02-01 16:20:08 +01005754 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005755 rep->analysers |= AN_RES_HTTP_XFER_BODY;
Willy Tarreau03945942009-12-22 16:50:27 +01005756
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005757 /*************************************************************
5758 * OK, that's finished for the headers. We have done what we *
5759 * could. Let's switch to the DATA state. *
5760 ************************************************************/
Willy Tarreaubaaee002006-06-26 02:48:02 +02005761
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005762 t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now);
Willy Tarreaua15645d2007-03-18 16:22:39 +01005763
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005764 /* if the user wants to log as soon as possible, without counting
5765 * bytes from the server, then this is the right moment. We have
5766 * to temporarily assign bytes_out to log what we currently have.
5767 */
Willy Tarreaud79a3b22012-12-28 09:40:16 +01005768 if (!LIST_ISEMPTY(&t->fe->logformat) && !(t->logs.logwait & LW_BYTES)) {
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005769 t->logs.t_close = t->logs.t_data; /* to get a valid end date */
5770 t->logs.bytes_out = txn->rsp.eoh;
Willy Tarreaua5555ec2008-11-30 19:02:32 +01005771 t->do_log(t);
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005772 t->logs.bytes_out = 0;
5773 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01005774
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005775 /* Note: we must not try to cheat by jumping directly to DATA,
5776 * otherwise we would not let the client side wake up.
5777 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01005778
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005779 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005780 }
Willy Tarreaue3fa6e52010-01-04 22:57:43 +01005781 return 1;
Willy Tarreauf5483bf2008-08-14 18:35:40 +02005782}
Willy Tarreaua15645d2007-03-18 16:22:39 +01005783
Willy Tarreaud98cf932009-12-27 22:54:55 +01005784/* This function is an analyser which forwards response body (including chunk
5785 * sizes if any). It is called as soon as we must forward, even if we forward
5786 * zero byte. The only situation where it must not be called is when we're in
5787 * tunnel mode and we want to forward till the close. It's used both to forward
5788 * remaining data and to resync after end of body. It expects the msg_state to
5789 * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to
5790 * read more data, or 1 once we can go on with next request or end the session.
Willy Tarreau124d9912011-03-01 20:30:48 +01005791 * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len
Willy Tarreau26927362012-05-18 23:22:52 +02005792 * bytes of pending data + the headers if not already done (between sol and sov).
5793 * It eventually adjusts sol to match sov after the data in between have been sent.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005794 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02005795int http_response_forward_body(struct session *s, struct channel *res, int an_bit)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005796{
5797 struct http_txn *txn = &s->txn;
5798 struct http_msg *msg = &s->txn.rsp;
Willy Tarreauea953162012-05-18 23:41:28 +02005799 unsigned int bytes;
William Lallemand82fe75c2012-10-23 10:25:10 +02005800 static struct buffer *tmpbuf = NULL;
5801 int compressing = 0;
William Lallemandbf3ae612012-11-19 12:35:37 +01005802 int consumed_data = 0;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005803 int ret;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005804
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005805 if (unlikely(msg->msg_state < HTTP_MSG_BODY))
5806 return 0;
5807
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005808 if ((res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02005809 ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) ||
Willy Tarreau6c2cbe12010-01-03 17:07:49 +01005810 !s->req->analysers) {
Willy Tarreau4fe41902010-06-07 22:27:41 +02005811 /* Output closed while we were sending data. We must abort and
5812 * wake the other side up.
5813 */
5814 msg->msg_state = HTTP_MSG_ERROR;
5815 http_resync_states(s);
Willy Tarreau082b01c2010-01-02 23:58:04 +01005816 return 1;
5817 }
5818
Willy Tarreau4fe41902010-06-07 22:27:41 +02005819 /* in most states, we should abort in case of early close */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005820 channel_auto_close(res);
Willy Tarreaub608feb2010-01-02 22:47:18 +01005821
William Lallemand82fe75c2012-10-23 10:25:10 +02005822 /* this is the first time we need the compression buffer */
5823 if (s->comp_algo != NULL && tmpbuf == NULL) {
5824 if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL)
5825 goto aborted_xfer; /* no memory */
5826 }
5827
Willy Tarreaud98cf932009-12-27 22:54:55 +01005828 if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) {
Willy Tarreaufa4a03c2012-03-09 21:28:54 +01005829 /* we have msg->sov which points to the first byte of message body.
William Lallemand82fe75c2012-10-23 10:25:10 +02005830 * rep->buf.p still points to the beginning of the message and msg->sol
5831 * is still null. We forward the headers, we don't need them.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005832 */
William Lallemand82fe75c2012-10-23 10:25:10 +02005833 channel_forward(res, msg->sov);
5834 msg->next = 0;
5835 msg->sov = 0;
Willy Tarreaua458b672012-03-05 11:17:50 +01005836
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01005837 if (msg->flags & HTTP_MSGF_TE_CHNK)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005838 msg->msg_state = HTTP_MSG_CHUNK_SIZE;
Willy Tarreau54d23df2012-10-25 19:04:45 +02005839 else
Willy Tarreaud98cf932009-12-27 22:54:55 +01005840 msg->msg_state = HTTP_MSG_DATA;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005841 }
5842
William Lallemand82fe75c2012-10-23 10:25:10 +02005843 if (s->comp_algo != NULL) {
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005844 ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */
William Lallemand82fe75c2012-10-23 10:25:10 +02005845 if (ret < 0)
5846 goto missing_data; /* not enough spaces in buffers */
5847 compressing = 1;
5848 }
5849
Willy Tarreaud98cf932009-12-27 22:54:55 +01005850 while (1) {
Willy Tarreau610ecce2010-01-04 21:15:02 +01005851 http_silent_debug(__LINE__, s);
Willy Tarreauea953162012-05-18 23:41:28 +02005852 /* we may have some data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02005853 if (s->comp_algo == NULL) {
5854 bytes = msg->sov - msg->sol;
5855 if (msg->chunk_len || bytes) {
5856 msg->sol = msg->sov;
5857 msg->next -= bytes; /* will be forwarded */
5858 msg->chunk_len += bytes;
5859 msg->chunk_len -= channel_forward(res, msg->chunk_len);
5860 }
Willy Tarreau638cd022010-01-03 07:42:04 +01005861 }
5862
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005863 switch (msg->msg_state - HTTP_MSG_DATA) {
5864 case HTTP_MSG_DATA - HTTP_MSG_DATA: /* must still forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005865 if (compressing) {
5866 consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf);
5867 if (ret < 0)
5868 goto aborted_xfer;
5869 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005870
5871 if (res->to_forward || msg->chunk_len)
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01005872 goto missing_data;
Willy Tarreaucaabe412010-01-03 23:08:28 +01005873
5874 /* nothing left to forward */
William Lallemandbf3ae612012-11-19 12:35:37 +01005875 if (msg->flags & HTTP_MSGF_TE_CHNK) {
Willy Tarreau54d23df2012-10-25 19:04:45 +02005876 msg->msg_state = HTTP_MSG_CHUNK_CRLF;
William Lallemandbf3ae612012-11-19 12:35:37 +01005877 } else {
Willy Tarreaucaabe412010-01-03 23:08:28 +01005878 msg->msg_state = HTTP_MSG_DONE;
William Lallemandbf3ae612012-11-19 12:35:37 +01005879 if (compressing && consumed_data) {
5880 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5881 compressing = 0;
5882 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005883 break;
William Lallemandbf3ae612012-11-19 12:35:37 +01005884 }
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005885 /* fall through for HTTP_MSG_CHUNK_CRLF */
5886
5887 case HTTP_MSG_CHUNK_CRLF - HTTP_MSG_DATA:
5888 /* we want the CRLF after the data */
5889
5890 ret = http_skip_chunk_crlf(msg);
5891 if (ret == 0)
5892 goto missing_data;
5893 else if (ret < 0) {
5894 if (msg->err_pos >= 0)
5895 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_CRLF, s->fe);
5896 goto return_bad_res;
5897 }
5898 /* skipping data in buffer for compression */
5899 if (compressing) {
5900 b_adv(res->buf, msg->next);
5901 msg->next = 0;
5902 msg->sov = 0;
5903 msg->sol = 0;
5904 }
5905 /* we're in MSG_CHUNK_SIZE now, fall through */
5906
5907 case HTTP_MSG_CHUNK_SIZE - HTTP_MSG_DATA:
Willy Tarreau124d9912011-03-01 20:30:48 +01005908 /* read the chunk size and assign it to ->chunk_len, then
Willy Tarreaua458b672012-03-05 11:17:50 +01005909 * set ->sov and ->next to point to the body and switch to DATA or
5910 * TRAILERS state.
Willy Tarreaud98cf932009-12-27 22:54:55 +01005911 */
Willy Tarreaud98cf932009-12-27 22:54:55 +01005912
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005913 ret = http_parse_chunk_size(msg);
Willy Tarreau54d23df2012-10-25 19:04:45 +02005914 if (ret == 0)
Willy Tarreaud98cf932009-12-27 22:54:55 +01005915 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005916 else if (ret < 0) {
5917 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005918 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_CHUNK_SIZE, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005919 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005920 }
William Lallemandbf3ae612012-11-19 12:35:37 +01005921 if (compressing) {
5922 if (likely(msg->chunk_len > 0)) {
5923 /* skipping data if we are in compression mode */
5924 b_adv(res->buf, msg->next);
5925 msg->next = 0;
5926 msg->sov = 0;
5927 msg->sol = 0;
5928 } else {
5929 if (consumed_data) {
5930 http_compression_buffer_end(s, &res->buf, &tmpbuf, 1);
5931 compressing = 0;
5932 }
5933 }
William Lallemand82fe75c2012-10-23 10:25:10 +02005934 }
Willy Tarreau0161d622013-04-02 01:26:55 +02005935 /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005936 break;
Willy Tarreau5523b322009-12-29 12:05:52 +01005937
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005938 case HTTP_MSG_TRAILERS - HTTP_MSG_DATA:
5939 ret = http_forward_trailers(msg);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005940 if (ret == 0)
5941 goto missing_data;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005942 else if (ret < 0) {
5943 if (msg->err_pos >= 0)
Willy Tarreau8a0cef22012-03-09 13:39:23 +01005944 http_capture_bad_message(&s->be->invalid_rep, s, msg, HTTP_MSG_TRAILERS, s->fe);
Willy Tarreaud98cf932009-12-27 22:54:55 +01005945 goto return_bad_res;
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005946 }
William Lallemand00bf1de2012-11-22 17:55:14 +01005947 if (s->comp_algo != NULL) {
5948 /* forwarding trailers */
5949 channel_forward(res, msg->next);
5950 msg->next = 0;
5951 }
Willy Tarreau2d43e182013-04-03 00:22:25 +02005952 /* we're in HTTP_MSG_DONE now, but we might still have
5953 * some data pending, so let's loop over once.
5954 */
5955 break;
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005956
5957 default:
Willy Tarreau610ecce2010-01-04 21:15:02 +01005958 /* other states, DONE...TUNNEL */
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005959
5960 ret = msg->msg_state;
Willy Tarreau4fe41902010-06-07 22:27:41 +02005961 /* for keep-alive we don't want to forward closes on DONE */
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02005962 if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
5963 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02005964 channel_dont_close(res);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005965 if (http_resync_states(s)) {
5966 http_silent_debug(__LINE__, s);
5967 /* some state changes occurred, maybe the analyser
5968 * was disabled too.
Willy Tarreau5523b322009-12-29 12:05:52 +01005969 */
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005970 if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02005971 if (res->flags & CF_SHUTW) {
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005972 /* response errors are most likely due to
5973 * the client aborting the transfer.
5974 */
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01005975 goto aborted_xfer;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005976 }
Willy Tarreaue1582eb2010-12-12 13:10:11 +01005977 if (msg->err_pos >= 0)
Willy Tarreaud655ffe2013-04-02 01:48:58 +02005978 http_capture_bad_message(&s->be->invalid_rep, s, msg, ret, s->fe);
Willy Tarreau610ecce2010-01-04 21:15:02 +01005979 goto return_bad_res;
Willy Tarreau3fe693b2010-12-12 12:50:05 +01005980 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005981 return 1;
Willy Tarreau5523b322009-12-29 12:05:52 +01005982 }
Willy Tarreau610ecce2010-01-04 21:15:02 +01005983 return 0;
Willy Tarreaud98cf932009-12-27 22:54:55 +01005984 }
5985 }
5986
Willy Tarreaud98cf932009-12-27 22:54:55 +01005987 missing_data:
William Lallemandbf3ae612012-11-19 12:35:37 +01005988 if (compressing && consumed_data) {
William Lallemand82fe75c2012-10-23 10:25:10 +02005989 http_compression_buffer_end(s, &res->buf, &tmpbuf, 0);
5990 compressing = 0;
5991 }
Willy Tarreauf003d372012-11-26 13:35:37 +01005992
5993 if (res->flags & CF_SHUTW)
5994 goto aborted_xfer;
5995
5996 /* stop waiting for data if the input is closed before the end. If the
5997 * client side was already closed, it means that the client has aborted,
5998 * so we don't want to count this as a server abort. Otherwise it's a
5999 * server abort.
6000 */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006001 if (res->flags & CF_SHUTR) {
Willy Tarreauf003d372012-11-26 13:35:37 +01006002 if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR))
6003 goto aborted_xfer;
Willy Tarreau40dba092010-03-04 18:14:51 +01006004 if (!(s->flags & SN_ERR_MASK))
6005 s->flags |= SN_ERR_SRVCL;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006006 s->be->be_counters.srv_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006007 if (objt_server(s->target))
6008 objt_server(s->target)->counters.srv_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006009 goto return_bad_res_stats_ok;
Willy Tarreau40dba092010-03-04 18:14:51 +01006010 }
Willy Tarreauf5c8bd62010-01-04 07:10:34 +01006011
Willy Tarreau40dba092010-03-04 18:14:51 +01006012 /* we need to obey the req analyser, so if it leaves, we must too */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006013 if (!s->req->analysers)
6014 goto return_bad_res;
6015
Willy Tarreauea953162012-05-18 23:41:28 +02006016 /* forward any data pending between sol and sov */
William Lallemand82fe75c2012-10-23 10:25:10 +02006017 if (s->comp_algo == NULL) {
6018 bytes = msg->sov - msg->sol;
6019 if (msg->chunk_len || bytes) {
6020 msg->sol = msg->sov;
6021 msg->next -= bytes; /* will be forwarded */
6022 msg->chunk_len += bytes;
6023 msg->chunk_len -= channel_forward(res, msg->chunk_len);
6024 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01006025 }
6026
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006027 /* When TE: chunked is used, we need to get there again to parse remaining
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006028 * chunks even if the server has closed, so we don't want to set CF_DONTCLOSE.
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006029 * Similarly, with keep-alive on the client side, we don't want to forward a
6030 * close.
6031 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006032 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo ||
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006033 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL ||
6034 (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)
Willy Tarreau8263d2b2012-08-28 00:06:31 +02006035 channel_dont_close(res);
Willy Tarreau92aa1fa2010-08-28 18:57:20 +02006036
Willy Tarreau5c620922011-05-11 19:56:11 +02006037 /* We know that more data are expected, but we couldn't send more that
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006038 * what we did. So we always set the CF_EXPECT_MORE flag so that the
Willy Tarreau07293032011-05-30 18:29:28 +02006039 * system knows it must not set a PUSH on this first part. Interactive
Willy Tarreau869fc1e2012-03-05 08:29:20 +01006040 * modes are already handled by the stream sock layer. We must not do
6041 * this in content-length mode because it could present the MSG_MORE
6042 * flag with the last block of forwarded data, which would cause an
6043 * additional delay to be observed by the receiver.
Willy Tarreau5c620922011-05-11 19:56:11 +02006044 */
Willy Tarreau08b4d792012-10-27 01:36:34 +02006045 if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo)
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02006046 res->flags |= CF_EXPECT_MORE;
Willy Tarreau5c620922011-05-11 19:56:11 +02006047
Willy Tarreaud98cf932009-12-27 22:54:55 +01006048 /* the session handler will take care of timeouts and errors */
Willy Tarreau610ecce2010-01-04 21:15:02 +01006049 http_silent_debug(__LINE__, s);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006050 return 0;
6051
Willy Tarreau40dba092010-03-04 18:14:51 +01006052 return_bad_res: /* let's centralize all bad responses */
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006053 s->be->be_counters.failed_resp++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006054 if (objt_server(s->target))
6055 objt_server(s->target)->counters.failed_resp++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006056
6057 return_bad_res_stats_ok:
Willy Tarreaud98cf932009-12-27 22:54:55 +01006058 txn->rsp.msg_state = HTTP_MSG_ERROR;
Willy Tarreau148d0992010-01-10 10:21:21 +01006059 /* don't send any error message as we're in the body */
6060 stream_int_retnclose(res->cons, NULL);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006061 res->analysers = 0;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006062 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006063 if (objt_server(s->target))
6064 health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP);
Willy Tarreaud98cf932009-12-27 22:54:55 +01006065
6066 if (!(s->flags & SN_ERR_MASK))
6067 s->flags |= SN_ERR_PRXCOND;
6068 if (!(s->flags & SN_FINST_MASK))
Willy Tarreau40dba092010-03-04 18:14:51 +01006069 s->flags |= SN_FINST_D;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006070 return 0;
6071
6072 aborted_xfer:
6073 txn->rsp.msg_state = HTTP_MSG_ERROR;
6074 /* don't send any error message as we're in the body */
6075 stream_int_retnclose(res->cons, NULL);
6076 res->analysers = 0;
6077 s->req->analysers = 0; /* we're in data phase, we want to abort both directions */
6078
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006079 s->fe->fe_counters.cli_aborts++;
6080 s->be->be_counters.cli_aborts++;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006081 if (objt_server(s->target))
6082 objt_server(s->target)->counters.cli_aborts++;
Willy Tarreaued2fd2d2010-12-29 11:23:27 +01006083
6084 if (!(s->flags & SN_ERR_MASK))
6085 s->flags |= SN_ERR_CLICL;
6086 if (!(s->flags & SN_FINST_MASK))
6087 s->flags |= SN_FINST_D;
Willy Tarreaud98cf932009-12-27 22:54:55 +01006088 return 0;
6089}
6090
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006091/* Iterate the same filter through all request headers.
6092 * Returns 1 if this filter can be stopped upon return, otherwise 0.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006093 * Since it can manage the switch to another backend, it updates the per-proxy
6094 * DENY stats.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006095 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006096int apply_filter_to_req_headers(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006097{
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006098 char term;
6099 char *cur_ptr, *cur_end, *cur_next;
6100 int cur_idx, old_idx, last_hdr;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006101 struct http_txn *txn = &t->txn;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006102 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006103 int delta;
Willy Tarreau0f7562b2007-01-07 15:46:13 +01006104
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006105 last_hdr = 0;
6106
Willy Tarreau9b28e032012-10-12 23:49:43 +02006107 cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006108 old_idx = 0;
6109
6110 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01006111 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006112 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006113 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006114 (exp->action == ACT_ALLOW ||
6115 exp->action == ACT_DENY ||
6116 exp->action == ACT_TARPIT))
6117 return 0;
6118
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006119 cur_idx = txn->hdr_idx.v[old_idx].next;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006120 if (!cur_idx)
6121 break;
6122
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006123 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006124 cur_ptr = cur_next;
6125 cur_end = cur_ptr + cur_hdr->len;
6126 cur_next = cur_end + cur_hdr->cr + 1;
6127
6128 /* Now we have one header between cur_ptr and cur_end,
6129 * and the next header starts at cur_next.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006130 */
6131
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006132 /* The annoying part is that pattern matching needs
6133 * that we modify the contents to null-terminate all
6134 * strings before testing them.
6135 */
6136
6137 term = *cur_end;
6138 *cur_end = '\0';
6139
6140 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6141 switch (exp->action) {
6142 case ACT_SETBE:
6143 /* It is not possible to jump a second time.
6144 * FIXME: should we return an HTTP/500 here so that
6145 * the admin knows there's a problem ?
6146 */
6147 if (t->be != t->fe)
6148 break;
6149
6150 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006151 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006152 last_hdr = 1;
6153 break;
6154
6155 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006156 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006157 last_hdr = 1;
6158 break;
6159
6160 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006161 txn->flags |= TX_CLDENY;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006162 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006163
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006164 t->fe->fe_counters.denied_req++;
6165 if (t->fe != t->be)
6166 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006167 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006168 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006169
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006170 break;
6171
6172 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006173 txn->flags |= TX_CLTARPIT;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006174 last_hdr = 1;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006175
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006176 t->fe->fe_counters.denied_req++;
6177 if (t->fe != t->be)
6178 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006179 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006180 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006181
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006182 break;
6183
6184 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006185 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6186 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006187 /* FIXME: if the user adds a newline in the replacement, the
6188 * index will not be recalculated for now, and the new line
6189 * will not be counted as a new header.
6190 */
6191
6192 cur_end += delta;
6193 cur_next += delta;
6194 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006195 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006196 break;
6197
6198 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02006199 delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006200 cur_next += delta;
6201
Willy Tarreaufa355d42009-11-29 18:12:29 +01006202 http_msg_move_end(&txn->req, delta);
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006203 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6204 txn->hdr_idx.used--;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006205 cur_hdr->len = 0;
6206 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01006207 cur_idx = old_idx;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006208 break;
6209
6210 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006211 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006212 if (cur_end)
6213 *cur_end = term; /* restore the string terminator */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006214
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006215 /* keep the link from this header to next one in case of later
6216 * removal of next header.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006217 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006218 old_idx = cur_idx;
6219 }
6220 return 0;
6221}
6222
6223
6224/* Apply the filter to the request line.
6225 * Returns 0 if nothing has been done, 1 if the filter has been applied,
6226 * or -1 if a replacement resulted in an invalid request line.
Willy Tarreaua15645d2007-03-18 16:22:39 +01006227 * Since it can manage the switch to another backend, it updates the per-proxy
6228 * DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006229 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006230int apply_filter_to_req_line(struct session *t, struct channel *req, struct hdr_exp *exp)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006231{
6232 char term;
6233 char *cur_ptr, *cur_end;
6234 int done;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006235 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006236 int delta;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006237
Willy Tarreau3d300592007-03-18 18:34:41 +01006238 if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT)))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006239 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01006240 else if (unlikely(txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006241 (exp->action == ACT_ALLOW ||
6242 exp->action == ACT_DENY ||
6243 exp->action == ACT_TARPIT))
6244 return 0;
6245 else if (exp->action == ACT_REMOVE)
6246 return 0;
6247
6248 done = 0;
6249
Willy Tarreau9b28e032012-10-12 23:49:43 +02006250 cur_ptr = req->buf->p;
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006251 cur_end = cur_ptr + txn->req.sl.rq.l;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006252
6253 /* Now we have the request line between cur_ptr and cur_end */
6254
6255 /* The annoying part is that pattern matching needs
6256 * that we modify the contents to null-terminate all
6257 * strings before testing them.
6258 */
6259
6260 term = *cur_end;
6261 *cur_end = '\0';
6262
6263 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
6264 switch (exp->action) {
6265 case ACT_SETBE:
6266 /* It is not possible to jump a second time.
6267 * FIXME: should we return an HTTP/500 here so that
6268 * the admin knows there's a problem ?
Willy Tarreau58f10d72006-12-04 02:26:12 +01006269 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006270 if (t->be != t->fe)
6271 break;
6272
6273 /* Swithing Proxy */
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02006274 session_set_backend(t, (struct proxy *)exp->replace);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006275 done = 1;
6276 break;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006277
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006278 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01006279 txn->flags |= TX_CLALLOW;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006280 done = 1;
6281 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006282
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006283 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01006284 txn->flags |= TX_CLDENY;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006285
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006286 t->fe->fe_counters.denied_req++;
6287 if (t->fe != t->be)
6288 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006289 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006290 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006291
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006292 done = 1;
6293 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006294
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006295 case ACT_TARPIT:
Willy Tarreau3d300592007-03-18 18:34:41 +01006296 txn->flags |= TX_CLTARPIT;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006297
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01006298 t->fe->fe_counters.denied_req++;
6299 if (t->fe != t->be)
6300 t->be->be_counters.denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006301 if (t->listener->counters)
Willy Tarreaubb695392010-06-23 08:43:37 +02006302 t->listener->counters->denied_req++;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +02006303
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006304 done = 1;
6305 break;
Willy Tarreaua496b602006-12-17 23:15:24 +01006306
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006307 case ACT_REPLACE:
6308 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01006309 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
6310 delta = buffer_replace2(req->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006311 /* FIXME: if the user adds a newline in the replacement, the
6312 * index will not be recalculated for now, and the new line
6313 * will not be counted as a new header.
6314 */
Willy Tarreaua496b602006-12-17 23:15:24 +01006315
Willy Tarreaufa355d42009-11-29 18:12:29 +01006316 http_msg_move_end(&txn->req, delta);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006317 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02006318 cur_end = (char *)http_parse_reqline(&txn->req,
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006319 HTTP_MSG_RQMETH,
6320 cur_ptr, cur_end + 1,
6321 NULL, NULL);
6322 if (unlikely(!cur_end))
6323 return -1;
Willy Tarreaua496b602006-12-17 23:15:24 +01006324
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006325 /* we have a full request and we know that we have either a CR
6326 * or an LF at <ptr>.
6327 */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006328 txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l);
6329 hdr_idx_set_start(&txn->hdr_idx, txn->req.sl.rq.l, *cur_end == '\r');
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006330 /* there is no point trying this regex on headers */
6331 return 1;
6332 }
6333 }
6334 *cur_end = term; /* restore the string terminator */
6335 return done;
6336}
Willy Tarreau97de6242006-12-27 17:18:38 +01006337
Willy Tarreau58f10d72006-12-04 02:26:12 +01006338
Willy Tarreau58f10d72006-12-04 02:26:12 +01006339
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006340/*
Willy Tarreau6c123b12010-01-28 20:22:06 +01006341 * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006342 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
Willy Tarreaua15645d2007-03-18 16:22:39 +01006343 * unparsable request. Since it can manage the switch to another backend, it
6344 * updates the per-proxy DENY stats.
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006345 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006346int apply_filters_to_request(struct session *s, struct channel *req, struct proxy *px)
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006347{
Willy Tarreau6c123b12010-01-28 20:22:06 +01006348 struct http_txn *txn = &s->txn;
6349 struct hdr_exp *exp;
6350
6351 for (exp = px->req_exp; exp; exp = exp->next) {
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006352 int ret;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006353
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006354 /*
6355 * The interleaving of transformations and verdicts
6356 * makes it difficult to decide to continue or stop
6357 * the evaluation.
6358 */
6359
Willy Tarreau6c123b12010-01-28 20:22:06 +01006360 if (txn->flags & (TX_CLDENY|TX_CLTARPIT))
6361 break;
6362
Willy Tarreau3d300592007-03-18 18:34:41 +01006363 if ((txn->flags & TX_CLALLOW) &&
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006364 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
Willy Tarreau6c123b12010-01-28 20:22:06 +01006365 exp->action == ACT_TARPIT || exp->action == ACT_PASS))
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006366 continue;
Willy Tarreau6c123b12010-01-28 20:22:06 +01006367
6368 /* if this filter had a condition, evaluate it now and skip to
6369 * next filter if the condition does not match.
6370 */
6371 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02006372 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau6c123b12010-01-28 20:22:06 +01006373 ret = acl_pass(ret);
6374 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
6375 ret = !ret;
6376
6377 if (!ret)
6378 continue;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006379 }
6380
6381 /* Apply the filter to the request line. */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006382 ret = apply_filter_to_req_line(s, req, exp);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006383 if (unlikely(ret < 0))
6384 return -1;
6385
6386 if (likely(ret == 0)) {
6387 /* The filter did not match the request, it can be
6388 * iterated through all headers.
6389 */
Willy Tarreau6c123b12010-01-28 20:22:06 +01006390 apply_filter_to_req_headers(s, req, exp);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006391 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006392 }
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006393 return 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006394}
6395
6396
Willy Tarreaua15645d2007-03-18 16:22:39 +01006397
Willy Tarreau58f10d72006-12-04 02:26:12 +01006398/*
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006399 * Try to retrieve the server associated to the appsession.
6400 * If the server is found, it's assigned to the session.
6401 */
Cyril Bontéb21570a2009-11-29 20:04:48 +01006402void manage_client_side_appsession(struct session *t, const char *buf, int len) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006403 struct http_txn *txn = &t->txn;
6404 appsess *asession = NULL;
6405 char *sessid_temp = NULL;
6406
Cyril Bontéb21570a2009-11-29 20:04:48 +01006407 if (len > t->be->appsession_len) {
6408 len = t->be->appsession_len;
6409 }
6410
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006411 if (t->be->options2 & PR_O2_AS_REQL) {
6412 /* request-learn option is enabled : store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006413 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006414 /* free previously allocated memory as we don't need the session id found in the URL anymore */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006415 pool_free2(apools.sessid, txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006416 }
6417
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006418 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006419 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6420 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6421 return;
6422 }
6423
Willy Tarreaua3377ee2010-01-10 10:49:11 +01006424 memcpy(txn->sessid, buf, len);
6425 txn->sessid[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006426 }
6427
6428 if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) {
6429 Alert("Not enough memory process_cli():asession->sessid:malloc().\n");
6430 send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n");
6431 return;
6432 }
6433
Cyril Bontéb21570a2009-11-29 20:04:48 +01006434 memcpy(sessid_temp, buf, len);
6435 sessid_temp[len] = 0;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006436
6437 asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp);
6438 /* free previously allocated memory */
6439 pool_free2(apools.sessid, sessid_temp);
6440
6441 if (asession != NULL) {
6442 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
6443 if (!(t->be->options2 & PR_O2_AS_REQL))
6444 asession->request_count++;
6445
6446 if (asession->serverid != NULL) {
6447 struct server *srv = t->be->srv;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02006448
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006449 while (srv) {
6450 if (strcmp(srv->id, asession->serverid) == 0) {
Willy Tarreau4de91492010-01-22 19:10:05 +01006451 if ((srv->state & SRV_RUNNING) ||
6452 (t->be->options & PR_O_PERSIST) ||
6453 (t->flags & SN_FORCE_PRST)) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006454 /* we found the server and it's usable */
6455 txn->flags &= ~TX_CK_MASK;
Willy Tarreau2a6d88d2010-01-24 13:10:43 +01006456 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006457 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006458 t->target = &srv->obj_type;
Willy Tarreau664beb82011-03-10 11:38:29 +01006459
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006460 break;
6461 } else {
6462 txn->flags &= ~TX_CK_MASK;
6463 txn->flags |= TX_CK_DOWN;
6464 }
6465 }
6466 srv = srv->next;
6467 }
6468 }
6469 }
6470}
6471
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006472/* Find the end of a cookie value contained between <s> and <e>. It works the
6473 * same way as with headers above except that the semi-colon also ends a token.
6474 * See RFC2965 for more information. Note that it requires a valid header to
6475 * return a valid result.
6476 */
6477char *find_cookie_value_end(char *s, const char *e)
6478{
6479 int quoted, qdpair;
6480
6481 quoted = qdpair = 0;
6482 for (; s < e; s++) {
6483 if (qdpair) qdpair = 0;
6484 else if (quoted) {
6485 if (*s == '\\') qdpair = 1;
6486 else if (*s == '"') quoted = 0;
6487 }
6488 else if (*s == '"') quoted = 1;
6489 else if (*s == ',' || *s == ';') return s;
6490 }
6491 return s;
6492}
6493
6494/* Delete a value in a header between delimiters <from> and <next> in buffer
6495 * <buf>. The number of characters displaced is returned, and the pointer to
6496 * the first delimiter is updated if required. The function tries as much as
6497 * possible to respect the following principles :
6498 * - replace <from> delimiter by the <next> one unless <from> points to a
6499 * colon, in which case <next> is simply removed
6500 * - set exactly one space character after the new first delimiter, unless
6501 * there are not enough characters in the block being moved to do so.
6502 * - remove unneeded spaces before the previous delimiter and after the new
6503 * one.
6504 *
6505 * It is the caller's responsibility to ensure that :
6506 * - <from> points to a valid delimiter or the colon ;
6507 * - <next> points to a valid delimiter or the final CR/LF ;
6508 * - there are non-space chars before <from> ;
6509 * - there is a CR/LF at or after <next>.
6510 */
Willy Tarreauaf819352012-08-27 22:08:00 +02006511int del_hdr_value(struct buffer *buf, char **from, char *next)
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006512{
6513 char *prev = *from;
6514
6515 if (*prev == ':') {
6516 /* We're removing the first value, preserve the colon and add a
6517 * space if possible.
6518 */
6519 if (!http_is_crlf[(unsigned char)*next])
6520 next++;
6521 prev++;
6522 if (prev < next)
6523 *prev++ = ' ';
6524
6525 while (http_is_spht[(unsigned char)*next])
6526 next++;
6527 } else {
6528 /* Remove useless spaces before the old delimiter. */
6529 while (http_is_spht[(unsigned char)*(prev-1)])
6530 prev--;
6531 *from = prev;
6532
6533 /* copy the delimiter and if possible a space if we're
6534 * not at the end of the line.
6535 */
6536 if (!http_is_crlf[(unsigned char)*next]) {
6537 *prev++ = *next++;
6538 if (prev + 1 < next)
6539 *prev++ = ' ';
6540 while (http_is_spht[(unsigned char)*next])
6541 next++;
6542 }
6543 }
6544 return buffer_replace2(buf, prev, next, NULL, 0);
6545}
6546
Cyril Bontébf47aeb2009-10-15 00:15:40 +02006547/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01006548 * Manage client-side cookie. It can impact performance by about 2% so it is
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006549 * desirable to call it only when needed. This code is quite complex because
6550 * of the multiple very crappy and ambiguous syntaxes we have to support. it
6551 * highly recommended not to touch this part without a good reason !
Willy Tarreau58f10d72006-12-04 02:26:12 +01006552 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02006553void manage_client_side_cookies(struct session *t, struct channel *req)
Willy Tarreau58f10d72006-12-04 02:26:12 +01006554{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006555 struct http_txn *txn = &t->txn;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006556 int preserve_hdr;
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01006557 int cur_idx, old_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006558 char *hdr_beg, *hdr_end, *hdr_next, *del_from;
6559 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006560
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006561 /* Iterate through the headers, we start with the start line. */
Willy Tarreau83969f42007-01-22 08:55:47 +01006562 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02006563 hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006564
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006565 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006566 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006567 int val;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006568
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006569 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006570 hdr_beg = hdr_next;
6571 hdr_end = hdr_beg + cur_hdr->len;
6572 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006573
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006574 /* We have one full header between hdr_beg and hdr_end, and the
6575 * next header starts at hdr_next. We're only interested in
Willy Tarreau58f10d72006-12-04 02:26:12 +01006576 * "Cookie:" headers.
6577 */
6578
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006579 val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01006580 if (!val) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006581 old_idx = cur_idx;
6582 continue;
6583 }
6584
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006585 del_from = NULL; /* nothing to be deleted */
6586 preserve_hdr = 0; /* assume we may kill the whole header */
6587
Willy Tarreau58f10d72006-12-04 02:26:12 +01006588 /* Now look for cookies. Conforming to RFC2109, we have to support
6589 * attributes whose name begin with a '$', and associate them with
6590 * the right cookie, if we want to delete this cookie.
6591 * So there are 3 cases for each cookie read :
6592 * 1) it's a special attribute, beginning with a '$' : ignore it.
6593 * 2) it's a server id cookie that we *MAY* want to delete : save
6594 * some pointers on it (last semi-colon, beginning of cookie...)
6595 * 3) it's an application cookie : we *MAY* have to delete a previous
6596 * "special" cookie.
6597 * At the end of loop, if a "special" cookie remains, we may have to
6598 * remove it. If no application cookie persists in the header, we
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006599 * *MUST* delete it.
6600 *
6601 * Note: RFC2965 is unclear about the processing of spaces around
6602 * the equal sign in the ATTR=VALUE form. A careful inspection of
6603 * the RFC explicitly allows spaces before it, and not within the
6604 * tokens (attrs or values). An inspection of RFC2109 allows that
6605 * too but section 10.1.3 lets one think that spaces may be allowed
6606 * after the equal sign too, resulting in some (rare) buggy
6607 * implementations trying to do that. So let's do what servers do.
6608 * Latest ietf draft forbids spaces all around. Also, earlier RFCs
6609 * allowed quoted strings in values, with any possible character
6610 * after a backslash, including control chars and delimitors, which
6611 * causes parsing to become ambiguous. Browsers also allow spaces
6612 * within values even without quotes.
6613 *
6614 * We have to keep multiple pointers in order to support cookie
6615 * removal at the beginning, middle or end of header without
6616 * corrupting the header. All of these headers are valid :
6617 *
6618 * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n
6619 * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n
6620 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
6621 * | | | | | | | | |
6622 * | | | | | | | | hdr_end <--+
6623 * | | | | | | | +--> next
6624 * | | | | | | +----> val_end
6625 * | | | | | +-----------> val_beg
6626 * | | | | +--------------> equal
6627 * | | | +----------------> att_end
6628 * | | +---------------------> att_beg
6629 * | +--------------------------> prev
6630 * +--------------------------------> hdr_beg
Willy Tarreau58f10d72006-12-04 02:26:12 +01006631 */
6632
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006633 for (prev = hdr_beg + 6; prev < hdr_end; prev = next) {
6634 /* Iterate through all cookies on this line */
6635
6636 /* find att_beg */
6637 att_beg = prev + 1;
6638 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
6639 att_beg++;
6640
6641 /* find att_end : this is the first character after the last non
6642 * space before the equal. It may be equal to hdr_end.
6643 */
6644 equal = att_end = att_beg;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006645
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006646 while (equal < hdr_end) {
6647 if (*equal == '=' || *equal == ',' || *equal == ';')
Willy Tarreau58f10d72006-12-04 02:26:12 +01006648 break;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006649 if (http_is_spht[(unsigned char)*equal++])
6650 continue;
6651 att_end = equal;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006652 }
6653
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006654 /* here, <equal> points to '=', a delimitor or the end. <att_end>
6655 * is between <att_beg> and <equal>, both may be identical.
6656 */
6657
6658 /* look for end of cookie if there is an equal sign */
6659 if (equal < hdr_end && *equal == '=') {
6660 /* look for the beginning of the value */
6661 val_beg = equal + 1;
6662 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
6663 val_beg++;
6664
6665 /* find the end of the value, respecting quotes */
6666 next = find_cookie_value_end(val_beg, hdr_end);
6667
6668 /* make val_end point to the first white space or delimitor after the value */
6669 val_end = next;
6670 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
6671 val_end--;
6672 } else {
6673 val_beg = val_end = next = equal;
Willy Tarreau305ae852010-01-03 19:45:54 +01006674 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006675
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006676 /* We have nothing to do with attributes beginning with '$'. However,
6677 * they will automatically be removed if a header before them is removed,
6678 * since they're supposed to be linked together.
6679 */
6680 if (*att_beg == '$')
6681 continue;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006682
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006683 /* Ignore cookies with no equal sign */
6684 if (equal == next) {
6685 /* This is not our cookie, so we must preserve it. But if we already
6686 * scheduled another cookie for removal, we cannot remove the
6687 * complete header, but we can remove the previous block itself.
6688 */
6689 preserve_hdr = 1;
6690 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006691 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006692 val_end += delta;
6693 next += delta;
6694 hdr_end += delta;
6695 hdr_next += delta;
6696 cur_hdr->len += delta;
6697 http_msg_move_end(&txn->req, delta);
6698 prev = del_from;
6699 del_from = NULL;
6700 }
6701 continue;
Willy Tarreau305ae852010-01-03 19:45:54 +01006702 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006703
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006704 /* if there are spaces around the equal sign, we need to
6705 * strip them otherwise we'll get trouble for cookie captures,
6706 * or even for rewrites. Since this happens extremely rarely,
6707 * it does not hurt performance.
Willy Tarreau58f10d72006-12-04 02:26:12 +01006708 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006709 if (unlikely(att_end != equal || val_beg > equal + 1)) {
6710 int stripped_before = 0;
6711 int stripped_after = 0;
6712
6713 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006714 stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006715 equal += stripped_before;
6716 val_beg += stripped_before;
6717 }
6718
6719 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006720 stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006721 val_beg += stripped_after;
6722 stripped_before += stripped_after;
6723 }
6724
6725 val_end += stripped_before;
6726 next += stripped_before;
6727 hdr_end += stripped_before;
6728 hdr_next += stripped_before;
6729 cur_hdr->len += stripped_before;
6730 http_msg_move_end(&txn->req, stripped_before);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006731 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006732 /* now everything is as on the diagram above */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006733
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006734 /* First, let's see if we want to capture this cookie. We check
6735 * that we don't already have a client side cookie, because we
6736 * can only capture one. Also as an optimisation, we ignore
6737 * cookies shorter than the declared name.
6738 */
6739 if (t->fe->capture_name != NULL && txn->cli_cookie == NULL &&
6740 (val_end - att_beg >= t->fe->capture_namelen) &&
6741 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
6742 int log_len = val_end - att_beg;
6743
6744 if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) {
6745 Alert("HTTP logging : out of memory.\n");
6746 } else {
6747 if (log_len > t->fe->capture_len)
6748 log_len = t->fe->capture_len;
6749 memcpy(txn->cli_cookie, att_beg, log_len);
6750 txn->cli_cookie[log_len] = 0;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006751 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006752 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006753
Willy Tarreaubca99692010-10-06 19:25:55 +02006754 /* Persistence cookies in passive, rewrite or insert mode have the
6755 * following form :
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006756 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006757 * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]]
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006758 *
Willy Tarreaubca99692010-10-06 19:25:55 +02006759 * For cookies in prefix mode, the form is :
6760 *
6761 * Cookie: NAME=SRV~VALUE
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006762 */
6763 if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
6764 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
6765 struct server *srv = t->be->srv;
6766 char *delim;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006767
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006768 /* if we're in cookie prefix mode, we'll search the delimitor so that we
6769 * have the server ID between val_beg and delim, and the original cookie between
6770 * delim+1 and val_end. Otherwise, delim==val_end :
6771 *
6772 * Cookie: NAME=SRV; # in all but prefix modes
6773 * Cookie: NAME=SRV~OPAQUE ; # in prefix mode
6774 * | || || | |+-> next
6775 * | || || | +--> val_end
6776 * | || || +---------> delim
6777 * | || |+------------> val_beg
6778 * | || +-------------> att_end = equal
6779 * | |+-----------------> att_beg
6780 * | +------------------> prev
6781 * +-------------------------> hdr_beg
6782 */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006783
Willy Tarreau67402132012-05-31 20:40:20 +02006784 if (t->be->ck_opts & PR_CK_PFX) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006785 for (delim = val_beg; delim < val_end; delim++)
6786 if (*delim == COOKIE_DELIM)
6787 break;
Willy Tarreaubca99692010-10-06 19:25:55 +02006788 } else {
6789 char *vbar1;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006790 delim = val_end;
Willy Tarreaubca99692010-10-06 19:25:55 +02006791 /* Now check if the cookie contains a date field, which would
6792 * appear after a vertical bar ('|') just after the server name
6793 * and before the delimiter.
6794 */
6795 vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg);
6796 if (vbar1) {
6797 /* OK, so left of the bar is the server's cookie and
Willy Tarreauf64d1412010-10-07 20:06:11 +02006798 * right is the last seen date. It is a base64 encoded
6799 * 30-bit value representing the UNIX date since the
6800 * epoch in 4-second quantities.
Willy Tarreaubca99692010-10-06 19:25:55 +02006801 */
Willy Tarreauf64d1412010-10-07 20:06:11 +02006802 int val;
Willy Tarreaubca99692010-10-06 19:25:55 +02006803 delim = vbar1++;
Willy Tarreauf64d1412010-10-07 20:06:11 +02006804 if (val_end - vbar1 >= 5) {
6805 val = b64tos30(vbar1);
6806 if (val > 0)
6807 txn->cookie_last_date = val << 2;
6808 }
6809 /* look for a second vertical bar */
6810 vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1);
6811 if (vbar1 && (val_end - vbar1 > 5)) {
6812 val = b64tos30(vbar1 + 1);
6813 if (val > 0)
6814 txn->cookie_first_date = val << 2;
6815 }
Willy Tarreaubca99692010-10-06 19:25:55 +02006816 }
6817 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006818
Willy Tarreauf64d1412010-10-07 20:06:11 +02006819 /* if the cookie has an expiration date and the proxy wants to check
6820 * it, then we do that now. We first check if the cookie is too old,
6821 * then only if it has expired. We detect strict overflow because the
6822 * time resolution here is not great (4 seconds). Cookies with dates
6823 * in the future are ignored if their offset is beyond one day. This
6824 * allows an admin to fix timezone issues without expiring everyone
6825 * and at the same time avoids keeping unwanted side effects for too
6826 * long.
6827 */
6828 if (txn->cookie_first_date && t->be->cookie_maxlife &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006829 (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) ||
6830 ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006831 txn->flags &= ~TX_CK_MASK;
6832 txn->flags |= TX_CK_OLD;
6833 delim = val_beg; // let's pretend we have not found the cookie
6834 txn->cookie_first_date = 0;
6835 txn->cookie_last_date = 0;
6836 }
6837 else if (txn->cookie_last_date && t->be->cookie_maxidle &&
Willy Tarreauef4f3912010-10-07 21:00:29 +02006838 (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) ||
6839 ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) {
Willy Tarreauf64d1412010-10-07 20:06:11 +02006840 txn->flags &= ~TX_CK_MASK;
6841 txn->flags |= TX_CK_EXPIRED;
6842 delim = val_beg; // let's pretend we have not found the cookie
6843 txn->cookie_first_date = 0;
6844 txn->cookie_last_date = 0;
6845 }
6846
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006847 /* Here, we'll look for the first running server which supports the cookie.
6848 * This allows to share a same cookie between several servers, for example
6849 * to dedicate backup servers to specific servers only.
6850 * However, to prevent clients from sticking to cookie-less backup server
6851 * when they have incidentely learned an empty cookie, we simply ignore
6852 * empty cookies and mark them as invalid.
6853 * The same behaviour is applied when persistence must be ignored.
6854 */
Willy Tarreau4a5cade2012-04-05 21:09:48 +02006855 if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006856 srv = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006857
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006858 while (srv) {
6859 if (srv->cookie && (srv->cklen == delim - val_beg) &&
6860 !memcmp(val_beg, srv->cookie, delim - val_beg)) {
6861 if ((srv->state & SRV_RUNNING) ||
6862 (t->be->options & PR_O_PERSIST) ||
6863 (t->flags & SN_FORCE_PRST)) {
6864 /* we found the server and we can use it */
6865 txn->flags &= ~TX_CK_MASK;
6866 txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN;
6867 t->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01006868 t->target = &srv->obj_type;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006869 break;
6870 } else {
6871 /* we found a server, but it's down,
6872 * mark it as such and go on in case
6873 * another one is available.
6874 */
6875 txn->flags &= ~TX_CK_MASK;
6876 txn->flags |= TX_CK_DOWN;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006877 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006878 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006879 srv = srv->next;
6880 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006881
Willy Tarreauf64d1412010-10-07 20:06:11 +02006882 if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) {
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006883 /* no server matched this cookie or we deliberately skipped it */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006884 txn->flags &= ~TX_CK_MASK;
Willy Tarreauc89ccb62012-04-05 21:18:22 +02006885 if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED)))
6886 txn->flags |= TX_CK_UNUSED;
6887 else
6888 txn->flags |= TX_CK_INVALID;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006889 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006890
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006891 /* depending on the cookie mode, we may have to either :
6892 * - delete the complete cookie if we're in insert+indirect mode, so that
6893 * the server never sees it ;
6894 * - remove the server id from the cookie value, and tag the cookie as an
6895 * application cookie so that it does not get accidentely removed later,
6896 * if we're in cookie prefix mode
6897 */
Willy Tarreau67402132012-05-31 20:40:20 +02006898 if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006899 int delta; /* negative */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006900
Willy Tarreau9b28e032012-10-12 23:49:43 +02006901 delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006902 val_end += delta;
6903 next += delta;
6904 hdr_end += delta;
6905 hdr_next += delta;
6906 cur_hdr->len += delta;
6907 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006908
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006909 del_from = NULL;
6910 preserve_hdr = 1; /* we want to keep this cookie */
6911 }
6912 else if (del_from == NULL &&
Willy Tarreau67402132012-05-31 20:40:20 +02006913 (t->be->ck_opts & (PR_CK_INS | PR_CK_IND)) == (PR_CK_INS | PR_CK_IND)) {
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006914 del_from = prev;
6915 }
6916 } else {
6917 /* This is not our cookie, so we must preserve it. But if we already
6918 * scheduled another cookie for removal, we cannot remove the
6919 * complete header, but we can remove the previous block itself.
6920 */
6921 preserve_hdr = 1;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006922
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006923 if (del_from != NULL) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006924 int delta = del_hdr_value(req->buf, &del_from, prev);
Willy Tarreaub8105542010-11-24 18:31:28 +01006925 if (att_beg >= del_from)
6926 att_beg += delta;
6927 if (att_end >= del_from)
6928 att_end += delta;
6929 val_beg += delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006930 val_end += delta;
6931 next += delta;
6932 hdr_end += delta;
6933 hdr_next += delta;
6934 cur_hdr->len += delta;
6935 http_msg_move_end(&txn->req, delta);
6936 prev = del_from;
6937 del_from = NULL;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006938 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006939 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006940
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006941 /* Look for the appsession cookie unless persistence must be ignored */
6942 if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
6943 int cmp_len, value_len;
6944 char *value_begin;
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02006945
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006946 if (t->be->options2 & PR_O2_AS_PFX) {
6947 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
6948 value_begin = att_beg + t->be->appsession_name_len;
6949 value_len = val_end - att_beg - t->be->appsession_name_len;
6950 } else {
6951 cmp_len = att_end - att_beg;
6952 value_begin = val_beg;
6953 value_len = val_end - val_beg;
6954 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01006955
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006956 /* let's see if the cookie is our appcookie */
6957 if (cmp_len == t->be->appsession_name_len &&
6958 memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) {
6959 manage_client_side_appsession(t, value_begin, value_len);
6960 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006961 }
6962
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006963 /* continue with next cookie on this header line */
6964 att_beg = next;
6965 } /* for each cookie */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006966
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006967 /* There are no more cookies on this line.
6968 * We may still have one (or several) marked for deletion at the
6969 * end of the line. We must do this now in two ways :
6970 * - if some cookies must be preserved, we only delete from the
6971 * mark to the end of line ;
6972 * - if nothing needs to be preserved, simply delete the whole header
Willy Tarreau58f10d72006-12-04 02:26:12 +01006973 */
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006974 if (del_from) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01006975 int delta;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006976 if (preserve_hdr) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006977 delta = del_hdr_value(req->buf, &del_from, hdr_end);
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006978 hdr_end = del_from;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006979 cur_hdr->len += delta;
6980 } else {
Willy Tarreau9b28e032012-10-12 23:49:43 +02006981 delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006982
6983 /* FIXME: this should be a separate function */
Willy Tarreau4dbc4a22007-03-03 16:23:22 +01006984 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
6985 txn->hdr_idx.used--;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006986 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01006987 cur_idx = old_idx;
Willy Tarreau58f10d72006-12-04 02:26:12 +01006988 }
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006989 hdr_next += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01006990 http_msg_move_end(&txn->req, delta);
Willy Tarreau58f10d72006-12-04 02:26:12 +01006991 }
6992
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006993 /* check next header */
Willy Tarreau58f10d72006-12-04 02:26:12 +01006994 old_idx = cur_idx;
Willy Tarreaueb7b0a22010-08-31 16:45:02 +02006995 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01006996}
6997
6998
Willy Tarreaua15645d2007-03-18 16:22:39 +01006999/* Iterate the same filter through all response headers contained in <rtr>.
7000 * Returns 1 if this filter can be stopped upon return, otherwise 0.
7001 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007002int apply_filter_to_resp_headers(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007003{
7004 char term;
7005 char *cur_ptr, *cur_end, *cur_next;
7006 int cur_idx, old_idx, last_hdr;
7007 struct http_txn *txn = &t->txn;
7008 struct hdr_idx_elem *cur_hdr;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007009 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007010
7011 last_hdr = 0;
7012
Willy Tarreau9b28e032012-10-12 23:49:43 +02007013 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007014 old_idx = 0;
7015
7016 while (!last_hdr) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007017 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007018 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007019 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007020 (exp->action == ACT_ALLOW ||
7021 exp->action == ACT_DENY))
7022 return 0;
7023
7024 cur_idx = txn->hdr_idx.v[old_idx].next;
7025 if (!cur_idx)
7026 break;
7027
7028 cur_hdr = &txn->hdr_idx.v[cur_idx];
7029 cur_ptr = cur_next;
7030 cur_end = cur_ptr + cur_hdr->len;
7031 cur_next = cur_end + cur_hdr->cr + 1;
7032
7033 /* Now we have one header between cur_ptr and cur_end,
7034 * and the next header starts at cur_next.
7035 */
7036
7037 /* The annoying part is that pattern matching needs
7038 * that we modify the contents to null-terminate all
7039 * strings before testing them.
7040 */
7041
7042 term = *cur_end;
7043 *cur_end = '\0';
7044
7045 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7046 switch (exp->action) {
7047 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007048 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007049 last_hdr = 1;
7050 break;
7051
7052 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007053 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007054 last_hdr = 1;
7055 break;
7056
7057 case ACT_REPLACE:
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007058 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7059 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007060 /* FIXME: if the user adds a newline in the replacement, the
7061 * index will not be recalculated for now, and the new line
7062 * will not be counted as a new header.
7063 */
7064
7065 cur_end += delta;
7066 cur_next += delta;
7067 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007068 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007069 break;
7070
7071 case ACT_REMOVE:
Willy Tarreau9b28e032012-10-12 23:49:43 +02007072 delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007073 cur_next += delta;
7074
Willy Tarreaufa355d42009-11-29 18:12:29 +01007075 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007076 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7077 txn->hdr_idx.used--;
7078 cur_hdr->len = 0;
7079 cur_end = NULL; /* null-term has been rewritten */
Willy Tarreau26db59e2010-11-28 06:57:24 +01007080 cur_idx = old_idx;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007081 break;
7082
7083 }
7084 }
7085 if (cur_end)
7086 *cur_end = term; /* restore the string terminator */
7087
7088 /* keep the link from this header to next one in case of later
7089 * removal of next header.
7090 */
7091 old_idx = cur_idx;
7092 }
7093 return 0;
7094}
7095
7096
7097/* Apply the filter to the status line in the response buffer <rtr>.
7098 * Returns 0 if nothing has been done, 1 if the filter has been applied,
7099 * or -1 if a replacement resulted in an invalid status line.
7100 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007101int apply_filter_to_sts_line(struct session *t, struct channel *rtr, struct hdr_exp *exp)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007102{
7103 char term;
7104 char *cur_ptr, *cur_end;
7105 int done;
7106 struct http_txn *txn = &t->txn;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007107 int delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007108
7109
Willy Tarreau3d300592007-03-18 18:34:41 +01007110 if (unlikely(txn->flags & TX_SVDENY))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007111 return 1;
Willy Tarreau3d300592007-03-18 18:34:41 +01007112 else if (unlikely(txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007113 (exp->action == ACT_ALLOW ||
7114 exp->action == ACT_DENY))
7115 return 0;
7116 else if (exp->action == ACT_REMOVE)
7117 return 0;
7118
7119 done = 0;
7120
Willy Tarreau9b28e032012-10-12 23:49:43 +02007121 cur_ptr = rtr->buf->p;
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007122 cur_end = cur_ptr + txn->rsp.sl.st.l;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007123
7124 /* Now we have the status line between cur_ptr and cur_end */
7125
7126 /* The annoying part is that pattern matching needs
7127 * that we modify the contents to null-terminate all
7128 * strings before testing them.
7129 */
7130
7131 term = *cur_end;
7132 *cur_end = '\0';
7133
7134 if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) {
7135 switch (exp->action) {
7136 case ACT_ALLOW:
Willy Tarreau3d300592007-03-18 18:34:41 +01007137 txn->flags |= TX_SVALLOW;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007138 done = 1;
7139 break;
7140
7141 case ACT_DENY:
Willy Tarreau3d300592007-03-18 18:34:41 +01007142 txn->flags |= TX_SVDENY;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007143 done = 1;
7144 break;
7145
7146 case ACT_REPLACE:
7147 *cur_end = term; /* restore the string terminator */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007148 trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch);
7149 delta = buffer_replace2(rtr->buf, cur_ptr, cur_end, trash.str, trash.len);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007150 /* FIXME: if the user adds a newline in the replacement, the
7151 * index will not be recalculated for now, and the new line
7152 * will not be counted as a new header.
7153 */
7154
Willy Tarreaufa355d42009-11-29 18:12:29 +01007155 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007156 cur_end += delta;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007157 cur_end = (char *)http_parse_stsline(&txn->rsp,
Willy Tarreau02785762007-04-03 14:45:44 +02007158 HTTP_MSG_RPVER,
Willy Tarreaua15645d2007-03-18 16:22:39 +01007159 cur_ptr, cur_end + 1,
7160 NULL, NULL);
7161 if (unlikely(!cur_end))
7162 return -1;
7163
7164 /* we have a full respnse and we know that we have either a CR
7165 * or an LF at <ptr>.
7166 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007167 txn->status = strl2ui(rtr->buf->p + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l);
Willy Tarreau1ba0e5f2010-06-07 13:57:32 +02007168 hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.st.l, *cur_end == '\r');
Willy Tarreaua15645d2007-03-18 16:22:39 +01007169 /* there is no point trying this regex on headers */
7170 return 1;
7171 }
7172 }
7173 *cur_end = term; /* restore the string terminator */
7174 return done;
7175}
7176
7177
7178
7179/*
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007180 * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007181 * Returns 0 if everything is alright, or -1 in case a replacement lead to an
7182 * unparsable response.
7183 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007184int apply_filters_to_response(struct session *s, struct channel *rtr, struct proxy *px)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007185{
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007186 struct http_txn *txn = &s->txn;
7187 struct hdr_exp *exp;
7188
7189 for (exp = px->rsp_exp; exp; exp = exp->next) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007190 int ret;
7191
7192 /*
7193 * The interleaving of transformations and verdicts
7194 * makes it difficult to decide to continue or stop
7195 * the evaluation.
7196 */
7197
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007198 if (txn->flags & TX_SVDENY)
7199 break;
7200
Willy Tarreau3d300592007-03-18 18:34:41 +01007201 if ((txn->flags & TX_SVALLOW) &&
Willy Tarreaua15645d2007-03-18 16:22:39 +01007202 (exp->action == ACT_ALLOW || exp->action == ACT_DENY ||
7203 exp->action == ACT_PASS)) {
7204 exp = exp->next;
7205 continue;
7206 }
7207
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007208 /* if this filter had a condition, evaluate it now and skip to
7209 * next filter if the condition does not match.
7210 */
7211 if (exp->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02007212 ret = acl_exec_cond(exp->cond, px, s, txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007213 ret = acl_pass(ret);
7214 if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS)
7215 ret = !ret;
7216 if (!ret)
7217 continue;
7218 }
7219
Willy Tarreaua15645d2007-03-18 16:22:39 +01007220 /* Apply the filter to the status line. */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007221 ret = apply_filter_to_sts_line(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007222 if (unlikely(ret < 0))
7223 return -1;
7224
7225 if (likely(ret == 0)) {
7226 /* The filter did not match the response, it can be
7227 * iterated through all headers.
7228 */
Willy Tarreaufdb563c2010-01-31 15:43:27 +01007229 apply_filter_to_resp_headers(s, rtr, exp);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007230 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007231 }
7232 return 0;
7233}
7234
7235
Willy Tarreaua15645d2007-03-18 16:22:39 +01007236/*
Willy Tarreau396d2c62007-11-04 19:30:00 +01007237 * Manage server-side cookies. It can impact performance by about 2% so it is
Willy Tarreau24581ba2010-08-31 22:39:35 +02007238 * desirable to call it only when needed. This function is also used when we
7239 * just need to know if there is a cookie (eg: for check-cache).
Willy Tarreaua15645d2007-03-18 16:22:39 +01007240 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007241void manage_server_side_cookies(struct session *t, struct channel *res)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007242{
7243 struct http_txn *txn = &t->txn;
Willy Tarreau827aee92011-03-10 16:55:02 +01007244 struct server *srv;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007245 int is_cookie2;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007246 int cur_idx, old_idx, delta;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007247 char *hdr_beg, *hdr_end, *hdr_next;
7248 char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007249
Willy Tarreaua15645d2007-03-18 16:22:39 +01007250 /* Iterate through the headers.
7251 * we start with the start line.
7252 */
7253 old_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007254 hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007255
7256 while ((cur_idx = txn->hdr_idx.v[old_idx].next)) {
7257 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007258 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007259
7260 cur_hdr = &txn->hdr_idx.v[cur_idx];
Willy Tarreau24581ba2010-08-31 22:39:35 +02007261 hdr_beg = hdr_next;
7262 hdr_end = hdr_beg + cur_hdr->len;
7263 hdr_next = hdr_end + cur_hdr->cr + 1;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007264
Willy Tarreau24581ba2010-08-31 22:39:35 +02007265 /* We have one full header between hdr_beg and hdr_end, and the
7266 * next header starts at hdr_next. We're only interested in
7267 * "Set-Cookie" and "Set-Cookie2" headers.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007268 */
7269
Willy Tarreau24581ba2010-08-31 22:39:35 +02007270 is_cookie2 = 0;
7271 prev = hdr_beg + 10;
7272 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10);
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007273 if (!val) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007274 val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11);
7275 if (!val) {
7276 old_idx = cur_idx;
7277 continue;
7278 }
7279 is_cookie2 = 1;
7280 prev = hdr_beg + 11;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007281 }
7282
Willy Tarreau24581ba2010-08-31 22:39:35 +02007283 /* OK, right now we know we have a Set-Cookie* at hdr_beg, and
7284 * <prev> points to the colon.
7285 */
Willy Tarreauf1348312010-10-07 15:54:11 +02007286 txn->flags |= TX_SCK_PRESENT;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007287
Willy Tarreau24581ba2010-08-31 22:39:35 +02007288 /* Maybe we only wanted to see if there was a Set-Cookie (eg:
7289 * check-cache is enabled) and we are not interested in checking
7290 * them. Warning, the cookie capture is declared in the frontend.
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007291 */
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007292 if (t->be->cookie_name == NULL &&
7293 t->be->appsession_name == NULL &&
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007294 t->fe->capture_name == NULL)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007295 return;
7296
Willy Tarreau24581ba2010-08-31 22:39:35 +02007297 /* OK so now we know we have to process this response cookie.
7298 * The format of the Set-Cookie header is slightly different
7299 * from the format of the Cookie header in that it does not
7300 * support the comma as a cookie delimiter (thus the header
7301 * cannot be folded) because the Expires attribute described in
7302 * the original Netscape's spec may contain an unquoted date
7303 * with a comma inside. We have to live with this because
7304 * many browsers don't support Max-Age and some browsers don't
7305 * support quoted strings. However the Set-Cookie2 header is
7306 * clean.
7307 *
7308 * We have to keep multiple pointers in order to support cookie
7309 * removal at the beginning, middle or end of header without
7310 * corrupting the header (in case of set-cookie2). A special
7311 * pointer, <scav> points to the beginning of the set-cookie-av
7312 * fields after the first semi-colon. The <next> pointer points
7313 * either to the end of line (set-cookie) or next unquoted comma
7314 * (set-cookie2). All of these headers are valid :
7315 *
7316 * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n
7317 * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7318 * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n
7319 * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n
7320 * | | | | | | | | | |
7321 * | | | | | | | | +-> next hdr_end <--+
7322 * | | | | | | | +------------> scav
7323 * | | | | | | +--------------> val_end
7324 * | | | | | +--------------------> val_beg
7325 * | | | | +----------------------> equal
7326 * | | | +------------------------> att_end
7327 * | | +----------------------------> att_beg
7328 * | +------------------------------> prev
7329 * +-----------------------------------------> hdr_beg
7330 */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007331
Willy Tarreau24581ba2010-08-31 22:39:35 +02007332 for (; prev < hdr_end; prev = next) {
7333 /* Iterate through all cookies on this line */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007334
Willy Tarreau24581ba2010-08-31 22:39:35 +02007335 /* find att_beg */
7336 att_beg = prev + 1;
7337 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
7338 att_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007339
Willy Tarreau24581ba2010-08-31 22:39:35 +02007340 /* find att_end : this is the first character after the last non
7341 * space before the equal. It may be equal to hdr_end.
7342 */
7343 equal = att_end = att_beg;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007344
Willy Tarreau24581ba2010-08-31 22:39:35 +02007345 while (equal < hdr_end) {
7346 if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ','))
7347 break;
7348 if (http_is_spht[(unsigned char)*equal++])
7349 continue;
7350 att_end = equal;
7351 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007352
Willy Tarreau24581ba2010-08-31 22:39:35 +02007353 /* here, <equal> points to '=', a delimitor or the end. <att_end>
7354 * is between <att_beg> and <equal>, both may be identical.
7355 */
7356
7357 /* look for end of cookie if there is an equal sign */
7358 if (equal < hdr_end && *equal == '=') {
7359 /* look for the beginning of the value */
7360 val_beg = equal + 1;
7361 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
7362 val_beg++;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007363
Willy Tarreau24581ba2010-08-31 22:39:35 +02007364 /* find the end of the value, respecting quotes */
7365 next = find_cookie_value_end(val_beg, hdr_end);
7366
7367 /* make val_end point to the first white space or delimitor after the value */
7368 val_end = next;
7369 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
7370 val_end--;
7371 } else {
7372 /* <equal> points to next comma, semi-colon or EOL */
7373 val_beg = val_end = next = equal;
7374 }
7375
7376 if (next < hdr_end) {
7377 /* Set-Cookie2 supports multiple cookies, and <next> points to
7378 * a colon or semi-colon before the end. So skip all attr-value
7379 * pairs and look for the next comma. For Set-Cookie, since
7380 * commas are permitted in values, skip to the end.
7381 */
7382 if (is_cookie2)
7383 next = find_hdr_value_end(next, hdr_end);
7384 else
7385 next = hdr_end;
7386 }
7387
7388 /* Now everything is as on the diagram above */
7389
7390 /* Ignore cookies with no equal sign */
7391 if (equal == val_end)
7392 continue;
7393
7394 /* If there are spaces around the equal sign, we need to
7395 * strip them otherwise we'll get trouble for cookie captures,
7396 * or even for rewrites. Since this happens extremely rarely,
7397 * it does not hurt performance.
Willy Tarreaua15645d2007-03-18 16:22:39 +01007398 */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007399 if (unlikely(att_end != equal || val_beg > equal + 1)) {
7400 int stripped_before = 0;
7401 int stripped_after = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007402
Willy Tarreau24581ba2010-08-31 22:39:35 +02007403 if (att_end != equal) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007404 stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007405 equal += stripped_before;
7406 val_beg += stripped_before;
7407 }
7408
7409 if (val_beg > equal + 1) {
Willy Tarreau9b28e032012-10-12 23:49:43 +02007410 stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007411 val_beg += stripped_after;
7412 stripped_before += stripped_after;
7413 }
7414
7415 val_end += stripped_before;
7416 next += stripped_before;
7417 hdr_end += stripped_before;
7418 hdr_next += stripped_before;
7419 cur_hdr->len += stripped_before;
Willy Tarreau1fc1f452011-04-07 22:35:37 +02007420 http_msg_move_end(&txn->rsp, stripped_before);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007421 }
7422
7423 /* First, let's see if we want to capture this cookie. We check
7424 * that we don't already have a server side cookie, because we
7425 * can only capture one. Also as an optimisation, we ignore
7426 * cookies shorter than the declared name.
7427 */
Willy Tarreaufd39dda2008-10-17 12:01:58 +02007428 if (t->fe->capture_name != NULL &&
Willy Tarreau3bac9ff2007-03-18 17:31:28 +01007429 txn->srv_cookie == NULL &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007430 (val_end - att_beg >= t->fe->capture_namelen) &&
7431 memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) {
7432 int log_len = val_end - att_beg;
Willy Tarreau086b3b42007-05-13 21:45:51 +02007433 if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007434 Alert("HTTP logging : out of memory.\n");
7435 }
Willy Tarreauf70fc752010-11-19 11:27:18 +01007436 else {
7437 if (log_len > t->fe->capture_len)
7438 log_len = t->fe->capture_len;
7439 memcpy(txn->srv_cookie, att_beg, log_len);
7440 txn->srv_cookie[log_len] = 0;
7441 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007442 }
7443
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007444 srv = objt_server(t->target);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007445 /* now check if we need to process it for persistence */
Willy Tarreau24581ba2010-08-31 22:39:35 +02007446 if (!(t->flags & SN_IGNORE_PRST) &&
7447 (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) &&
7448 (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) {
Willy Tarreauf1348312010-10-07 15:54:11 +02007449 /* assume passive cookie by default */
7450 txn->flags &= ~TX_SCK_MASK;
7451 txn->flags |= TX_SCK_FOUND;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007452
7453 /* If the cookie is in insert mode on a known server, we'll delete
7454 * this occurrence because we'll insert another one later.
7455 * We'll delete it too if the "indirect" option is set and we're in
Willy Tarreau24581ba2010-08-31 22:39:35 +02007456 * a direct access.
7457 */
Willy Tarreau67402132012-05-31 20:40:20 +02007458 if (t->be->ck_opts & PR_CK_PSV) {
Willy Tarreauba4c5be2010-10-23 12:46:42 +02007459 /* The "preserve" flag was set, we don't want to touch the
7460 * server's cookie.
7461 */
7462 }
Willy Tarreau67402132012-05-31 20:40:20 +02007463 else if ((srv && (t->be->ck_opts & PR_CK_INS)) ||
7464 ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007465 /* this cookie must be deleted */
7466 if (*prev == ':' && next == hdr_end) {
7467 /* whole header */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007468 delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007469 txn->hdr_idx.v[old_idx].next = cur_hdr->next;
7470 txn->hdr_idx.used--;
7471 cur_hdr->len = 0;
Willy Tarreau26db59e2010-11-28 06:57:24 +01007472 cur_idx = old_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007473 hdr_next += delta;
7474 http_msg_move_end(&txn->rsp, delta);
7475 /* note: while both invalid now, <next> and <hdr_end>
7476 * are still equal, so the for() will stop as expected.
7477 */
7478 } else {
7479 /* just remove the value */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007480 int delta = del_hdr_value(res->buf, &prev, next);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007481 next = prev;
7482 hdr_end += delta;
7483 hdr_next += delta;
7484 cur_hdr->len += delta;
7485 http_msg_move_end(&txn->rsp, delta);
7486 }
Willy Tarreauf1348312010-10-07 15:54:11 +02007487 txn->flags &= ~TX_SCK_MASK;
Willy Tarreau3d300592007-03-18 18:34:41 +01007488 txn->flags |= TX_SCK_DELETED;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007489 /* and go on with next cookie */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007490 }
Willy Tarreau67402132012-05-31 20:40:20 +02007491 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007492 /* replace bytes val_beg->val_end with the cookie name associated
Willy Tarreaua15645d2007-03-18 16:22:39 +01007493 * with this server since we know it.
7494 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007495 delta = buffer_replace2(res->buf, val_beg, val_end, srv->cookie, srv->cklen);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007496 next += delta;
7497 hdr_end += delta;
7498 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007499 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007500 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007501
Willy Tarreauf1348312010-10-07 15:54:11 +02007502 txn->flags &= ~TX_SCK_MASK;
7503 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007504 }
Willy Tarreaua0590312012-06-06 16:07:00 +02007505 else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) {
Willy Tarreaua15645d2007-03-18 16:22:39 +01007506 /* insert the cookie name associated with this server
Willy Tarreau24581ba2010-08-31 22:39:35 +02007507 * before existing cookie, and insert a delimiter between them..
Willy Tarreaua15645d2007-03-18 16:22:39 +01007508 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007509 delta = buffer_replace2(res->buf, val_beg, val_beg, srv->cookie, srv->cklen + 1);
Willy Tarreau24581ba2010-08-31 22:39:35 +02007510 next += delta;
7511 hdr_end += delta;
7512 hdr_next += delta;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007513 cur_hdr->len += delta;
Willy Tarreaufa355d42009-11-29 18:12:29 +01007514 http_msg_move_end(&txn->rsp, delta);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007515
Willy Tarreau827aee92011-03-10 16:55:02 +01007516 val_beg[srv->cklen] = COOKIE_DELIM;
Willy Tarreauf1348312010-10-07 15:54:11 +02007517 txn->flags &= ~TX_SCK_MASK;
7518 txn->flags |= TX_SCK_REPLACED;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007519 }
7520 }
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02007521 /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */
7522 else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007523 int cmp_len, value_len;
7524 char *value_begin;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007525
Cyril Bontéb21570a2009-11-29 20:04:48 +01007526 if (t->be->options2 & PR_O2_AS_PFX) {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007527 cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len);
7528 value_begin = att_beg + t->be->appsession_name_len;
7529 value_len = MIN(t->be->appsession_len, val_end - att_beg - t->be->appsession_name_len);
Cyril Bontéb21570a2009-11-29 20:04:48 +01007530 } else {
Willy Tarreau24581ba2010-08-31 22:39:35 +02007531 cmp_len = att_end - att_beg;
7532 value_begin = val_beg;
7533 value_len = MIN(t->be->appsession_len, val_end - val_beg);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007534 }
Cyril Bontéb21570a2009-11-29 20:04:48 +01007535
Cyril Bonté17530c32010-04-06 21:11:10 +02007536 if ((cmp_len == t->be->appsession_name_len) &&
Willy Tarreau24581ba2010-08-31 22:39:35 +02007537 (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7538 /* free a possibly previously allocated memory */
7539 pool_free2(apools.sessid, txn->sessid);
7540
Cyril Bontéb21570a2009-11-29 20:04:48 +01007541 /* Store the sessid in the session for future use */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007542 if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) {
Cyril Bontéb21570a2009-11-29 20:04:48 +01007543 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7544 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
7545 return;
7546 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007547 memcpy(txn->sessid, value_begin, value_len);
7548 txn->sessid[value_len] = 0;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007549 }
Willy Tarreau24581ba2010-08-31 22:39:35 +02007550 }
7551 /* that's done for this cookie, check the next one on the same
7552 * line when next != hdr_end (only if is_cookie2).
7553 */
7554 }
7555 /* check next header */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007556 old_idx = cur_idx;
Willy Tarreau24581ba2010-08-31 22:39:35 +02007557 }
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007558
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007559 if (txn->sessid != NULL) {
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007560 appsess *asession = NULL;
7561 /* only do insert, if lookup fails */
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007562 asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007563 if (asession == NULL) {
Willy Tarreau1fac7532010-01-09 19:23:06 +01007564 size_t server_id_len;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007565 if ((asession = pool_alloc2(pool2_appsess)) == NULL) {
7566 Alert("Not enough Memory process_srv():asession:calloc().\n");
7567 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n");
7568 return;
7569 }
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007570 asession->serverid = NULL; /* to avoid a double free in case of allocation error */
7571
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007572 if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) {
7573 Alert("Not enough Memory process_srv():asession->sessid:malloc().\n");
7574 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007575 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007576 return;
7577 }
Willy Tarreaua3377ee2010-01-10 10:49:11 +01007578 memcpy(asession->sessid, txn->sessid, t->be->appsession_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007579 asession->sessid[t->be->appsession_len] = 0;
7580
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007581 server_id_len = strlen(objt_server(t->target)->id) + 1;
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007582 if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) {
Willy Tarreau77eb9b82010-11-19 11:29:06 +01007583 Alert("Not enough Memory process_srv():asession->serverid:malloc().\n");
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007584 send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n");
Cyril Bonté41689c22010-01-10 00:30:14 +01007585 t->be->htbl_proxy.destroy(asession);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007586 return;
7587 }
7588 asession->serverid[0] = '\0';
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007589 memcpy(asession->serverid, objt_server(t->target)->id, server_id_len);
Cyril Bontébf47aeb2009-10-15 00:15:40 +02007590
7591 asession->request_count = 0;
7592 appsession_hash_insert(&(t->be->htbl_proxy), asession);
7593 }
7594
7595 asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession);
7596 asession->request_count++;
7597 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007598}
7599
7600
Willy Tarreaua15645d2007-03-18 16:22:39 +01007601/*
7602 * Check if response is cacheable or not. Updates t->flags.
7603 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02007604void check_response_for_cacheability(struct session *t, struct channel *rtr)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007605{
7606 struct http_txn *txn = &t->txn;
7607 char *p1, *p2;
7608
7609 char *cur_ptr, *cur_end, *cur_next;
7610 int cur_idx;
7611
Willy Tarreau5df51872007-11-25 16:20:08 +01007612 if (!(txn->flags & TX_CACHEABLE))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007613 return;
7614
7615 /* Iterate through the headers.
7616 * we start with the start line.
7617 */
7618 cur_idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007619 cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx);
Willy Tarreaua15645d2007-03-18 16:22:39 +01007620
7621 while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) {
7622 struct hdr_idx_elem *cur_hdr;
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007623 int val;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007624
7625 cur_hdr = &txn->hdr_idx.v[cur_idx];
7626 cur_ptr = cur_next;
7627 cur_end = cur_ptr + cur_hdr->len;
7628 cur_next = cur_end + cur_hdr->cr + 1;
7629
7630 /* We have one full header between cur_ptr and cur_end, and the
7631 * next header starts at cur_next. We're only interested in
7632 * "Cookie:" headers.
7633 */
7634
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007635 val = http_header_match2(cur_ptr, cur_end, "Pragma", 6);
7636 if (val) {
7637 if ((cur_end - (cur_ptr + val) >= 8) &&
7638 strncasecmp(cur_ptr + val, "no-cache", 8) == 0) {
7639 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
7640 return;
7641 }
Willy Tarreaua15645d2007-03-18 16:22:39 +01007642 }
7643
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007644 val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13);
7645 if (!val)
Willy Tarreaua15645d2007-03-18 16:22:39 +01007646 continue;
7647
7648 /* OK, right now we know we have a cache-control header at cur_ptr */
7649
Willy Tarreauaa9dce32007-03-18 23:50:16 +01007650 p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */
Willy Tarreaua15645d2007-03-18 16:22:39 +01007651
7652 if (p1 >= cur_end) /* no more info */
7653 continue;
7654
7655 /* p1 is at the beginning of the value */
7656 p2 = p1;
7657
Willy Tarreau8f8e6452007-06-17 21:51:38 +02007658 while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2))
Willy Tarreaua15645d2007-03-18 16:22:39 +01007659 p2++;
7660
7661 /* we have a complete value between p1 and p2 */
7662 if (p2 < cur_end && *p2 == '=') {
7663 /* we have something of the form no-cache="set-cookie" */
7664 if ((cur_end - p1 >= 21) &&
7665 strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0
7666 && (p1[20] == '"' || p1[20] == ','))
Willy Tarreau3d300592007-03-18 18:34:41 +01007667 txn->flags &= ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007668 continue;
7669 }
7670
7671 /* OK, so we know that either p2 points to the end of string or to a comma */
7672 if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) ||
7673 ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) ||
7674 ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) ||
7675 ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007676 txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007677 return;
7678 }
7679
7680 if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) {
Willy Tarreau3d300592007-03-18 18:34:41 +01007681 txn->flags |= TX_CACHEABLE | TX_CACHE_COOK;
Willy Tarreaua15645d2007-03-18 16:22:39 +01007682 continue;
7683 }
7684 }
7685}
7686
7687
Willy Tarreau58f10d72006-12-04 02:26:12 +01007688/*
7689 * Try to retrieve a known appsession in the URI, then the associated server.
7690 * If the server is found, it's assigned to the session.
7691 */
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007692void get_srv_from_appsession(struct session *t, const char *begin, int len)
Willy Tarreau58f10d72006-12-04 02:26:12 +01007693{
Cyril Bontéb21570a2009-11-29 20:04:48 +01007694 char *end_params, *first_param, *cur_param, *next_param;
7695 char separator;
7696 int value_len;
7697
7698 int mode = t->be->options2 & PR_O2_AS_M_ANY;
Willy Tarreau58f10d72006-12-04 02:26:12 +01007699
Willy Tarreaue2e27a52007-04-01 00:01:37 +02007700 if (t->be->appsession_name == NULL ||
Cyril Bonté17530c32010-04-06 21:11:10 +02007701 (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 +01007702 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007703 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007704
Cyril Bontéb21570a2009-11-29 20:04:48 +01007705 first_param = NULL;
7706 switch (mode) {
7707 case PR_O2_AS_M_PP:
7708 first_param = memchr(begin, ';', len);
7709 break;
7710 case PR_O2_AS_M_QS:
7711 first_param = memchr(begin, '?', len);
7712 break;
7713 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007714
Cyril Bontéb21570a2009-11-29 20:04:48 +01007715 if (first_param == NULL) {
Willy Tarreau58f10d72006-12-04 02:26:12 +01007716 return;
Cyril Bontéb21570a2009-11-29 20:04:48 +01007717 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007718
Cyril Bontéb21570a2009-11-29 20:04:48 +01007719 switch (mode) {
7720 case PR_O2_AS_M_PP:
7721 if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) {
7722 end_params = (char *) begin + len;
7723 }
7724 separator = ';';
7725 break;
7726 case PR_O2_AS_M_QS:
7727 end_params = (char *) begin + len;
7728 separator = '&';
7729 break;
7730 default:
7731 /* unknown mode, shouldn't happen */
7732 return;
7733 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007734
Cyril Bontéb21570a2009-11-29 20:04:48 +01007735 cur_param = next_param = end_params;
7736 while (cur_param > first_param) {
7737 cur_param--;
7738 if ((cur_param[0] == separator) || (cur_param == first_param)) {
7739 /* let's see if this is the appsession parameter */
7740 if ((cur_param + t->be->appsession_name_len + 1 < next_param) &&
7741 ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') &&
7742 (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) {
7743 /* Cool... it's the right one */
7744 cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2);
7745 value_len = MIN(t->be->appsession_len, next_param - cur_param);
7746 if (value_len > 0) {
7747 manage_client_side_appsession(t, cur_param, value_len);
7748 }
7749 break;
7750 }
7751 next_param = cur_param;
7752 }
7753 }
Willy Tarreau58f10d72006-12-04 02:26:12 +01007754#if defined(DEBUG_HASH)
Aleksandar Lazic697bbb02008-08-13 19:57:02 +02007755 Alert("get_srv_from_appsession\n");
Willy Tarreau51041c72007-09-09 21:56:53 +02007756 appsession_hash_dump(&(t->be->htbl_proxy));
Willy Tarreau58f10d72006-12-04 02:26:12 +01007757#endif
Willy Tarreau58f10d72006-12-04 02:26:12 +01007758}
7759
Willy Tarreaub2513902006-12-17 14:52:38 +01007760/*
Cyril Bonté70be45d2010-10-12 00:14:35 +02007761 * In a GET, HEAD or POST request, check if the requested URI matches the stats uri
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007762 * for the current backend.
Willy Tarreaub2513902006-12-17 14:52:38 +01007763 *
Cyril Bonté70be45d2010-10-12 00:14:35 +02007764 * It is assumed that the request is either a HEAD, GET, or POST and that the
Willy Tarreau295a8372011-03-10 11:25:07 +01007765 * uri_auth field is valid.
Willy Tarreaub2513902006-12-17 14:52:38 +01007766 *
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007767 * Returns 1 if stats should be provided, otherwise 0.
Willy Tarreaub2513902006-12-17 14:52:38 +01007768 */
Willy Tarreau295a8372011-03-10 11:25:07 +01007769int stats_check_uri(struct stream_interface *si, struct http_txn *txn, struct proxy *backend)
Willy Tarreaub2513902006-12-17 14:52:38 +01007770{
7771 struct uri_auth *uri_auth = backend->uri_auth;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007772 struct http_msg *msg = &txn->req;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007773 const char *uri = msg->chn->buf->p+ msg->sl.rq.u;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007774 const char *h;
Willy Tarreaub2513902006-12-17 14:52:38 +01007775
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007776 if (!uri_auth)
7777 return 0;
7778
Cyril Bonté70be45d2010-10-12 00:14:35 +02007779 if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD && txn->meth != HTTP_METH_POST)
Krzysztof Piotr Oledzki8c8bd452010-01-29 19:29:32 +01007780 return 0;
7781
Willy Tarreau295a8372011-03-10 11:25:07 +01007782 memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats));
Cyril Bonté19979e12012-04-04 12:57:21 +02007783 si->applet.ctx.stats.st_code = STAT_STATUS_INIT;
Willy Tarreau354898b2012-12-23 18:15:23 +01007784 si->applet.ctx.stats.flags |= STAT_FMT_HTML; /* assume HTML mode by default */
Willy Tarreau39f7e6d2008-03-17 21:38:24 +01007785
Willy Tarreau8d5d7f22007-01-21 19:16:41 +01007786 /* check URI size */
Willy Tarreau3a215be2012-03-09 21:39:51 +01007787 if (uri_auth->uri_len > msg->sl.rq.u_l)
Willy Tarreaub2513902006-12-17 14:52:38 +01007788 return 0;
7789
Willy Tarreau3a215be2012-03-09 21:39:51 +01007790 h = uri;
Willy Tarreau0214c3a2007-01-07 13:47:30 +01007791 if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0)
Willy Tarreaub2513902006-12-17 14:52:38 +01007792 return 0;
7793
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007794 h += uri_auth->uri_len;
Willy Tarreau3a215be2012-03-09 21:39:51 +01007795 while (h <= uri + msg->sl.rq.u_l - 3) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007796 if (memcmp(h, ";up", 3) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007797 si->applet.ctx.stats.flags |= STAT_HIDE_DOWN;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007798 break;
7799 }
7800 h++;
7801 }
7802
7803 if (uri_auth->refresh) {
Willy Tarreau3a215be2012-03-09 21:39:51 +01007804 h = uri + uri_auth->uri_len;
7805 while (h <= uri + msg->sl.rq.u_l - 10) {
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007806 if (memcmp(h, ";norefresh", 10) == 0) {
Willy Tarreau295a8372011-03-10 11:25:07 +01007807 si->applet.ctx.stats.flags |= STAT_NO_REFRESH;
Willy Tarreaue7150cd2007-07-25 14:43:32 +02007808 break;
7809 }
7810 h++;
7811 }
7812 }
7813
Willy Tarreau3a215be2012-03-09 21:39:51 +01007814 h = uri + uri_auth->uri_len;
7815 while (h <= uri + msg->sl.rq.u_l - 4) {
Willy Tarreau55bb8452007-10-17 18:44:57 +02007816 if (memcmp(h, ";csv", 4) == 0) {
Willy Tarreau354898b2012-12-23 18:15:23 +01007817 si->applet.ctx.stats.flags &= ~STAT_FMT_HTML;
Willy Tarreau55bb8452007-10-17 18:44:57 +02007818 break;
7819 }
7820 h++;
7821 }
7822
Willy Tarreau3a215be2012-03-09 21:39:51 +01007823 h = uri + uri_auth->uri_len;
7824 while (h <= uri + msg->sl.rq.u_l - 8) {
Cyril Bonté70be45d2010-10-12 00:14:35 +02007825 if (memcmp(h, ";st=", 4) == 0) {
Cyril Bonté19979e12012-04-04 12:57:21 +02007826 int i;
Cyril Bonté70be45d2010-10-12 00:14:35 +02007827 h += 4;
Cyril Bonté20a804a2012-05-10 19:42:52 +02007828 si->applet.ctx.stats.st_code = STAT_STATUS_UNKN;
Cyril Bonté19979e12012-04-04 12:57:21 +02007829 for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) {
7830 if (strncmp(stat_status_codes[i], h, 4) == 0) {
7831 si->applet.ctx.stats.st_code = i;
7832 break;
7833 }
7834 }
Cyril Bonté70be45d2010-10-12 00:14:35 +02007835 break;
7836 }
7837 h++;
7838 }
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02007839
7840 si->applet.ctx.stats.scope_str = 0;
7841 si->applet.ctx.stats.scope_len = 0;
7842 h = uri + uri_auth->uri_len;
7843 while (h <= uri + msg->sl.rq.u_l - 8) {
7844 if (memcmp(h, STAT_SCOPE_INPUT_NAME "=", strlen(STAT_SCOPE_INPUT_NAME) + 1) == 0) {
7845 int itx = 0;
7846 const char *h2;
7847 char scope_txt[STAT_SCOPE_TXT_MAXLEN + 1];
7848 const char *err;
7849
7850 h += strlen(STAT_SCOPE_INPUT_NAME) + 1;
7851 h2 = h;
7852 si->applet.ctx.stats.scope_str = h2 - msg->chn->buf->p;
7853 while (*h != ';' && *h != '\0' && *h != '&' && *h != ' ' && *h != '\n') {
7854 itx++;
7855 h++;
7856 }
7857
7858 if (itx > STAT_SCOPE_TXT_MAXLEN)
7859 itx = STAT_SCOPE_TXT_MAXLEN;
7860 si->applet.ctx.stats.scope_len = itx;
7861
7862 /* scope_txt = search query, si->applet.ctx.stats.scope_len is always <= STAT_SCOPE_TXT_MAXLEN */
7863 memcpy(scope_txt, h2, itx);
7864 scope_txt[itx] = '\0';
7865 err = invalid_char(scope_txt);
7866 if (err) {
7867 /* bad char in search text => clear scope */
7868 si->applet.ctx.stats.scope_str = 0;
7869 si->applet.ctx.stats.scope_len = 0;
7870 }
7871 break;
7872 }
7873 h++;
7874 }
7875
7876
Willy Tarreaub2513902006-12-17 14:52:38 +01007877 return 1;
7878}
7879
Willy Tarreau4076a152009-04-02 15:18:36 +02007880/*
7881 * Capture a bad request or response and archive it in the proxy's structure.
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007882 * By default it tries to report the error position as msg->err_pos. However if
7883 * this one is not set, it will then report msg->next, which is the last known
7884 * parsing point. The function is able to deal with wrapping buffers. It always
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007885 * displays buffers as a contiguous area starting at buf->p.
Willy Tarreau4076a152009-04-02 15:18:36 +02007886 */
7887void http_capture_bad_message(struct error_snapshot *es, struct session *s,
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007888 struct http_msg *msg,
Willy Tarreau078272e2010-12-12 12:46:33 +01007889 int state, struct proxy *other_end)
Willy Tarreau4076a152009-04-02 15:18:36 +02007890{
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007891 struct channel *chn = msg->chn;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007892 int len1, len2;
Willy Tarreau8a0cef22012-03-09 13:39:23 +01007893
Willy Tarreau9b28e032012-10-12 23:49:43 +02007894 es->len = MIN(chn->buf->i, sizeof(es->buf));
7895 len1 = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007896 len1 = MIN(len1, es->len);
7897 len2 = es->len - len1; /* remaining data if buffer wraps */
7898
Willy Tarreau9b28e032012-10-12 23:49:43 +02007899 memcpy(es->buf, chn->buf->p, len1);
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007900 if (len2)
Willy Tarreau9b28e032012-10-12 23:49:43 +02007901 memcpy(es->buf + len1, chn->buf->data, len2);
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007902
Willy Tarreau4076a152009-04-02 15:18:36 +02007903 if (msg->err_pos >= 0)
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007904 es->pos = msg->err_pos;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007905 else
Willy Tarreau69d8c5d2012-05-08 09:44:41 +02007906 es->pos = msg->next;
Willy Tarreau81f2fb92010-12-12 13:09:08 +01007907
Willy Tarreau4076a152009-04-02 15:18:36 +02007908 es->when = date; // user-visible date
7909 es->sid = s->uniq_id;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01007910 es->srv = objt_server(s->target);
Willy Tarreau4076a152009-04-02 15:18:36 +02007911 es->oe = other_end;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02007912 es->src = s->req->prod->conn->addr.from;
Willy Tarreau078272e2010-12-12 12:46:33 +01007913 es->state = state;
Willy Tarreau10479e42010-12-12 14:00:34 +01007914 es->ev_id = error_snapshot_id++;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007915 es->b_flags = chn->flags;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007916 es->s_flags = s->flags;
7917 es->t_flags = s->txn.flags;
7918 es->m_flags = msg->flags;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007919 es->b_out = chn->buf->o;
7920 es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p;
Willy Tarreaucdbdd522012-10-12 22:51:15 +02007921 es->b_tot = chn->total;
Willy Tarreaud04b1bc2012-05-08 11:03:10 +02007922 es->m_clen = msg->chunk_len;
7923 es->m_blen = msg->body_len;
Willy Tarreau4076a152009-04-02 15:18:36 +02007924}
Willy Tarreaub2513902006-12-17 14:52:38 +01007925
Willy Tarreau294c4732011-12-16 21:35:50 +01007926/* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of
7927 * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is
7928 * performed over the whole headers. Otherwise it must contain a valid header
7929 * context, initialised with ctx->idx=0 for the first lookup in a series. If
7930 * <occ> is positive or null, occurrence #occ from the beginning (or last ctx)
7931 * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less
7932 * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is
7933 * -1.
7934 * The return value is 0 if nothing was found, or non-zero otherwise.
Willy Tarreaubce70882009-09-07 11:51:47 +02007935 */
Willy Tarreau185b5c42012-04-26 15:11:51 +02007936unsigned int http_get_hdr(const struct http_msg *msg, const char *hname, int hlen,
Willy Tarreau294c4732011-12-16 21:35:50 +01007937 struct hdr_idx *idx, int occ,
7938 struct hdr_ctx *ctx, char **vptr, int *vlen)
Willy Tarreaubce70882009-09-07 11:51:47 +02007939{
Willy Tarreau294c4732011-12-16 21:35:50 +01007940 struct hdr_ctx local_ctx;
7941 char *ptr_hist[MAX_HDR_HISTORY];
7942 int len_hist[MAX_HDR_HISTORY];
Willy Tarreaubce70882009-09-07 11:51:47 +02007943 unsigned int hist_ptr;
Willy Tarreau294c4732011-12-16 21:35:50 +01007944 int found;
Willy Tarreaubce70882009-09-07 11:51:47 +02007945
Willy Tarreau294c4732011-12-16 21:35:50 +01007946 if (!ctx) {
7947 local_ctx.idx = 0;
7948 ctx = &local_ctx;
7949 }
7950
Willy Tarreaubce70882009-09-07 11:51:47 +02007951 if (occ >= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007952 /* search from the beginning */
Willy Tarreau9b28e032012-10-12 23:49:43 +02007953 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreaubce70882009-09-07 11:51:47 +02007954 occ--;
7955 if (occ <= 0) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007956 *vptr = ctx->line + ctx->val;
7957 *vlen = ctx->vlen;
7958 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007959 }
7960 }
Willy Tarreau294c4732011-12-16 21:35:50 +01007961 return 0;
Willy Tarreaubce70882009-09-07 11:51:47 +02007962 }
7963
7964 /* negative occurrence, we scan all the list then walk back */
7965 if (-occ > MAX_HDR_HISTORY)
7966 return 0;
7967
Willy Tarreau294c4732011-12-16 21:35:50 +01007968 found = hist_ptr = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02007969 while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) {
Willy Tarreau294c4732011-12-16 21:35:50 +01007970 ptr_hist[hist_ptr] = ctx->line + ctx->val;
7971 len_hist[hist_ptr] = ctx->vlen;
7972 if (++hist_ptr >= MAX_HDR_HISTORY)
Willy Tarreaubce70882009-09-07 11:51:47 +02007973 hist_ptr = 0;
7974 found++;
7975 }
7976 if (-occ > found)
7977 return 0;
7978 /* OK now we have the last occurrence in [hist_ptr-1], and we need to
7979 * find occurrence -occ, so we have to check [hist_ptr+occ].
7980 */
7981 hist_ptr += occ;
7982 if (hist_ptr >= MAX_HDR_HISTORY)
7983 hist_ptr -= MAX_HDR_HISTORY;
Willy Tarreau294c4732011-12-16 21:35:50 +01007984 *vptr = ptr_hist[hist_ptr];
7985 *vlen = len_hist[hist_ptr];
7986 return 1;
Willy Tarreaubce70882009-09-07 11:51:47 +02007987}
7988
Willy Tarreaubaaee002006-06-26 02:48:02 +02007989/*
Willy Tarreaue92693a2012-09-24 21:13:39 +02007990 * Print a debug line with a header. Always stop at the first CR or LF char,
7991 * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an
7992 * arrow is printed after the line which contains the pointer.
Willy Tarreau58f10d72006-12-04 02:26:12 +01007993 */
7994void debug_hdr(const char *dir, struct session *t, const char *start, const char *end)
7995{
Willy Tarreau19d14ef2012-10-29 16:51:55 +01007996 int max;
7997 chunk_printf(&trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id,
Willy Tarreau7f7ad912012-11-11 19:27:15 +01007998 dir, (unsigned short)t->req->prod->conn->t.sock.fd,
7999 (unsigned short)t->req->cons->conn->t.sock.fd);
Willy Tarreaue92693a2012-09-24 21:13:39 +02008000
8001 for (max = 0; start + max < end; max++)
8002 if (start[max] == '\r' || start[max] == '\n')
8003 break;
8004
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008005 UBOUND(max, trash.size - trash.len - 3);
8006 trash.len += strlcpy2(trash.str + trash.len, start, max + 1);
8007 trash.str[trash.len++] = '\n';
8008 if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */;
Willy Tarreau58f10d72006-12-04 02:26:12 +01008009}
8010
Willy Tarreau0937bc42009-12-22 15:03:09 +01008011/*
8012 * Initialize a new HTTP transaction for session <s>. It is assumed that all
8013 * the required fields are properly allocated and that we only need to (re)init
8014 * them. This should be used before processing any new request.
8015 */
8016void http_init_txn(struct session *s)
8017{
8018 struct http_txn *txn = &s->txn;
8019 struct proxy *fe = s->fe;
8020
8021 txn->flags = 0;
8022 txn->status = -1;
8023
William Lallemand5f232402012-04-05 18:02:55 +02008024 global.req_count++;
8025
Willy Tarreauf64d1412010-10-07 20:06:11 +02008026 txn->cookie_first_date = 0;
8027 txn->cookie_last_date = 0;
8028
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008029 txn->req.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008030 txn->req.sol = txn->req.eol = txn->req.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008031 txn->req.next = 0;
Willy Tarreaua36fc4d2012-02-17 17:39:37 +01008032 txn->rsp.flags = 0;
Willy Tarreau26927362012-05-18 23:22:52 +02008033 txn->rsp.sol = txn->rsp.eol = txn->rsp.eoh = 0; /* relative to the buffer */
Willy Tarreaua458b672012-03-05 11:17:50 +01008034 txn->rsp.next = 0;
Willy Tarreau124d9912011-03-01 20:30:48 +01008035 txn->req.chunk_len = 0LL;
8036 txn->req.body_len = 0LL;
8037 txn->rsp.chunk_len = 0LL;
8038 txn->rsp.body_len = 0LL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008039 txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */
8040 txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */
Willy Tarreau394db372012-10-12 22:40:39 +02008041 txn->req.chn = s->req;
8042 txn->rsp.chn = s->rep;
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008043
8044 txn->auth.method = HTTP_AUTH_UNKNOWN;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008045
8046 txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */
8047 if (fe->options2 & PR_O2_REQBUG_OK)
8048 txn->req.err_pos = -1; /* let buggy requests pass */
8049
Willy Tarreau46023632010-01-07 22:51:47 +01008050 if (txn->req.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008051 memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *));
8052
Willy Tarreau46023632010-01-07 22:51:47 +01008053 if (txn->rsp.cap)
Willy Tarreau0937bc42009-12-22 15:03:09 +01008054 memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *));
8055
8056 if (txn->hdr_idx.v)
8057 hdr_idx_init(&txn->hdr_idx);
8058}
8059
8060/* to be used at the end of a transaction */
8061void http_end_txn(struct session *s)
8062{
8063 struct http_txn *txn = &s->txn;
8064
8065 /* these ones will have been dynamically allocated */
8066 pool_free2(pool2_requri, txn->uri);
8067 pool_free2(pool2_capture, txn->cli_cookie);
8068 pool_free2(pool2_capture, txn->srv_cookie);
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008069 pool_free2(apools.sessid, txn->sessid);
William Lallemanda73203e2012-03-12 12:48:57 +01008070 pool_free2(pool2_uniqueid, s->unique_id);
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01008071
William Lallemanda73203e2012-03-12 12:48:57 +01008072 s->unique_id = NULL;
Willy Tarreaua3377ee2010-01-10 10:49:11 +01008073 txn->sessid = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008074 txn->uri = NULL;
8075 txn->srv_cookie = NULL;
8076 txn->cli_cookie = NULL;
Willy Tarreau46023632010-01-07 22:51:47 +01008077
8078 if (txn->req.cap) {
8079 struct cap_hdr *h;
8080 for (h = s->fe->req_cap; h; h = h->next)
8081 pool_free2(h->pool, txn->req.cap[h->index]);
8082 memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *));
8083 }
8084
8085 if (txn->rsp.cap) {
8086 struct cap_hdr *h;
8087 for (h = s->fe->rsp_cap; h; h = h->next)
8088 pool_free2(h->pool, txn->rsp.cap[h->index]);
8089 memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *));
8090 }
8091
Willy Tarreau0937bc42009-12-22 15:03:09 +01008092}
8093
8094/* to be used at the end of a transaction to prepare a new one */
8095void http_reset_txn(struct session *s)
8096{
8097 http_end_txn(s);
8098 http_init_txn(s);
8099
8100 s->be = s->fe;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008101 s->logs.logwait = s->fe->to_log;
Simon Hormanaf514952011-06-21 14:34:57 +09008102 session_del_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01008103 s->target = NULL;
Emeric Brunb982a3d2010-01-04 15:45:53 +01008104 /* re-init store persistence */
8105 s->store_count = 0;
8106
Willy Tarreau0937bc42009-12-22 15:03:09 +01008107 s->pend_pos = NULL;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008108
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02008109 s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */
Willy Tarreau0937bc42009-12-22 15:03:09 +01008110
Willy Tarreau739cfba2010-01-25 23:11:14 +01008111 /* We must trim any excess data from the response buffer, because we
8112 * may have blocked an invalid response from a server that we don't
8113 * want to accidentely forward once we disable the analysers, nor do
8114 * we want those data to come along with next response. A typical
8115 * example of such data would be from a buggy server responding to
8116 * a HEAD with some data, or sending more than the advertised
8117 * content-length.
8118 */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008119 if (unlikely(s->rep->buf->i))
8120 s->rep->buf->i = 0;
Willy Tarreau739cfba2010-01-25 23:11:14 +01008121
Willy Tarreau0937bc42009-12-22 15:03:09 +01008122 s->req->rto = s->fe->timeout.client;
Willy Tarreaud04e8582010-05-31 12:31:35 +02008123 s->req->wto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008124
Willy Tarreaud04e8582010-05-31 12:31:35 +02008125 s->rep->rto = TICK_ETERNITY;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008126 s->rep->wto = s->fe->timeout.client;
Willy Tarreau0937bc42009-12-22 15:03:09 +01008127
8128 s->req->rex = TICK_ETERNITY;
8129 s->req->wex = TICK_ETERNITY;
8130 s->req->analyse_exp = TICK_ETERNITY;
8131 s->rep->rex = TICK_ETERNITY;
8132 s->rep->wex = TICK_ETERNITY;
8133 s->rep->analyse_exp = TICK_ETERNITY;
8134}
Willy Tarreau58f10d72006-12-04 02:26:12 +01008135
Willy Tarreauff011f22011-01-06 17:51:27 +01008136void free_http_req_rules(struct list *r) {
8137 struct http_req_rule *tr, *pr;
8138
8139 list_for_each_entry_safe(pr, tr, r, list) {
8140 LIST_DEL(&pr->list);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008141 if (pr->action == HTTP_REQ_ACT_AUTH)
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008142 free(pr->arg.auth.realm);
Willy Tarreauff011f22011-01-06 17:51:27 +01008143
8144 free(pr);
8145 }
8146}
8147
8148struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy)
8149{
8150 struct http_req_rule *rule;
8151 int cur_arg;
8152
8153 rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule));
8154 if (!rule) {
8155 Alert("parsing [%s:%d]: out of memory.\n", file, linenum);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008156 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008157 }
8158
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008159 if (!strcmp(args[0], "allow")) {
Willy Tarreauff011f22011-01-06 17:51:27 +01008160 rule->action = HTTP_REQ_ACT_ALLOW;
8161 cur_arg = 1;
8162 } else if (!strcmp(args[0], "deny")) {
8163 rule->action = HTTP_REQ_ACT_DENY;
8164 cur_arg = 1;
Willy Tarreauccbcc372012-12-27 12:37:57 +01008165 } else if (!strcmp(args[0], "tarpit")) {
8166 rule->action = HTTP_REQ_ACT_TARPIT;
8167 cur_arg = 1;
Willy Tarreauff011f22011-01-06 17:51:27 +01008168 } else if (!strcmp(args[0], "auth")) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008169 rule->action = HTTP_REQ_ACT_AUTH;
Willy Tarreauff011f22011-01-06 17:51:27 +01008170 cur_arg = 1;
8171
8172 while(*args[cur_arg]) {
8173 if (!strcmp(args[cur_arg], "realm")) {
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008174 rule->arg.auth.realm = strdup(args[cur_arg + 1]);
Willy Tarreauff011f22011-01-06 17:51:27 +01008175 cur_arg+=2;
8176 continue;
8177 } else
8178 break;
8179 }
Willy Tarreau20b0de52012-12-24 15:45:22 +01008180 } else if (strcmp(args[0], "add-header") == 0 || strcmp(args[0], "set-header") == 0) {
8181 rule->action = *args[0] == 'a' ? HTTP_REQ_ACT_ADD_HDR : HTTP_REQ_ACT_SET_HDR;
8182 cur_arg = 1;
8183
Willy Tarreau8d1c5162013-04-03 14:13:58 +02008184 if (!*args[cur_arg] || !*args[cur_arg+1] ||
8185 (*args[cur_arg+2] && strcmp(args[cur_arg+2], "if") != 0 && strcmp(args[cur_arg+2], "unless") != 0)) {
Willy Tarreau20b0de52012-12-24 15:45:22 +01008186 Alert("parsing [%s:%d]: 'http-request %s' expects exactly 2 arguments.\n",
8187 file, linenum, args[0]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008188 goto out_err;
Willy Tarreau20b0de52012-12-24 15:45:22 +01008189 }
8190
8191 rule->arg.hdr_add.name = strdup(args[cur_arg]);
8192 rule->arg.hdr_add.name_len = strlen(rule->arg.hdr_add.name);
8193 LIST_INIT(&rule->arg.hdr_add.fmt);
Willy Tarreaua4312fa2013-04-02 16:34:32 +02008194
8195 proxy->conf.args.ctx = ARGC_HDR;
Willy Tarreau434c57c2013-01-08 01:10:24 +01008196 parse_logformat_string(args[cur_arg + 1], proxy, &rule->arg.hdr_add.fmt, 0,
8197 (proxy->cap & PR_CAP_FE) ? SMP_VAL_FE_HRQ_HDR : SMP_VAL_BE_HRQ_HDR);
Willy Tarreau20b0de52012-12-24 15:45:22 +01008198 cur_arg += 2;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008199 } else if (strcmp(args[0], "redirect") == 0) {
8200 struct redirect_rule *redir;
8201 char *errmsg;
8202
8203 if ((redir = http_parse_redirect_rule(file, linenum, proxy, (const char **)args + 1, &errmsg)) == NULL) {
8204 Alert("parsing [%s:%d] : error detected in %s '%s' while parsing 'http-request %s' rule : %s.\n",
8205 file, linenum, proxy_type_str(proxy), proxy->id, args[0], errmsg);
8206 goto out_err;
8207 }
8208
8209 /* this redirect rule might already contain a parsed condition which
8210 * we'll pass to the http-request rule.
8211 */
8212 rule->action = HTTP_REQ_ACT_REDIR;
8213 rule->arg.redir = redir;
8214 rule->cond = redir->cond;
8215 redir->cond = NULL;
8216 cur_arg = 2;
8217 return rule;
Willy Tarreauff011f22011-01-06 17:51:27 +01008218 } else {
Baptiste Assmann116eefe2013-01-05 16:02:07 +01008219 Alert("parsing [%s:%d]: 'http-request' expects 'allow', 'deny', 'auth', 'redirect', 'tarpit', 'add-header', 'set-header', but got '%s'%s.\n",
Willy Tarreau5c2e1982012-12-24 12:00:25 +01008220 file, linenum, args[0], *args[0] ? "" : " (missing argument)");
Willy Tarreau81499eb2012-12-27 12:19:02 +01008221 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008222 }
8223
8224 if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) {
8225 struct acl_cond *cond;
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008226 char *errmsg = NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008227
Willy Tarreaub7451bb2012-04-27 12:38:15 +02008228 if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) {
8229 Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n",
8230 file, linenum, args[0], errmsg);
8231 free(errmsg);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008232 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008233 }
8234 rule->cond = cond;
8235 }
8236 else if (*args[cur_arg]) {
8237 Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or"
8238 " either 'if' or 'unless' followed by a condition but found '%s'.\n",
8239 file, linenum, args[0], args[cur_arg]);
Willy Tarreau81499eb2012-12-27 12:19:02 +01008240 goto out_err;
Willy Tarreauff011f22011-01-06 17:51:27 +01008241 }
8242
8243 return rule;
Willy Tarreau81499eb2012-12-27 12:19:02 +01008244 out_err:
8245 free(rule);
8246 return NULL;
Willy Tarreauff011f22011-01-06 17:51:27 +01008247}
8248
Willy Tarreau4baae242012-12-27 12:00:31 +01008249/* Parses a redirect rule. Returns the redirect rule on success or NULL on error,
8250 * with <err> filled with the error message.
8251 */
8252struct redirect_rule *http_parse_redirect_rule(const char *file, int linenum, struct proxy *curproxy,
8253 const char **args, char **errmsg)
8254{
8255 struct redirect_rule *rule;
8256 int cur_arg;
8257 int type = REDIRECT_TYPE_NONE;
8258 int code = 302;
8259 const char *destination = NULL;
8260 const char *cookie = NULL;
8261 int cookie_set = 0;
8262 unsigned int flags = REDIRECT_FLAG_NONE;
8263 struct acl_cond *cond = NULL;
8264
8265 cur_arg = 0;
8266 while (*(args[cur_arg])) {
8267 if (strcmp(args[cur_arg], "location") == 0) {
8268 if (!*args[cur_arg + 1])
8269 goto missing_arg;
8270
8271 type = REDIRECT_TYPE_LOCATION;
8272 cur_arg++;
8273 destination = args[cur_arg];
8274 }
8275 else if (strcmp(args[cur_arg], "prefix") == 0) {
8276 if (!*args[cur_arg + 1])
8277 goto missing_arg;
8278
8279 type = REDIRECT_TYPE_PREFIX;
8280 cur_arg++;
8281 destination = args[cur_arg];
8282 }
8283 else if (strcmp(args[cur_arg], "scheme") == 0) {
8284 if (!*args[cur_arg + 1])
8285 goto missing_arg;
8286
8287 type = REDIRECT_TYPE_SCHEME;
8288 cur_arg++;
8289 destination = args[cur_arg];
8290 }
8291 else if (strcmp(args[cur_arg], "set-cookie") == 0) {
8292 if (!*args[cur_arg + 1])
8293 goto missing_arg;
8294
8295 cur_arg++;
8296 cookie = args[cur_arg];
8297 cookie_set = 1;
8298 }
8299 else if (strcmp(args[cur_arg], "clear-cookie") == 0) {
8300 if (!*args[cur_arg + 1])
8301 goto missing_arg;
8302
8303 cur_arg++;
8304 cookie = args[cur_arg];
8305 cookie_set = 0;
8306 }
8307 else if (strcmp(args[cur_arg], "code") == 0) {
8308 if (!*args[cur_arg + 1])
8309 goto missing_arg;
8310
8311 cur_arg++;
8312 code = atol(args[cur_arg]);
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008313 if (code < 301 || code > 308 || (code > 303 && code < 307)) {
Willy Tarreau4baae242012-12-27 12:00:31 +01008314 memprintf(errmsg,
Yves Lafon3e8d1ae2013-03-11 11:06:05 -04008315 "'%s': unsupported HTTP code '%s' (must be one of 301, 302, 303, 307 or 308)",
Willy Tarreau4baae242012-12-27 12:00:31 +01008316 args[cur_arg - 1], args[cur_arg]);
8317 return NULL;
8318 }
8319 }
8320 else if (!strcmp(args[cur_arg],"drop-query")) {
8321 flags |= REDIRECT_FLAG_DROP_QS;
8322 }
8323 else if (!strcmp(args[cur_arg],"append-slash")) {
8324 flags |= REDIRECT_FLAG_APPEND_SLASH;
8325 }
8326 else if (strcmp(args[cur_arg], "if") == 0 ||
8327 strcmp(args[cur_arg], "unless") == 0) {
8328 cond = build_acl_cond(file, linenum, curproxy, (const char **)args + cur_arg, errmsg);
8329 if (!cond) {
8330 memprintf(errmsg, "error in condition: %s", *errmsg);
8331 return NULL;
8332 }
8333 break;
8334 }
8335 else {
8336 memprintf(errmsg,
8337 "expects 'code', 'prefix', 'location', 'scheme', 'set-cookie', 'clear-cookie', 'drop-query' or 'append-slash' (was '%s')",
8338 args[cur_arg]);
8339 return NULL;
8340 }
8341 cur_arg++;
8342 }
8343
8344 if (type == REDIRECT_TYPE_NONE) {
8345 memprintf(errmsg, "redirection type expected ('prefix', 'location', or 'scheme')");
8346 return NULL;
8347 }
8348
8349 rule = (struct redirect_rule *)calloc(1, sizeof(*rule));
8350 rule->cond = cond;
8351 rule->rdr_str = strdup(destination);
8352 rule->rdr_len = strlen(destination);
8353 if (cookie) {
8354 /* depending on cookie_set, either we want to set the cookie, or to clear it.
8355 * a clear consists in appending "; path=/; Max-Age=0;" at the end.
8356 */
8357 rule->cookie_len = strlen(cookie);
8358 if (cookie_set) {
8359 rule->cookie_str = malloc(rule->cookie_len + 10);
8360 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8361 memcpy(rule->cookie_str + rule->cookie_len, "; path=/;", 10);
8362 rule->cookie_len += 9;
8363 } else {
8364 rule->cookie_str = malloc(rule->cookie_len + 21);
8365 memcpy(rule->cookie_str, cookie, rule->cookie_len);
8366 memcpy(rule->cookie_str + rule->cookie_len, "; path=/; Max-Age=0;", 21);
8367 rule->cookie_len += 20;
8368 }
8369 }
8370 rule->type = type;
8371 rule->code = code;
8372 rule->flags = flags;
8373 LIST_INIT(&rule->list);
8374 return rule;
8375
8376 missing_arg:
8377 memprintf(errmsg, "missing argument for '%s'", args[cur_arg]);
8378 return NULL;
8379}
8380
Willy Tarreau8797c062007-05-07 00:55:35 +02008381/************************************************************************/
8382/* The code below is dedicated to ACL parsing and matching */
8383/************************************************************************/
8384
8385
Willy Tarreau14174bc2012-04-16 14:34:04 +02008386/* This function ensures that the prerequisites for an L7 fetch are ready,
8387 * which means that a request or response is ready. If some data is missing,
8388 * a parsing attempt is made. This is useful in TCP-based ACLs which are able
Willy Tarreau24e32d82012-04-23 23:55:44 +02008389 * to extract data from L7. If <req_vol> is non-null during a request prefetch,
8390 * another test is made to ensure the required information is not gone.
Willy Tarreau14174bc2012-04-16 14:34:04 +02008391 *
8392 * The function returns :
8393 * 0 if some data is missing or if the requested data cannot be fetched
8394 * -1 if it is certain that we'll never have any HTTP message there
8395 * 1 if an HTTP message is ready
8396 */
8397static int
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008398acl_prefetch_http(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008399 const struct arg *args, struct sample *smp, int req_vol)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008400{
8401 struct http_txn *txn = l7;
8402 struct http_msg *msg = &txn->req;
8403
8404 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
8405 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
8406 */
8407
8408 if (unlikely(!s || !txn))
8409 return 0;
8410
8411 /* Check for a dependency on a request */
Willy Tarreauf853c462012-04-23 18:53:56 +02008412 smp->type = SMP_T_BOOL;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008413
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008414 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreau14174bc2012-04-16 14:34:04 +02008415 if (unlikely(!s->req))
8416 return 0;
8417
Willy Tarreauaae75e32013-03-29 12:31:49 +01008418 /* If the buffer does not leave enough free space at the end,
8419 * we must first realign it.
8420 */
8421 if (s->req->buf->p > s->req->buf->data &&
8422 s->req->buf->i + s->req->buf->p > s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)
8423 buffer_slow_realign(s->req->buf);
8424
Willy Tarreau14174bc2012-04-16 14:34:04 +02008425 if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008426 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008427 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008428 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008429 return -1;
8430 }
8431
8432 /* Try to decode HTTP request */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008433 if (likely(msg->next < s->req->buf->i))
Willy Tarreau14174bc2012-04-16 14:34:04 +02008434 http_msg_analyzer(msg, &txn->hdr_idx);
8435
8436 /* Still no valid request ? */
8437 if (unlikely(msg->msg_state < HTTP_MSG_BODY)) {
Willy Tarreau3bf1b2b2012-08-27 20:46:07 +02008438 if ((msg->msg_state == HTTP_MSG_ERROR) ||
Willy Tarreau9b28e032012-10-12 23:49:43 +02008439 buffer_full(s->req->buf, global.tune.maxrewrite)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008440 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008441 return -1;
8442 }
8443 /* wait for final state */
Willy Tarreau37406352012-04-23 16:16:37 +02008444 smp->flags |= SMP_F_MAY_CHANGE;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008445 return 0;
8446 }
8447
8448 /* OK we just got a valid HTTP request. We have some minor
8449 * preparation to perform so that further checks can rely
8450 * on HTTP tests.
8451 */
Willy Tarreauaae75e32013-03-29 12:31:49 +01008452
8453 /* If the request was parsed but was too large, we must absolutely
8454 * return an error so that it is not processed. At the moment this
8455 * cannot happen, but if the parsers are to change in the future,
8456 * we want this check to be maintained.
8457 */
8458 if (unlikely(s->req->buf->i + s->req->buf->p >
8459 s->req->buf->data + s->req->buf->size - global.tune.maxrewrite)) {
8460 msg->msg_state = HTTP_MSG_ERROR;
8461 return 1;
8462 }
8463
Willy Tarreau9b28e032012-10-12 23:49:43 +02008464 txn->meth = find_http_meth(msg->chn->buf->p, msg->sl.rq.m_l);
Willy Tarreau14174bc2012-04-16 14:34:04 +02008465 if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)
8466 s->flags |= SN_REDIRECTABLE;
8467
8468 if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(txn)) {
Willy Tarreau197e10a2012-04-23 19:18:42 +02008469 smp->data.uint = 0;
Willy Tarreau14174bc2012-04-16 14:34:04 +02008470 return -1;
8471 }
8472 }
8473
Willy Tarreau24e32d82012-04-23 23:55:44 +02008474 if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
Willy Tarreau14174bc2012-04-16 14:34:04 +02008475 return 0; /* data might have moved and indexes changed */
8476
8477 /* otherwise everything's ready for the request */
8478 }
Willy Tarreau24e32d82012-04-23 23:55:44 +02008479 else {
8480 /* Check for a dependency on a response */
Willy Tarreau14174bc2012-04-16 14:34:04 +02008481 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8482 return 0;
8483 }
8484
8485 /* everything's OK */
8486 return 1;
8487}
Willy Tarreau8797c062007-05-07 00:55:35 +02008488
Willy Tarreauc0239e02012-04-16 14:42:55 +02008489#define CHECK_HTTP_MESSAGE_FIRST() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008490 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 +02008491
Willy Tarreau24e32d82012-04-23 23:55:44 +02008492#define CHECK_HTTP_MESSAGE_FIRST_PERM() \
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008493 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 +02008494
Willy Tarreau8797c062007-05-07 00:55:35 +02008495
8496/* 1. Check on METHOD
8497 * We use the pre-parsed method if it is known, and store its number as an
8498 * integer. If it is unknown, we use the pointer and the length.
8499 */
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008500static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque, char **err)
Willy Tarreau8797c062007-05-07 00:55:35 +02008501{
8502 int len, meth;
8503
Willy Tarreauae8b7962007-06-09 23:10:04 +02008504 len = strlen(*text);
8505 meth = find_http_meth(*text, len);
Willy Tarreau8797c062007-05-07 00:55:35 +02008506
8507 pattern->val.i = meth;
8508 if (meth == HTTP_METH_OTHER) {
Willy Tarreauae8b7962007-06-09 23:10:04 +02008509 pattern->ptr.str = strdup(*text);
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008510 if (!pattern->ptr.str) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02008511 memprintf(err, "out of memory while loading pattern");
Willy Tarreau8797c062007-05-07 00:55:35 +02008512 return 0;
Willy Tarreau7dcb6482012-04-27 17:52:25 +02008513 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008514 pattern->len = len;
8515 }
8516 return 1;
8517}
8518
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008519/* This function fetches the method of current HTTP request and stores
8520 * it in the global pattern struct as a chunk. There are two possibilities :
8521 * - if the method is known (not HTTP_METH_OTHER), its identifier is stored
8522 * in <len> and <ptr> is NULL ;
8523 * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and
8524 * <len> to its length.
8525 * This is intended to be used with acl_match_meth() only.
8526 */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008527static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008528smp_fetch_meth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008529 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008530{
8531 int meth;
8532 struct http_txn *txn = l7;
8533
Willy Tarreau24e32d82012-04-23 23:55:44 +02008534 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008535
Willy Tarreau8797c062007-05-07 00:55:35 +02008536 meth = txn->meth;
Willy Tarreauf853c462012-04-23 18:53:56 +02008537 smp->type = SMP_T_UINT;
8538 smp->data.uint = meth;
Willy Tarreau8797c062007-05-07 00:55:35 +02008539 if (meth == HTTP_METH_OTHER) {
Willy Tarreauc11416f2007-06-17 16:58:38 +02008540 if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE)
8541 /* ensure the indexes are not affected */
8542 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008543 smp->type = SMP_T_CSTR;
8544 smp->data.str.len = txn->req.sl.rq.m_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008545 smp->data.str.str = txn->req.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008546 }
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008547 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008548 return 1;
8549}
8550
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008551/* See above how the method is stored in the global pattern */
Willy Tarreau37406352012-04-23 16:16:37 +02008552static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern)
Willy Tarreau8797c062007-05-07 00:55:35 +02008553{
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008554 int icase;
8555
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008556
Willy Tarreauf853c462012-04-23 18:53:56 +02008557 if (smp->type == SMP_T_UINT) {
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008558 /* well-known method */
Willy Tarreauf853c462012-04-23 18:53:56 +02008559 if (smp->data.uint == pattern->val.i)
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008560 return ACL_PAT_PASS;
Willy Tarreau11382812008-07-09 16:18:21 +02008561 return ACL_PAT_FAIL;
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008562 }
Willy Tarreau8797c062007-05-07 00:55:35 +02008563
Willy Tarreau8e5e9552011-12-16 15:38:49 +01008564 /* Uncommon method, only HTTP_METH_OTHER is accepted now */
8565 if (pattern->val.i != HTTP_METH_OTHER)
8566 return ACL_PAT_FAIL;
Willy Tarreau8797c062007-05-07 00:55:35 +02008567
8568 /* Other method, we must compare the strings */
Willy Tarreauf853c462012-04-23 18:53:56 +02008569 if (pattern->len != smp->data.str.len)
Willy Tarreau11382812008-07-09 16:18:21 +02008570 return ACL_PAT_FAIL;
Willy Tarreauc8d7c962007-06-17 08:20:33 +02008571
8572 icase = pattern->flags & ACL_PAT_F_IGNORE_CASE;
Willy Tarreauf853c462012-04-23 18:53:56 +02008573 if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) ||
8574 (!icase && strncmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0))
Willy Tarreau11382812008-07-09 16:18:21 +02008575 return ACL_PAT_FAIL;
8576 return ACL_PAT_PASS;
Willy Tarreau8797c062007-05-07 00:55:35 +02008577}
8578
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008579static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008580smp_fetch_rqver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008581 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008582{
8583 struct http_txn *txn = l7;
8584 char *ptr;
8585 int len;
8586
Willy Tarreauc0239e02012-04-16 14:42:55 +02008587 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008588
Willy Tarreau8797c062007-05-07 00:55:35 +02008589 len = txn->req.sl.rq.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008590 ptr = txn->req.chn->buf->p + txn->req.sl.rq.v;
Willy Tarreau8797c062007-05-07 00:55:35 +02008591
8592 while ((len-- > 0) && (*ptr++ != '/'));
8593 if (len <= 0)
8594 return 0;
8595
Willy Tarreauf853c462012-04-23 18:53:56 +02008596 smp->type = SMP_T_CSTR;
8597 smp->data.str.str = ptr;
8598 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008599
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008600 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008601 return 1;
8602}
8603
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008604static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008605smp_fetch_stver(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008606 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008607{
8608 struct http_txn *txn = l7;
8609 char *ptr;
8610 int len;
8611
Willy Tarreauc0239e02012-04-16 14:42:55 +02008612 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008613
Willy Tarreauf26b2522012-12-14 08:33:14 +01008614 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8615 return 0;
8616
Willy Tarreau8797c062007-05-07 00:55:35 +02008617 len = txn->rsp.sl.st.v_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008618 ptr = txn->rsp.chn->buf->p;
Willy Tarreau8797c062007-05-07 00:55:35 +02008619
8620 while ((len-- > 0) && (*ptr++ != '/'));
8621 if (len <= 0)
8622 return 0;
8623
Willy Tarreauf853c462012-04-23 18:53:56 +02008624 smp->type = SMP_T_CSTR;
8625 smp->data.str.str = ptr;
8626 smp->data.str.len = len;
Willy Tarreau8797c062007-05-07 00:55:35 +02008627
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008628 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008629 return 1;
8630}
8631
8632/* 3. Check on Status Code. We manipulate integers here. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008633static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01008634smp_fetch_stcode(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008635 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008636{
8637 struct http_txn *txn = l7;
8638 char *ptr;
8639 int len;
8640
Willy Tarreauc0239e02012-04-16 14:42:55 +02008641 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008642
Willy Tarreauf26b2522012-12-14 08:33:14 +01008643 if (txn->rsp.msg_state < HTTP_MSG_BODY)
8644 return 0;
8645
Willy Tarreau8797c062007-05-07 00:55:35 +02008646 len = txn->rsp.sl.st.c_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008647 ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c;
Willy Tarreau8797c062007-05-07 00:55:35 +02008648
Willy Tarreauf853c462012-04-23 18:53:56 +02008649 smp->type = SMP_T_UINT;
8650 smp->data.uint = __strl2ui(ptr, len);
Willy Tarreau37406352012-04-23 16:16:37 +02008651 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008652 return 1;
8653}
8654
8655/* 4. Check on URL/URI. A pointer to the URI is stored. */
Willy Tarreaud41f8d82007-06-10 10:06:18 +02008656static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008657smp_fetch_url(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008658 const struct arg *args, struct sample *smp)
Willy Tarreau8797c062007-05-07 00:55:35 +02008659{
8660 struct http_txn *txn = l7;
8661
Willy Tarreauc0239e02012-04-16 14:42:55 +02008662 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008663
Willy Tarreauf853c462012-04-23 18:53:56 +02008664 smp->type = SMP_T_CSTR;
8665 smp->data.str.len = txn->req.sl.rq.u_l;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008666 smp->data.str.str = txn->req.chn->buf->p + txn->req.sl.rq.u;
Willy Tarreau37406352012-04-23 16:16:37 +02008667 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau8797c062007-05-07 00:55:35 +02008668 return 1;
8669}
8670
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008671static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008672smp_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008673 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008674{
8675 struct http_txn *txn = l7;
8676
Willy Tarreauc0239e02012-04-16 14:42:55 +02008677 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008678
8679 /* Parse HTTP request */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008680 url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to);
8681 if (((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_family != AF_INET)
Willy Tarreauf4362b32011-12-16 17:49:52 +01008682 return 0;
Willy Tarreauf853c462012-04-23 18:53:56 +02008683 smp->type = SMP_T_IPV4;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008684 smp->data.ipv4 = ((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_addr;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008685
8686 /*
8687 * If we are parsing url in frontend space, we prepare backend stage
8688 * to not parse again the same url ! optimization lazyness...
8689 */
8690 if (px->options & PR_O_HTTP_PROXY)
8691 l4->flags |= SN_ADDR_SET;
8692
Willy Tarreau37406352012-04-23 16:16:37 +02008693 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008694 return 1;
8695}
8696
8697static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008698smp_fetch_url_port(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008699 const struct arg *args, struct sample *smp)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008700{
8701 struct http_txn *txn = l7;
8702
Willy Tarreauc0239e02012-04-16 14:42:55 +02008703 CHECK_HTTP_MESSAGE_FIRST();
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008704
8705 /* Same optimization as url_ip */
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008706 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 +02008707 smp->type = SMP_T_UINT;
Willy Tarreauf2943dc2012-10-26 20:10:28 +02008708 smp->data.uint = ntohs(((struct sockaddr_in *)&l4->req->cons->conn->addr.to)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008709
8710 if (px->options & PR_O_HTTP_PROXY)
8711 l4->flags |= SN_ADDR_SET;
8712
Willy Tarreau21e5b0e2012-04-23 19:25:44 +02008713 smp->flags = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01008714 return 1;
8715}
8716
Willy Tarreau185b5c42012-04-26 15:11:51 +02008717/* Fetch an HTTP header. A pointer to the beginning of the value is returned.
8718 * Accepts an optional argument of type string containing the header field name,
8719 * and an optional argument of type signed or unsigned integer to request an
8720 * explicit occurrence of the header. Note that in the event of a missing name,
8721 * headers are considered from the first one.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008722 */
Willy Tarreau33a7e692007-06-10 19:45:56 +02008723static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008724smp_fetch_hdr(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008725 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008726{
8727 struct http_txn *txn = l7;
8728 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02008729 struct hdr_ctx *ctx = smp->ctx.a[0];
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008730 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau185b5c42012-04-26 15:11:51 +02008731 int occ = 0;
8732 const char *name_str = NULL;
8733 int name_len = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008734
Willy Tarreaua890d072013-04-02 12:01:06 +02008735 if (!ctx) {
8736 /* first call */
8737 ctx = &static_hdr_ctx;
8738 ctx->idx = 0;
Willy Tarreauffb6f082013-04-02 23:16:53 +02008739 smp->ctx.a[0] = ctx;
Willy Tarreaua890d072013-04-02 12:01:06 +02008740 }
8741
Willy Tarreau185b5c42012-04-26 15:11:51 +02008742 if (args) {
8743 if (args[0].type != ARGT_STR)
8744 return 0;
8745 name_str = args[0].data.str.str;
8746 name_len = args[0].data.str.len;
8747
8748 if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT)
8749 occ = args[1].data.uint;
8750 }
Willy Tarreau34db1082012-04-19 17:16:54 +02008751
Willy Tarreaue333ec92012-04-16 16:26:40 +02008752 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau33a7e692007-06-10 19:45:56 +02008753
Willy Tarreau185b5c42012-04-26 15:11:51 +02008754 if (ctx && !(smp->flags & SMP_F_NOT_LAST))
Willy Tarreau33a7e692007-06-10 19:45:56 +02008755 /* search for header from the beginning */
8756 ctx->idx = 0;
8757
Willy Tarreau185b5c42012-04-26 15:11:51 +02008758 if (!occ && !(opt & SMP_OPT_ITERATE))
8759 /* no explicit occurrence and single fetch => last header by default */
8760 occ = -1;
8761
8762 if (!occ)
8763 /* prepare to report multiple occurrences for ACL fetches */
Willy Tarreau37406352012-04-23 16:16:37 +02008764 smp->flags |= SMP_F_NOT_LAST;
Willy Tarreau664092c2011-12-16 19:11:42 +01008765
Willy Tarreau185b5c42012-04-26 15:11:51 +02008766 smp->type = SMP_T_CSTR;
8767 smp->flags |= SMP_F_VOL_HDR;
8768 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 +02008769 return 1;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008770
Willy Tarreau37406352012-04-23 16:16:37 +02008771 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008772 return 0;
8773}
8774
Willy Tarreauc11416f2007-06-17 16:58:38 +02008775/* 6. Check on HTTP header count. The number of occurrences is returned.
Willy Tarreau34db1082012-04-19 17:16:54 +02008776 * Accepts exactly 1 argument of type string.
Willy Tarreauc11416f2007-06-17 16:58:38 +02008777 */
8778static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008779smp_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008780 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008781{
8782 struct http_txn *txn = l7;
8783 struct hdr_idx *idx = &txn->hdr_idx;
8784 struct hdr_ctx ctx;
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02008785 const struct http_msg *msg = ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) ? &txn->req : &txn->rsp;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008786 int cnt;
Willy Tarreau8797c062007-05-07 00:55:35 +02008787
Willy Tarreau24e32d82012-04-23 23:55:44 +02008788 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02008789 return 0;
8790
Willy Tarreaue333ec92012-04-16 16:26:40 +02008791 CHECK_HTTP_MESSAGE_FIRST();
8792
Willy Tarreau33a7e692007-06-10 19:45:56 +02008793 ctx.idx = 0;
8794 cnt = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008795 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 +02008796 cnt++;
8797
Willy Tarreauf853c462012-04-23 18:53:56 +02008798 smp->type = SMP_T_UINT;
8799 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02008800 smp->flags = SMP_F_VOL_HDR;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008801 return 1;
8802}
8803
Willy Tarreau185b5c42012-04-26 15:11:51 +02008804/* Fetch an HTTP header's integer value. The integer value is returned. It
8805 * takes a mandatory argument of type string and an optional one of type int
8806 * to designate a specific occurrence. It returns an unsigned integer, which
8807 * may or may not be appropriate for everything.
Willy Tarreau33a7e692007-06-10 19:45:56 +02008808 */
8809static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008810smp_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008811 const struct arg *args, struct sample *smp)
Willy Tarreau33a7e692007-06-10 19:45:56 +02008812{
Willy Tarreau185b5c42012-04-26 15:11:51 +02008813 int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp);
Willy Tarreaue333ec92012-04-16 16:26:40 +02008814
Willy Tarreauf853c462012-04-23 18:53:56 +02008815 if (ret > 0) {
8816 smp->type = SMP_T_UINT;
8817 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
8818 }
Willy Tarreau33a7e692007-06-10 19:45:56 +02008819
Willy Tarreaud53e2422012-04-16 17:21:11 +02008820 return ret;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008821}
8822
Cyril Bonté69fa9922012-10-25 00:01:06 +02008823/* Fetch an HTTP header's IP value. takes a mandatory argument of type string
8824 * and an optional one of type int to designate a specific occurrence.
8825 * It returns an IPv4 or IPv6 address.
Willy Tarreau106f9792009-09-19 07:54:16 +02008826 */
8827static int
Willy Tarreau185b5c42012-04-26 15:11:51 +02008828smp_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008829 const struct arg *args, struct sample *smp)
Willy Tarreau106f9792009-09-19 07:54:16 +02008830{
Willy Tarreaud53e2422012-04-16 17:21:11 +02008831 int ret;
Willy Tarreaue333ec92012-04-16 16:26:40 +02008832
Willy Tarreau185b5c42012-04-26 15:11:51 +02008833 while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) {
Cyril Bonté69fa9922012-10-25 00:01:06 +02008834 if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) {
8835 smp->type = SMP_T_IPV4;
Willy Tarreaud53e2422012-04-16 17:21:11 +02008836 break;
Cyril Bonté69fa9922012-10-25 00:01:06 +02008837 } else {
Willy Tarreau47ca5452012-12-23 20:22:19 +01008838 struct chunk *temp = get_trash_chunk();
Cyril Bonté69fa9922012-10-25 00:01:06 +02008839 if (smp->data.str.len < temp->size - 1) {
8840 memcpy(temp->str, smp->data.str.str, smp->data.str.len);
8841 temp->str[smp->data.str.len] = '\0';
8842 if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) {
8843 smp->type = SMP_T_IPV6;
8844 break;
8845 }
8846 }
8847 }
8848
Willy Tarreaud53e2422012-04-16 17:21:11 +02008849 /* if the header doesn't match an IP address, fetch next one */
Willy Tarreau185b5c42012-04-26 15:11:51 +02008850 if (!(smp->flags & SMP_F_NOT_LAST))
8851 return 0;
Willy Tarreau106f9792009-09-19 07:54:16 +02008852 }
Willy Tarreaud53e2422012-04-16 17:21:11 +02008853 return ret;
Willy Tarreau106f9792009-09-19 07:54:16 +02008854}
8855
Willy Tarreau737b0c12007-06-10 21:28:46 +02008856/* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at
8857 * the first '/' after the possible hostname, and ends before the possible '?'.
8858 */
8859static int
Willy Tarreau6812bcf2012-04-29 09:28:50 +02008860smp_fetch_path(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02008861 const struct arg *args, struct sample *smp)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008862{
8863 struct http_txn *txn = l7;
8864 char *ptr, *end;
Willy Tarreau33a7e692007-06-10 19:45:56 +02008865
Willy Tarreauc0239e02012-04-16 14:42:55 +02008866 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreauc11416f2007-06-17 16:58:38 +02008867
Willy Tarreau9b28e032012-10-12 23:49:43 +02008868 end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
Willy Tarreau21d2af32008-02-14 20:25:24 +01008869 ptr = http_get_path(txn);
8870 if (!ptr)
Willy Tarreau737b0c12007-06-10 21:28:46 +02008871 return 0;
8872
8873 /* OK, we got the '/' ! */
Willy Tarreauf853c462012-04-23 18:53:56 +02008874 smp->type = SMP_T_CSTR;
8875 smp->data.str.str = ptr;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008876
8877 while (ptr < end && *ptr != '?')
8878 ptr++;
8879
Willy Tarreauf853c462012-04-23 18:53:56 +02008880 smp->data.str.len = ptr - smp->data.str.str;
Willy Tarreau37406352012-04-23 16:16:37 +02008881 smp->flags = SMP_F_VOL_1ST;
Willy Tarreau737b0c12007-06-10 21:28:46 +02008882 return 1;
8883}
8884
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008885/* This produces a concatenation of the first occurrence of the Host header
8886 * followed by the path component if it begins with a slash ('/'). This means
8887 * that '*' will not be added, resulting in exactly the first Host entry.
8888 * If no Host header is found, then the path is returned as-is. The returned
8889 * value is stored in the trash so it does not need to be marked constant.
8890 */
8891static int
8892smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8893 const struct arg *args, struct sample *smp)
8894{
8895 struct http_txn *txn = l7;
8896 char *ptr, *end, *beg;
8897 struct hdr_ctx ctx;
8898
8899 CHECK_HTTP_MESSAGE_FIRST();
8900
8901 ctx.idx = 0;
Willy Tarreau9b28e032012-10-12 23:49:43 +02008902 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 +02008903 !ctx.vlen)
8904 return smp_fetch_path(px, l4, l7, opt, args, smp);
8905
8906 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008907 memcpy(trash.str, ctx.line + ctx.val, ctx.vlen);
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008908 smp->type = SMP_T_STR;
Willy Tarreau19d14ef2012-10-29 16:51:55 +01008909 smp->data.str.str = trash.str;
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02008910 smp->data.str.len = ctx.vlen;
8911
8912 /* now retrieve the path */
Willy Tarreau9b28e032012-10-12 23:49:43 +02008913 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 +02008914 beg = http_get_path(txn);
8915 if (!beg)
8916 beg = end;
8917
8918 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8919
8920 if (beg < ptr && *beg == '/') {
8921 memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg);
8922 smp->data.str.len += ptr - beg;
8923 }
8924
8925 smp->flags = SMP_F_VOL_1ST;
8926 return 1;
8927}
8928
Willy Tarreauab1f7b72012-12-09 13:38:54 +01008929/* This produces a 32-bit hash of the concatenation of the first occurrence of
8930 * the Host header followed by the path component if it begins with a slash ('/').
8931 * This means that '*' will not be added, resulting in exactly the first Host
8932 * entry. If no Host header is found, then the path is used. The resulting value
8933 * is hashed using the url hash followed by a full avalanche hash and provides a
8934 * 32-bit integer value. This fetch is useful for tracking per-URL activity on
8935 * high-traffic sites without having to store whole paths.
8936 */
8937static int
8938smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8939 const struct arg *args, struct sample *smp)
8940{
8941 struct http_txn *txn = l7;
8942 struct hdr_ctx ctx;
8943 unsigned int hash = 0;
8944 char *ptr, *beg, *end;
8945 int len;
8946
8947 CHECK_HTTP_MESSAGE_FIRST();
8948
8949 ctx.idx = 0;
8950 if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) {
8951 /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */
8952 ptr = ctx.line + ctx.val;
8953 len = ctx.vlen;
8954 while (len--)
8955 hash = *(ptr++) + (hash << 6) + (hash << 16) - hash;
8956 }
8957
8958 /* now retrieve the path */
8959 end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l;
8960 beg = http_get_path(txn);
8961 if (!beg)
8962 beg = end;
8963
8964 for (ptr = beg; ptr < end && *ptr != '?'; ptr++);
8965
8966 if (beg < ptr && *beg == '/') {
8967 while (beg < ptr)
8968 hash = *(beg++) + (hash << 6) + (hash << 16) - hash;
8969 }
8970 hash = full_hash(hash);
8971
8972 smp->type = SMP_T_UINT;
8973 smp->data.uint = hash;
8974 smp->flags = SMP_F_VOL_1ST;
8975 return 1;
8976}
8977
Willy Tarreau4a550602012-12-09 14:53:32 +01008978/* This concatenates the source address with the 32-bit hash of the Host and
8979 * URL as returned by smp_fetch_base32(). The idea is to have per-source and
8980 * per-url counters. The result is a binary block from 8 to 20 bytes depending
8981 * on the source address length. The URL hash is stored before the address so
8982 * that in environments where IPv6 is insignificant, truncating the output to
8983 * 8 bytes would still work.
8984 */
8985static int
8986smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
8987 const struct arg *args, struct sample *smp)
8988{
Willy Tarreau47ca5452012-12-23 20:22:19 +01008989 struct chunk *temp;
Willy Tarreau4a550602012-12-09 14:53:32 +01008990
8991 if (!smp_fetch_base32(px, l4, l7, opt, args, smp))
8992 return 0;
8993
Willy Tarreau47ca5452012-12-23 20:22:19 +01008994 temp = get_trash_chunk();
Willy Tarreau4a550602012-12-09 14:53:32 +01008995 memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint));
8996 temp->len += sizeof(smp->data.uint);
8997
8998 switch (l4->si[0].conn->addr.from.ss_family) {
8999 case AF_INET:
9000 memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4);
9001 temp->len += 4;
9002 break;
9003 case AF_INET6:
9004 memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16);
9005 temp->len += 16;
9006 break;
9007 default:
9008 return 0;
9009 }
9010
9011 smp->data.str = *temp;
9012 smp->type = SMP_T_BIN;
9013 return 1;
9014}
9015
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009016static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009017smp_fetch_proto_http(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009018 const struct arg *args, struct sample *smp)
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009019{
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009020 /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged
9021 * as a layer7 ACL, which involves automatic allocation of hdr_idx.
9022 */
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009023
Willy Tarreau24e32d82012-04-23 23:55:44 +02009024 CHECK_HTTP_MESSAGE_FIRST_PERM();
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009025
Willy Tarreauf853c462012-04-23 18:53:56 +02009026 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009027 smp->data.uint = 1;
Willy Tarreau2492d5b2009-07-11 00:06:00 +02009028 return 1;
9029}
9030
Willy Tarreau7f18e522010-10-22 20:04:13 +02009031/* return a valid test if the current request is the first one on the connection */
9032static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009033smp_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009034 const struct arg *args, struct sample *smp)
Willy Tarreau7f18e522010-10-22 20:04:13 +02009035{
9036 if (!s)
9037 return 0;
9038
Willy Tarreauf853c462012-04-23 18:53:56 +02009039 smp->type = SMP_T_BOOL;
Willy Tarreau197e10a2012-04-23 19:18:42 +02009040 smp->data.uint = !(s->txn.flags & TX_NOT_FIRST);
Willy Tarreau7f18e522010-10-22 20:04:13 +02009041 return 1;
9042}
9043
Willy Tarreau34db1082012-04-19 17:16:54 +02009044/* Accepts exactly 1 argument of type userlist */
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009045static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009046smp_fetch_http_auth(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009047 const struct arg *args, struct sample *smp)
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009048{
9049
Willy Tarreau24e32d82012-04-23 23:55:44 +02009050 if (!args || args->type != ARGT_USR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009051 return 0;
9052
Willy Tarreauc0239e02012-04-16 14:42:55 +02009053 CHECK_HTTP_MESSAGE_FIRST();
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009054
Willy Tarreauc0239e02012-04-16 14:42:55 +02009055 if (!get_http_auth(l4))
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01009056 return 0;
9057
Willy Tarreauf853c462012-04-23 18:53:56 +02009058 smp->type = SMP_T_BOOL;
Willy Tarreau24e32d82012-04-23 23:55:44 +02009059 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 +01009060 return 1;
9061}
Willy Tarreau8797c062007-05-07 00:55:35 +02009062
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009063/* Accepts exactly 1 argument of type userlist */
9064static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009065smp_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau4a3fd4c2012-05-10 23:18:26 +02009066 const struct arg *args, struct sample *smp)
9067{
9068
9069 if (!args || args->type != ARGT_USR)
9070 return 0;
9071
9072 CHECK_HTTP_MESSAGE_FIRST();
9073
9074 if (!get_http_auth(l4))
9075 return 0;
9076
9077 /* acl_match_auth() will need several information at once */
9078 smp->ctx.a[0] = args->data.usr; /* user list */
9079 smp->ctx.a[1] = l4->txn.auth.user; /* user name */
9080 smp->ctx.a[2] = l4->txn.auth.pass; /* password */
9081
9082 /* if the user does not belong to the userlist or has a wrong password,
9083 * report that it unconditionally does not match. Otherwise we return
9084 * a non-zero integer which will be ignored anyway since all the params
9085 * that acl_match_auth() will use are in test->ctx.a[0,1,2].
9086 */
9087 smp->type = SMP_T_BOOL;
9088 smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass);
9089 if (smp->data.uint)
9090 smp->type = SMP_T_UINT;
9091
9092 return 1;
9093}
9094
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009095/* Try to find the next occurrence of a cookie name in a cookie header value.
9096 * The lookup begins at <hdr>. The pointer and size of the next occurrence of
9097 * the cookie value is returned into *value and *value_l, and the function
9098 * returns a pointer to the next pointer to search from if the value was found.
9099 * Otherwise if the cookie was not found, NULL is returned and neither value
9100 * nor value_l are touched. The input <hdr> string should first point to the
9101 * header's value, and the <hdr_end> pointer must point to the first character
9102 * not part of the value. <list> must be non-zero if value may represent a list
9103 * of values (cookie headers). This makes it faster to abort parsing when no
9104 * list is expected.
9105 */
9106static char *
9107extract_cookie_value(char *hdr, const char *hdr_end,
9108 char *cookie_name, size_t cookie_name_l, int list,
Willy Tarreau3fb818c2012-04-11 17:21:08 +02009109 char **value, int *value_l)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009110{
9111 char *equal, *att_end, *att_beg, *val_beg, *val_end;
9112 char *next;
9113
9114 /* we search at least a cookie name followed by an equal, and more
9115 * generally something like this :
9116 * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n
9117 */
9118 for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) {
9119 /* Iterate through all cookies on this line */
9120
9121 while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg])
9122 att_beg++;
9123
9124 /* find att_end : this is the first character after the last non
9125 * space before the equal. It may be equal to hdr_end.
9126 */
9127 equal = att_end = att_beg;
9128
9129 while (equal < hdr_end) {
9130 if (*equal == '=' || *equal == ';' || (list && *equal == ','))
9131 break;
9132 if (http_is_spht[(unsigned char)*equal++])
9133 continue;
9134 att_end = equal;
9135 }
9136
9137 /* here, <equal> points to '=', a delimitor or the end. <att_end>
9138 * is between <att_beg> and <equal>, both may be identical.
9139 */
9140
9141 /* look for end of cookie if there is an equal sign */
9142 if (equal < hdr_end && *equal == '=') {
9143 /* look for the beginning of the value */
9144 val_beg = equal + 1;
9145 while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg])
9146 val_beg++;
9147
9148 /* find the end of the value, respecting quotes */
9149 next = find_cookie_value_end(val_beg, hdr_end);
9150
9151 /* make val_end point to the first white space or delimitor after the value */
9152 val_end = next;
9153 while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)])
9154 val_end--;
9155 } else {
9156 val_beg = val_end = next = equal;
9157 }
9158
9159 /* We have nothing to do with attributes beginning with '$'. However,
9160 * they will automatically be removed if a header before them is removed,
9161 * since they're supposed to be linked together.
9162 */
9163 if (*att_beg == '$')
9164 continue;
9165
9166 /* Ignore cookies with no equal sign */
9167 if (equal == next)
9168 continue;
9169
9170 /* Now we have the cookie name between att_beg and att_end, and
9171 * its value between val_beg and val_end.
9172 */
9173
9174 if (att_end - att_beg == cookie_name_l &&
9175 memcmp(att_beg, cookie_name, cookie_name_l) == 0) {
9176 /* let's return this value and indicate where to go on from */
9177 *value = val_beg;
9178 *value_l = val_end - val_beg;
9179 return next + 1;
9180 }
9181
9182 /* Set-Cookie headers only have the name in the first attr=value part */
9183 if (!list)
9184 break;
9185 }
9186
9187 return NULL;
9188}
9189
Willy Tarreaue333ec92012-04-16 16:26:40 +02009190/* Iterate over all cookies present in a message. The context is stored in
Willy Tarreau37406352012-04-23 16:16:37 +02009191 * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the
Willy Tarreaua890d072013-04-02 12:01:06 +02009192 * end-of-header-value, and smp->ctx.a[2] for the hdr_ctx. Depending on
Willy Tarreaue333ec92012-04-16 16:26:40 +02009193 * the direction, multiple cookies may be parsed on the same line or not.
Willy Tarreau24e32d82012-04-23 23:55:44 +02009194 * The cookie name is in args and the name length in args->data.str.len.
Willy Tarreau28376d62012-04-26 21:26:10 +02009195 * Accepts exactly 1 argument of type string. If the input options indicate
9196 * that no iterating is desired, then only last value is fetched if any.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009197 */
9198static int
Willy Tarreau51539362012-05-08 12:46:28 +02009199smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9200 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009201{
9202 struct http_txn *txn = l7;
9203 struct hdr_idx *idx = &txn->hdr_idx;
Willy Tarreaua890d072013-04-02 12:01:06 +02009204 struct hdr_ctx *ctx = smp->ctx.a[2];
Willy Tarreaue333ec92012-04-16 16:26:40 +02009205 const struct http_msg *msg;
9206 const char *hdr_name;
9207 int hdr_name_len;
9208 char *sol;
Willy Tarreau28376d62012-04-26 21:26:10 +02009209 int occ = 0;
9210 int found = 0;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009211
Willy Tarreau24e32d82012-04-23 23:55:44 +02009212 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009213 return 0;
9214
Willy Tarreaua890d072013-04-02 12:01:06 +02009215 if (!ctx) {
9216 /* first call */
9217 ctx = &static_hdr_ctx;
9218 ctx->idx = 0;
9219 smp->ctx.a[2] = ctx;
9220 }
9221
Willy Tarreaue333ec92012-04-16 16:26:40 +02009222 CHECK_HTTP_MESSAGE_FIRST();
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009223
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009224 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009225 msg = &txn->req;
9226 hdr_name = "Cookie";
9227 hdr_name_len = 6;
9228 } else {
9229 msg = &txn->rsp;
9230 hdr_name = "Set-Cookie";
9231 hdr_name_len = 10;
9232 }
9233
Willy Tarreau28376d62012-04-26 21:26:10 +02009234 if (!occ && !(opt & SMP_OPT_ITERATE))
9235 /* no explicit occurrence and single fetch => last cookie by default */
9236 occ = -1;
9237
9238 /* OK so basically here, either we want only one value and it's the
9239 * last one, or we want to iterate over all of them and we fetch the
9240 * next one.
9241 */
9242
Willy Tarreau9b28e032012-10-12 23:49:43 +02009243 sol = msg->chn->buf->p;
Willy Tarreau37406352012-04-23 16:16:37 +02009244 if (!(smp->flags & SMP_F_NOT_LAST)) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009245 /* search for the header from the beginning, we must first initialize
9246 * the search parameters.
9247 */
Willy Tarreau37406352012-04-23 16:16:37 +02009248 smp->ctx.a[0] = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009249 ctx->idx = 0;
9250 }
9251
Willy Tarreau28376d62012-04-26 21:26:10 +02009252 smp->flags |= SMP_F_VOL_HDR;
9253
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009254 while (1) {
Willy Tarreau37406352012-04-23 16:16:37 +02009255 /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */
9256 if (!smp->ctx.a[0]) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009257 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx))
9258 goto out;
9259
Willy Tarreau24e32d82012-04-23 23:55:44 +02009260 if (ctx->vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009261 continue;
9262
Willy Tarreau37406352012-04-23 16:16:37 +02009263 smp->ctx.a[0] = ctx->line + ctx->val;
9264 smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009265 }
9266
Willy Tarreauf853c462012-04-23 18:53:56 +02009267 smp->type = SMP_T_CSTR;
Willy Tarreau37406352012-04-23 16:16:37 +02009268 smp->ctx.a[0] = extract_cookie_value(smp->ctx.a[0], smp->ctx.a[1],
Willy Tarreau24e32d82012-04-23 23:55:44 +02009269 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009270 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009271 &smp->data.str.str,
9272 &smp->data.str.len);
Willy Tarreau37406352012-04-23 16:16:37 +02009273 if (smp->ctx.a[0]) {
Willy Tarreau28376d62012-04-26 21:26:10 +02009274 found = 1;
9275 if (occ >= 0) {
9276 /* one value was returned into smp->data.str.{str,len} */
9277 smp->flags |= SMP_F_NOT_LAST;
9278 return 1;
9279 }
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009280 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009281 /* if we're looking for last occurrence, let's loop */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009282 }
Willy Tarreau28376d62012-04-26 21:26:10 +02009283 /* all cookie headers and values were scanned. If we're looking for the
9284 * last occurrence, we may return it now.
9285 */
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009286 out:
Willy Tarreau37406352012-04-23 16:16:37 +02009287 smp->flags &= ~SMP_F_NOT_LAST;
Willy Tarreau28376d62012-04-26 21:26:10 +02009288 return found;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009289}
9290
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009291/* Iterate over all cookies present in a request to count how many occurrences
Willy Tarreau24e32d82012-04-23 23:55:44 +02009292 * match the name in args and args->data.str.len. If <multi> is non-null, then
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009293 * multiple cookies may be parsed on the same line.
Willy Tarreau34db1082012-04-19 17:16:54 +02009294 * Accepts exactly 1 argument of type string.
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009295 */
9296static int
Willy Tarreau409bcde2013-01-08 00:31:00 +01009297smp_fetch_cookie_cnt(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009298 const struct arg *args, struct sample *smp)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009299{
9300 struct http_txn *txn = l7;
9301 struct hdr_idx *idx = &txn->hdr_idx;
9302 struct hdr_ctx ctx;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009303 const struct http_msg *msg;
9304 const char *hdr_name;
9305 int hdr_name_len;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009306 int cnt;
9307 char *val_beg, *val_end;
Willy Tarreaue333ec92012-04-16 16:26:40 +02009308 char *sol;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009309
Willy Tarreau24e32d82012-04-23 23:55:44 +02009310 if (!args || args->type != ARGT_STR)
Willy Tarreau34db1082012-04-19 17:16:54 +02009311 return 0;
9312
Willy Tarreaue333ec92012-04-16 16:26:40 +02009313 CHECK_HTTP_MESSAGE_FIRST();
9314
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009315 if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) {
Willy Tarreaue333ec92012-04-16 16:26:40 +02009316 msg = &txn->req;
9317 hdr_name = "Cookie";
9318 hdr_name_len = 6;
9319 } else {
9320 msg = &txn->rsp;
9321 hdr_name = "Set-Cookie";
9322 hdr_name_len = 10;
9323 }
9324
Willy Tarreau9b28e032012-10-12 23:49:43 +02009325 sol = msg->chn->buf->p;
Willy Tarreau46787ed2012-04-11 17:28:40 +02009326 val_end = val_beg = NULL;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009327 ctx.idx = 0;
9328 cnt = 0;
9329
9330 while (1) {
9331 /* Note: val_beg == NULL every time we need to fetch a new header */
9332 if (!val_beg) {
9333 if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx))
9334 break;
9335
Willy Tarreau24e32d82012-04-23 23:55:44 +02009336 if (ctx.vlen < args->data.str.len + 1)
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009337 continue;
9338
9339 val_beg = ctx.line + ctx.val;
9340 val_end = val_beg + ctx.vlen;
9341 }
9342
Willy Tarreauf853c462012-04-23 18:53:56 +02009343 smp->type = SMP_T_CSTR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009344 while ((val_beg = extract_cookie_value(val_beg, val_end,
Willy Tarreau24e32d82012-04-23 23:55:44 +02009345 args->data.str.str, args->data.str.len,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02009346 (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ,
Willy Tarreauf853c462012-04-23 18:53:56 +02009347 &smp->data.str.str,
9348 &smp->data.str.len))) {
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009349 cnt++;
9350 }
9351 }
9352
Willy Tarreauf853c462012-04-23 18:53:56 +02009353 smp->data.uint = cnt;
Willy Tarreau37406352012-04-23 16:16:37 +02009354 smp->flags |= SMP_F_VOL_HDR;
Willy Tarreau04aa6a92012-04-06 18:57:55 +02009355 return 1;
9356}
9357
Willy Tarreau51539362012-05-08 12:46:28 +02009358/* Fetch an cookie's integer value. The integer value is returned. It
9359 * takes a mandatory argument of type string. It relies on smp_fetch_cookie().
9360 */
9361static int
9362smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9363 const struct arg *args, struct sample *smp)
9364{
9365 int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp);
9366
9367 if (ret > 0) {
9368 smp->type = SMP_T_UINT;
9369 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9370 }
9371
9372 return ret;
9373}
9374
Willy Tarreau8797c062007-05-07 00:55:35 +02009375/************************************************************************/
Willy Tarreau12785782012-04-27 21:37:17 +02009376/* The code below is dedicated to sample fetches */
Willy Tarreau4a568972010-05-12 08:08:50 +02009377/************************************************************************/
9378
David Cournapeau16023ee2010-12-23 20:55:41 +09009379/*
9380 * Given a path string and its length, find the position of beginning of the
9381 * query string. Returns NULL if no query string is found in the path.
9382 *
9383 * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22:
9384 *
9385 * find_query_string(path, n) points to "yo=mama;ye=daddy" string.
9386 */
bedis4c75cca2012-10-05 08:38:24 +02009387static inline char *find_param_list(char *path, size_t path_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009388{
9389 char *p;
Emeric Brun485479d2010-09-23 18:02:19 +02009390
bedis4c75cca2012-10-05 08:38:24 +02009391 p = memchr(path, delim, path_l);
David Cournapeau16023ee2010-12-23 20:55:41 +09009392 return p ? p + 1 : NULL;
9393}
9394
bedis4c75cca2012-10-05 08:38:24 +02009395static inline int is_param_delimiter(char c, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009396{
bedis4c75cca2012-10-05 08:38:24 +02009397 return c == '&' || c == ';' || c == delim;
David Cournapeau16023ee2010-12-23 20:55:41 +09009398}
9399
9400/*
9401 * Given a url parameter, find the starting position of the first occurence,
9402 * or NULL if the parameter is not found.
9403 *
9404 * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye",
9405 * the function will return query_string+8.
9406 */
9407static char*
9408find_url_param_pos(char* query_string, size_t query_string_l,
bedis4c75cca2012-10-05 08:38:24 +02009409 char* url_param_name, size_t url_param_name_l,
9410 char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009411{
9412 char *pos, *last;
9413
9414 pos = query_string;
9415 last = query_string + query_string_l - url_param_name_l - 1;
9416
9417 while (pos <= last) {
9418 if (pos[url_param_name_l] == '=') {
9419 if (memcmp(pos, url_param_name, url_param_name_l) == 0)
9420 return pos;
9421 pos += url_param_name_l + 1;
9422 }
bedis4c75cca2012-10-05 08:38:24 +02009423 while (pos <= last && !is_param_delimiter(*pos, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009424 pos++;
9425 pos++;
9426 }
9427 return NULL;
9428}
9429
9430/*
9431 * Given a url parameter name, returns its value and size into *value and
9432 * *value_l respectively, and returns non-zero. If the parameter is not found,
9433 * zero is returned and value/value_l are not touched.
9434 */
9435static int
9436find_url_param_value(char* path, size_t path_l,
9437 char* url_param_name, size_t url_param_name_l,
bedis4c75cca2012-10-05 08:38:24 +02009438 char** value, int* value_l, char delim)
David Cournapeau16023ee2010-12-23 20:55:41 +09009439{
9440 char *query_string, *qs_end;
9441 char *arg_start;
9442 char *value_start, *value_end;
9443
bedis4c75cca2012-10-05 08:38:24 +02009444 query_string = find_param_list(path, path_l, delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009445 if (!query_string)
9446 return 0;
9447
9448 qs_end = path + path_l;
9449 arg_start = find_url_param_pos(query_string, qs_end - query_string,
bedis4c75cca2012-10-05 08:38:24 +02009450 url_param_name, url_param_name_l,
9451 delim);
David Cournapeau16023ee2010-12-23 20:55:41 +09009452 if (!arg_start)
9453 return 0;
9454
9455 value_start = arg_start + url_param_name_l + 1;
9456 value_end = value_start;
9457
bedis4c75cca2012-10-05 08:38:24 +02009458 while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009459 value_end++;
9460
9461 *value = value_start;
9462 *value_l = value_end - value_start;
Willy Tarreau00134332011-01-04 14:57:34 +01009463 return value_end != value_start;
David Cournapeau16023ee2010-12-23 20:55:41 +09009464}
9465
9466static int
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009467smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9468 const struct arg *args, struct sample *smp)
David Cournapeau16023ee2010-12-23 20:55:41 +09009469{
bedis4c75cca2012-10-05 08:38:24 +02009470 char delim = '?';
David Cournapeau16023ee2010-12-23 20:55:41 +09009471 struct http_txn *txn = l7;
9472 struct http_msg *msg = &txn->req;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009473
bedis4c75cca2012-10-05 08:38:24 +02009474 if (!args || args[0].type != ARGT_STR ||
9475 (args[1].type && args[1].type != ARGT_STR))
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009476 return 0;
9477
9478 CHECK_HTTP_MESSAGE_FIRST();
David Cournapeau16023ee2010-12-23 20:55:41 +09009479
bedis4c75cca2012-10-05 08:38:24 +02009480 if (args[1].type)
9481 delim = *args[1].data.str.str;
9482
Willy Tarreau9b28e032012-10-12 23:49:43 +02009483 if (!find_url_param_value(msg->chn->buf->p + msg->sl.rq.u, msg->sl.rq.u_l,
bedis4c75cca2012-10-05 08:38:24 +02009484 args->data.str.str, args->data.str.len,
9485 &smp->data.str.str, &smp->data.str.len,
9486 delim))
David Cournapeau16023ee2010-12-23 20:55:41 +09009487 return 0;
9488
Willy Tarreaub8c8f1f2012-04-23 22:38:26 +02009489 smp->type = SMP_T_CSTR;
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009490 smp->flags = SMP_F_VOL_1ST;
David Cournapeau16023ee2010-12-23 20:55:41 +09009491 return 1;
9492}
9493
Willy Tarreaua9fddca2012-07-31 07:51:48 +02009494/* Return the signed integer value for the specified url parameter (see url_param
9495 * above).
9496 */
9497static int
9498smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt,
9499 const struct arg *args, struct sample *smp)
9500{
9501 int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp);
9502
9503 if (ret > 0) {
9504 smp->type = SMP_T_UINT;
9505 smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len);
9506 }
9507
9508 return ret;
9509}
9510
Willy Tarreau185b5c42012-04-26 15:11:51 +02009511/* This function is used to validate the arguments passed to any "hdr" fetch
9512 * keyword. These keywords support an optional positive or negative occurrence
9513 * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It
9514 * is assumed that the types are already the correct ones. Returns 0 on error,
9515 * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an
9516 * error message in case of error, that the caller is responsible for freeing.
9517 * The initial location must either be freeable or NULL.
9518 */
9519static int val_hdr(struct arg *arg, char **err_msg)
9520{
9521 if (arg && arg[1].type == ARGT_SINT && arg[1].data.sint < -MAX_HDR_HISTORY) {
Willy Tarreaueb6cead2012-09-20 19:43:14 +02009522 memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY);
Willy Tarreau185b5c42012-04-26 15:11:51 +02009523 return 0;
9524 }
9525 return 1;
9526}
9527
Willy Tarreau4a568972010-05-12 08:08:50 +02009528/************************************************************************/
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009529/* All supported ACL keywords must be declared here. */
9530/************************************************************************/
9531
9532/* Note: must not be declared <const> as its list will be overwritten.
9533 * Please take care of keeping this list alphabetically sorted.
9534 */
9535static struct acl_kw_list acl_kws = {{ },{
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009536 { "base", "base", acl_parse_str, acl_match_str },
9537 { "base_beg", "base", acl_parse_str, acl_match_beg },
9538 { "base_dir", "base", acl_parse_str, acl_match_dir },
9539 { "base_dom", "base", acl_parse_str, acl_match_dom },
9540 { "base_end", "base", acl_parse_str, acl_match_end },
9541 { "base_len", "base", acl_parse_int, acl_match_len },
9542 { "base_reg", "base", acl_parse_reg, acl_match_reg },
9543 { "base_sub", "base", acl_parse_str, acl_match_sub },
Willy Tarreaua7ad50c2012-04-29 15:39:40 +02009544
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009545 { "cook", "req.cook", acl_parse_str, acl_match_str },
9546 { "cook_beg", "req.cook", acl_parse_str, acl_match_beg },
9547 { "cook_cnt", "req.cook_cnt", acl_parse_int, acl_match_int },
9548 { "cook_dir", "req.cook", acl_parse_str, acl_match_dir },
9549 { "cook_dom", "req.cook", acl_parse_str, acl_match_dom },
9550 { "cook_end", "req.cook", acl_parse_str, acl_match_end },
9551 { "cook_len", "req.cook", acl_parse_int, acl_match_len },
9552 { "cook_reg", "req.cook", acl_parse_reg, acl_match_reg },
9553 { "cook_sub", "req.cook", acl_parse_str, acl_match_sub },
9554 { "cook_val", "req.cook_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009555
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009556 { "hdr", "req.hdr", acl_parse_str, acl_match_str },
9557 { "hdr_beg", "req.hdr", acl_parse_str, acl_match_beg },
9558 { "hdr_cnt", "req.hdr_cnt", acl_parse_int, acl_match_int },
9559 { "hdr_dir", "req.hdr", acl_parse_str, acl_match_dir },
9560 { "hdr_dom", "req.hdr", acl_parse_str, acl_match_dom },
9561 { "hdr_end", "req.hdr", acl_parse_str, acl_match_end },
9562 { "hdr_ip", "req.hdr_ip", acl_parse_ip, acl_match_ip },
9563 { "hdr_len", "req.hdr", acl_parse_int, acl_match_len },
9564 { "hdr_reg", "req.hdr", acl_parse_reg, acl_match_reg },
9565 { "hdr_sub", "req.hdr", acl_parse_str, acl_match_sub },
9566 { "hdr_val", "req.hdr_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009567
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009568 { "http_auth", NULL, acl_parse_nothing, acl_match_nothing },
9569 { "http_auth_group", NULL, acl_parse_strcat, acl_match_auth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009570
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009571 { "http_first_req", NULL, acl_parse_nothing, acl_match_nothing },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009572
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009573 { "method", NULL, acl_parse_meth, acl_match_meth },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009574
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009575 { "path", "path", acl_parse_str, acl_match_str },
9576 { "path_beg", "path", acl_parse_str, acl_match_beg },
9577 { "path_dir", "path", acl_parse_str, acl_match_dir },
9578 { "path_dom", "path", acl_parse_str, acl_match_dom },
9579 { "path_end", "path", acl_parse_str, acl_match_end },
9580 { "path_len", "path", acl_parse_int, acl_match_len },
9581 { "path_reg", "path", acl_parse_reg, acl_match_reg },
9582 { "path_sub", "path", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009583
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009584 { "req_proto_http", "req.proto_http",acl_parse_nothing, acl_match_nothing },
Willy Tarreauff5afcc2013-03-31 18:49:18 +02009585 { "req_ver", "req.ver", acl_parse_str, acl_match_str },
9586 { "resp_ver", "res.ver", acl_parse_str, acl_match_str },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009587
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009588 { "scook", "res.cook", acl_parse_str, acl_match_str },
9589 { "scook_beg", "res.cook", acl_parse_str, acl_match_beg },
9590 { "scook_cnt", "res.cook_cnt", acl_parse_int, acl_match_int },
9591 { "scook_dir", "res.cook", acl_parse_str, acl_match_dir },
9592 { "scook_dom", "res.cook", acl_parse_str, acl_match_dom },
9593 { "scook_end", "res.cook", acl_parse_str, acl_match_end },
9594 { "scook_len", "res.cook", acl_parse_int, acl_match_len },
9595 { "scook_reg", "res.cook", acl_parse_reg, acl_match_reg },
9596 { "scook_sub", "res.cook", acl_parse_str, acl_match_sub },
9597 { "scook_val", "res.cook_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009598
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009599 { "shdr", "res.hdr", acl_parse_str, acl_match_str },
9600 { "shdr_beg", "res.hdr", acl_parse_str, acl_match_beg },
9601 { "shdr_cnt", "res.hdr_cnt", acl_parse_int, acl_match_int },
9602 { "shdr_dir", "res.hdr", acl_parse_str, acl_match_dir },
9603 { "shdr_dom", "res.hdr", acl_parse_str, acl_match_dom },
9604 { "shdr_end", "res.hdr", acl_parse_str, acl_match_end },
9605 { "shdr_ip", "res.hdr_ip", acl_parse_ip, acl_match_ip },
9606 { "shdr_len", "res.hdr", acl_parse_int, acl_match_len },
9607 { "shdr_reg", "res.hdr", acl_parse_reg, acl_match_reg },
9608 { "shdr_sub", "res.hdr", acl_parse_str, acl_match_sub },
9609 { "shdr_val", "res.hdr_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009610
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009611 { "status", NULL, acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009612
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009613 { "url", "url", acl_parse_str, acl_match_str },
9614 { "url_beg", "url", acl_parse_str, acl_match_beg },
9615 { "url_dir", "url", acl_parse_str, acl_match_dir },
9616 { "url_dom", "url", acl_parse_str, acl_match_dom },
9617 { "url_end", "url", acl_parse_str, acl_match_end },
9618 { "url_ip", "url_ip", acl_parse_ip, acl_match_ip },
9619 { "url_len", "url", acl_parse_int, acl_match_len },
9620 { "url_port", "url_port", acl_parse_int, acl_match_int },
9621 { "url_reg", "url", acl_parse_reg, acl_match_reg },
9622 { "url_sub", "url", acl_parse_str, acl_match_sub },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009623
Willy Tarreaud86e29d2013-03-25 08:21:05 +01009624 { "urlp", "urlp", acl_parse_str, acl_match_str },
9625 { "urlp_beg", "urlp", acl_parse_str, acl_match_beg },
9626 { "urlp_dir", "urlp", acl_parse_str, acl_match_dir },
9627 { "urlp_dom", "urlp", acl_parse_str, acl_match_dom },
9628 { "urlp_end", "urlp", acl_parse_str, acl_match_end },
9629 { "urlp_ip", "urlp", acl_parse_ip, acl_match_ip },
9630 { "urlp_len", "urlp", acl_parse_int, acl_match_len },
9631 { "urlp_reg", "urlp", acl_parse_reg, acl_match_reg },
9632 { "urlp_sub", "urlp", acl_parse_str, acl_match_sub },
9633 { "urlp_val", "urlp_val", acl_parse_int, acl_match_int },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009634
Willy Tarreau8ed669b2013-01-11 15:49:37 +01009635 { /* END */ },
Willy Tarreau25c1ebc2012-04-25 16:21:44 +02009636}};
9637
9638/************************************************************************/
9639/* All supported pattern keywords must be declared here. */
Willy Tarreau4a568972010-05-12 08:08:50 +02009640/************************************************************************/
9641/* Note: must not be declared <const> as its list will be overwritten */
Willy Tarreau12785782012-04-27 21:37:17 +02009642static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{
Willy Tarreau409bcde2013-01-08 00:31:00 +01009643 { "base", smp_fetch_base, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9644 { "base32", smp_fetch_base32, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
9645 { "base32+src", smp_fetch_base32_src, 0, NULL, SMP_T_BIN, SMP_USE_HRQHV },
9646
9647 /* cookie is valid in both directions (eg: for "stick ...") but cook*
9648 * are only here to match the ACL's name, are request-only and are used
9649 * for ACL compatibility only.
9650 */
9651 { "cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9652 { "cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
9653 { "cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9654 { "cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9655
9656 /* hdr is valid in both directions (eg: for "stick ...") but hdr_* are
9657 * only here to match the ACL's name, are request-only and are used for
9658 * ACL compatibility only.
9659 */
9660 { "hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV|SMP_USE_HRSHV },
9661 { "hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9662 { "hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
9663 { "hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
9664
Willy Tarreau0a0daec2013-04-02 22:44:58 +02009665 { "http_auth", smp_fetch_http_auth, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
9666 { "http_auth_group", smp_fetch_http_auth_grp, ARG1(1,USR), NULL, SMP_T_BOOL, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009667 { "http_first_req", smp_fetch_http_first_req, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
9668 { "method", smp_fetch_meth, 0, NULL, SMP_T_UINT, SMP_USE_HRQHP },
9669 { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +01009670
9671 /* HTTP protocol on the request path */
9672 { "req.proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009673 { "req_proto_http", smp_fetch_proto_http, 0, NULL, SMP_T_BOOL, SMP_USE_HRQHP },
Willy Tarreau18ed2562013-01-14 15:56:36 +01009674
9675 /* HTTP version on the request path */
9676 { "req.ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009677 { "req_ver", smp_fetch_rqver, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
Willy Tarreau18ed2562013-01-14 15:56:36 +01009678
9679 /* HTTP version on the response path */
9680 { "res.ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
Willy Tarreau409bcde2013-01-08 00:31:00 +01009681 { "resp_ver", smp_fetch_stver, 0, NULL, SMP_T_CSTR, SMP_USE_HRSHV },
9682
Willy Tarreau18ed2562013-01-14 15:56:36 +01009683 /* explicit req.{cook,hdr} are used to force the fetch direction to be request-only */
9684 { "req.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9685 { "req.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9686 { "req.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9687
9688 { "req.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRQHV },
9689 { "req.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9690 { "req.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRQHV },
9691 { "req.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRQHV },
9692
9693 /* explicit req.{cook,hdr} are used to force the fetch direction to be response-only */
9694 { "res.cook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
9695 { "res.cook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9696 { "res.cook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9697
9698 { "res.hdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
9699 { "res.hdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9700 { "res.hdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
9701 { "res.hdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
9702
Willy Tarreau409bcde2013-01-08 00:31:00 +01009703 /* scook is valid only on the response and is used for ACL compatibility */
9704 { "scook", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV },
9705 { "scook_cnt", smp_fetch_cookie_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9706 { "scook_val", smp_fetch_cookie_val, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9707 { "set-cookie", smp_fetch_cookie, ARG1(0,STR), NULL, SMP_T_CSTR, SMP_USE_HRSHV }, /* deprecated */
9708
9709 /* shdr is valid only on the response and is used for ACL compatibility */
9710 { "shdr", smp_fetch_hdr, ARG2(0,STR,SINT), val_hdr, SMP_T_CSTR, SMP_USE_HRSHV },
9711 { "shdr_cnt", smp_fetch_hdr_cnt, ARG1(0,STR), NULL, SMP_T_UINT, SMP_USE_HRSHV },
9712 { "shdr_ip", smp_fetch_hdr_ip, ARG2(0,STR,SINT), val_hdr, SMP_T_IPV4, SMP_USE_HRSHV },
9713 { "shdr_val", smp_fetch_hdr_val, ARG2(0,STR,SINT), val_hdr, SMP_T_UINT, SMP_USE_HRSHV },
9714
9715 { "status", smp_fetch_stcode, 0, NULL, SMP_T_UINT, SMP_USE_HRSHP },
9716 { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9717 { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_USE_HRQHV },
9718 { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_USE_HRQHV },
9719 { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9720 { "urlp" , smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_USE_HRQHV },
9721 { "urlp_val", smp_fetch_url_param_val, ARG2(1,STR,STR), NULL, SMP_T_UINT, SMP_USE_HRQHV },
9722 { /* END */ },
Willy Tarreau4a568972010-05-12 08:08:50 +02009723}};
9724
Willy Tarreau8797c062007-05-07 00:55:35 +02009725
9726__attribute__((constructor))
9727static void __http_protocol_init(void)
9728{
9729 acl_register_keywords(&acl_kws);
Willy Tarreau12785782012-04-27 21:37:17 +02009730 sample_register_fetches(&sample_fetch_keywords);
Willy Tarreau8797c062007-05-07 00:55:35 +02009731}
9732
9733
Willy Tarreau58f10d72006-12-04 02:26:12 +01009734/*
Willy Tarreaubaaee002006-06-26 02:48:02 +02009735 * Local variables:
9736 * c-indent-level: 8
9737 * c-basic-offset: 8
9738 * End:
9739 */